Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,10 @@ const Select = React.forwardRef<BaseSelectRef, SelectProps<any, DefaultOptionTyp

const newRawValues = Array.from(new Set<RawValueType>([...rawValues, ...patchValues]));
triggerChange(newRawValues);
newRawValues.forEach((newRawValue) => {
triggerSelect(newRawValue, true);
// Emit `onSelect` once per token of the current input. Other values that were already
// selected are not selected again, so they must not trigger it.
new Set<RawValueType>(patchValues).forEach((patchValue) => {
triggerSelect(patchValue, true);
});
};

Expand Down
30 changes: 30 additions & 0 deletions tests/Tags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,36 @@ describe('Select.Tags', () => {
expectOpen(container, false);
});

it('should not trigger onSelect for unrelated selected values when separating tokens', () => {
const handleChange = jest.fn();
const handleSelect = jest.fn();
const { container } = render(
<Select
mode="tags"
defaultValue={['a', 'b']}
tokenSeparators={[',']}
onChange={handleChange}
onSelect={handleSelect}
/>,
);

fireEvent.change(container.querySelector('input'), { target: { value: '2,3,' } });

expect(handleChange).toHaveBeenCalledWith(['a', 'b', '2', '3'], expect.anything());
expect(handleSelect.mock.calls.map(([value]) => value)).toEqual(['2', '3']);
});

it('should trigger onSelect once for each token of the input, even an already selected one', () => {
const handleSelect = jest.fn();
const { container } = render(
<Select mode="tags" defaultValue={['1']} tokenSeparators={[',']} onSelect={handleSelect} />,
);

fireEvent.change(container.querySelector('input'), { target: { value: '1,2,2,' } });

expect(handleSelect.mock.calls.map(([value]) => value)).toEqual(['1', '2']);
});

it('should work when menu is closed', () => {
const handleChange = jest.fn();
const handleSelect = jest.fn();
Expand Down