Skip to content

Commit 20adf23

Browse files
committed
Support invalid state for ChipsInput chips
1 parent 6c1cf58 commit 20adf23

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

demo/src/screens/incubatorScreens/IncubatorChipsInputScreen.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, {Component} from 'react';
22
import {View, Text, Card, TextField, Button, Colors, Incubator} from 'react-native-ui-lib'; //eslint-disable-line
3+
import _ from 'lodash';
34

45
export default class ChipsInputScreen extends Component {
56
state = {
@@ -21,9 +22,28 @@ export default class ChipsInputScreen extends Component {
2122
containerStyle: {borderWidth: 0},
2223
dismissColor: Colors.white
2324
}}
25+
invalidChipProps={{
26+
dismissColor: Colors.red30,
27+
labelStyle: {color: Colors.red30},
28+
backgroundColor: Colors.white,
29+
containerStyle: {borderColor: Colors.red30}
30+
}}
2431
chips={this.state.chips}
2532
leadingAccessory={<Text>TO: </Text>}
2633
onChange={newChips => {
34+
_.chain(newChips)
35+
.groupBy('label')
36+
.forEach(group => {
37+
if (group.length === 1) {
38+
delete group[0].invalid;
39+
} else {
40+
group[group.length - 1].invalid = true;
41+
}
42+
})
43+
.values()
44+
.flatten()
45+
.value();
46+
2747
this.setState({chips: newChips});
2848
}}
2949
/>

generatedTypes/src/incubator/ChipsInput/index.d.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@ export declare enum ChipsInputChangeReason {
55
Added = "added",
66
Removed = "removed"
77
}
8+
declare type ChipsInputChipProps = ChipProps & {
9+
invalid?: boolean;
10+
};
811
export declare type ChipsInputProps = Omit<TextFieldProps, 'ref'> & {
912
/**
1013
* Chip items to render in the input
1114
*/
12-
chips?: ChipProps[];
15+
chips?: ChipsInputChipProps[];
1316
/**
1417
* A default set of chip props to pass to all chips
1518
*/
1619
defaultChipProps?: ChipProps;
20+
/**
21+
* A default set of chip props to pass to all invalid chips
22+
*/
23+
invalidChipProps?: ChipProps;
1724
/**
1825
* Change callback for when chips changed (either added or removed)
1926
*/
20-
onChange?: (chips: ChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void;
27+
onChange?: (chips: ChipsInputChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void;
2128
/**
2229
* Maximum chips
2330
*/
@@ -27,15 +34,19 @@ declare const _default: React.ForwardRefExoticComponent<Omit<TextFieldProps, "re
2734
/**
2835
* Chip items to render in the input
2936
*/
30-
chips?: ChipProps[] | undefined;
37+
chips?: ChipsInputChipProps[] | undefined;
3138
/**
3239
* A default set of chip props to pass to all chips
3340
*/
3441
defaultChipProps?: ChipProps | undefined;
42+
/**
43+
* A default set of chip props to pass to all invalid chips
44+
*/
45+
invalidChipProps?: ChipProps | undefined;
3546
/**
3647
* Change callback for when chips changed (either added or removed)
3748
*/
38-
onChange?: ((chips: ChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void) | undefined;
49+
onChange?: ((chips: ChipsInputChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void) | undefined;
3950
/**
4051
* Maximum chips
4152
*/

src/incubator/ChipsInput/index.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,25 @@ export enum ChipsInputChangeReason {
1313
Removed = 'removed'
1414
}
1515

16+
type ChipsInputChipProps = ChipProps & {invalid?: boolean};
17+
1618
export type ChipsInputProps = Omit<TextFieldProps, 'ref'> & {
1719
/**
1820
* Chip items to render in the input
1921
*/
20-
chips?: ChipProps[];
22+
chips?: ChipsInputChipProps[];
2123
/**
2224
* A default set of chip props to pass to all chips
2325
*/
2426
defaultChipProps?: ChipProps;
27+
/**
28+
* A default set of chip props to pass to all invalid chips
29+
*/
30+
invalidChipProps?: ChipProps;
2531
/**
2632
* Change callback for when chips changed (either added or removed)
2733
*/
28-
onChange?: (chips: ChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void;
34+
onChange?: (chips: ChipsInputChipProps[], changeReason: ChipsInputChangeReason, updatedChip: ChipProps) => void;
2935
/**
3036
* Maximum chips
3137
*/
@@ -34,7 +40,16 @@ export type ChipsInputProps = Omit<TextFieldProps, 'ref'> & {
3440

3541
const ChipsInput = (props: ChipsInputProps, refToForward: React.Ref<any>) => {
3642
const fieldRef = useCombinedRefs(refToForward);
37-
const {chips = [], defaultChipProps, leadingAccessory, onChange, fieldStyle, maxChips, ...others} = props;
43+
const {
44+
chips = [],
45+
defaultChipProps,
46+
invalidChipProps,
47+
leadingAccessory,
48+
onChange,
49+
fieldStyle,
50+
maxChips,
51+
...others
52+
} = props;
3853
const [markedForRemoval, setMarkedForRemoval] = useState<number | undefined>(undefined);
3954
const fieldValue = useRef(others.value);
4055

@@ -110,6 +125,7 @@ const ChipsInput = (props: ChipsInputProps, refToForward: React.Ref<any>) => {
110125
marginB-s2
111126
dismissIcon={removeIcon}
112127
{...defaultChipProps}
128+
{...(chip.invalid ? invalidChipProps : undefined)}
113129
{...chip}
114130
onPress={onChipPress}
115131
onDismiss={isMarkedForRemoval ? removeMarkedChip : undefined}

0 commit comments

Comments
 (0)