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 2 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
46 changes: 29 additions & 17 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,29 +111,39 @@ 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});
}
if (!maxChips) {
return renderChip({index, chip, isMarkedForRemoval});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (maxChips && index < maxChips) {
return renderChip({index, chip, isMarkedForRemoval});
}
if (!maxChips) {
return renderChip({index, chip, isMarkedForRemoval});
}
if (!maxChips || (maxChips && index < maxChips)) {
return renderChip({index, chip, isMarkedForRemoval});
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maxChips is missing from the dependencies

})}
</>
);
Expand Down