Skip to content

fixed maxChips props when chips passed as prop #2680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/components/chipsInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export enum ChipsInputChangeReason {
Removed = 'removed'
}

type RenderChip = {index: number; chip: ChipsInputChipProps; isMarkedForRemoval: boolean};

export type ChipsInputChipProps = ChipProps & {invalid?: boolean};

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

const renderChip = (props: RenderChip) => {
const {index, chip, isMarkedForRemoval} = props;
return (
<Chip
key={index}
customValue={index}
// resetSpacings
// paddingH-s2
marginR-s2
marginB-s2
dismissIcon={removeIcon}
recorderTag={'mask'}
{...defaultChipProps}
{...(chip.invalid ? invalidChipProps : undefined)}
{...chip}
onPress={onChipPress}
onDismiss={isMarkedForRemoval ? removeMarkedChip : undefined}
/>
);
};

const chipList = useMemo(() => {
return (
<>
{leadingAccessory}
{map(chips, (chip, index) => {
const isMarkedForRemoval = index === markedForRemoval;
return (
<Chip
key={index}
customValue={index}
// resetSpacings
// paddingH-s2
marginR-s2
marginB-s2
dismissIcon={removeIcon}
recorderTag={'mask'}
{...defaultChipProps}
{...(chip.invalid ? invalidChipProps : undefined)}
{...chip}
onPress={onChipPress}
onDismiss={isMarkedForRemoval ? removeMarkedChip : undefined}
/>
);
if (!maxChips || index < maxChips) {
return renderChip({index, chip, isMarkedForRemoval});
}
})}
</>
);
}, [chips, leadingAccessory, defaultChipProps, removeMarkedChip, markedForRemoval]);
}, [chips, leadingAccessory, defaultChipProps, removeMarkedChip, markedForRemoval, maxChips]);

return (
<TextField
Expand Down