Skip to content

Commit eb25f4d

Browse files
authored
fixed maxChips props when chips passed as prop (#2680)
* fixed maxChips props when chips passed as prop * fixed red screen * fixed review notes * refactor the maxChips statement
1 parent 1cd67e4 commit eb25f4d

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/components/chipsInput/index.tsx

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

16+
type RenderChip = {index: number; chip: ChipsInputChipProps; isMarkedForRemoval: boolean};
17+
1618
export type ChipsInputChipProps = ChipProps & {invalid?: boolean};
1719

1820
export type ChipsInputProps = Omit<TextFieldProps, 'ref'> & {
@@ -109,33 +111,40 @@ const ChipsInput = forwardRef((props: ChipsInputProps, refToForward: React.Ref<a
109111
},
110112
[chips, props.onKeyPress, markedForRemoval, removeMarkedChip]);
111113

114+
const renderChip = (props: RenderChip) => {
115+
const {index, chip, isMarkedForRemoval} = props;
116+
return (
117+
<Chip
118+
key={index}
119+
customValue={index}
120+
// resetSpacings
121+
// paddingH-s2
122+
marginR-s2
123+
marginB-s2
124+
dismissIcon={removeIcon}
125+
recorderTag={'mask'}
126+
{...defaultChipProps}
127+
{...(chip.invalid ? invalidChipProps : undefined)}
128+
{...chip}
129+
onPress={onChipPress}
130+
onDismiss={isMarkedForRemoval ? removeMarkedChip : undefined}
131+
/>
132+
);
133+
};
134+
112135
const chipList = useMemo(() => {
113136
return (
114137
<>
115138
{leadingAccessory}
116139
{map(chips, (chip, index) => {
117140
const isMarkedForRemoval = index === markedForRemoval;
118-
return (
119-
<Chip
120-
key={index}
121-
customValue={index}
122-
// resetSpacings
123-
// paddingH-s2
124-
marginR-s2
125-
marginB-s2
126-
dismissIcon={removeIcon}
127-
recorderTag={'mask'}
128-
{...defaultChipProps}
129-
{...(chip.invalid ? invalidChipProps : undefined)}
130-
{...chip}
131-
onPress={onChipPress}
132-
onDismiss={isMarkedForRemoval ? removeMarkedChip : undefined}
133-
/>
134-
);
141+
if (!maxChips || index < maxChips) {
142+
return renderChip({index, chip, isMarkedForRemoval});
143+
}
135144
})}
136145
</>
137146
);
138-
}, [chips, leadingAccessory, defaultChipProps, removeMarkedChip, markedForRemoval]);
147+
}, [chips, leadingAccessory, defaultChipProps, removeMarkedChip, markedForRemoval, maxChips]);
139148

140149
return (
141150
<TextField

0 commit comments

Comments
 (0)