Skip to content

Commit 2879b2c

Browse files
authored
fix: ignore when search input value equal option item value (#969)
* fix: ignore when search input value equal option item value * fix: add test option items length and mock the issue situation
1 parent 889122e commit 2879b2c

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/Select.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,20 @@ const Select = React.forwardRef(
403403
) {
404404
return filteredOptions;
405405
}
406-
406+
// ignore when search value equal select input value
407+
if (filteredOptions.some((item) => item[mergedFieldNames.value] === mergedSearchValue)) {
408+
return filteredOptions;
409+
}
407410
// Fill search value as option
408411
return [createTagOption(mergedSearchValue), ...filteredOptions];
409-
}, [createTagOption, optionFilterProp, mode, filteredOptions, mergedSearchValue]);
412+
}, [
413+
createTagOption,
414+
optionFilterProp,
415+
mode,
416+
filteredOptions,
417+
mergedSearchValue,
418+
mergedFieldNames,
419+
]);
410420

411421
const orderedFilteredOptions = React.useMemo(() => {
412422
if (!filterSort) {

tests/Tags.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,26 @@ describe('Select.Tags', () => {
483483

484484
expect(preventDefault).toHaveBeenCalled();
485485
});
486+
487+
// https://github.com/ant-design/ant-design/issues/43954
488+
it('when insert a same input is one of options value, should not create a tag option', () => {
489+
const errSpy = jest.spyOn(console, 'error');
490+
491+
const wrapper = mount(
492+
<Select
493+
mode="tags"
494+
options={[
495+
{ label: 'US-美国', value: 'US' },
496+
{ label: 'CN-中国', value: 'CN' },
497+
]}
498+
optionFilterProp="label"
499+
/>,
500+
);
501+
toggleOpen(wrapper);
502+
expect(wrapper.find('.rc-select-item-option').length).toBe(2);
503+
wrapper.find('input').simulate('change', { target: { value: 'US' } });
504+
505+
expect(wrapper.find('.rc-select-item-option').length).toBe(1);
506+
expect(errSpy).not.toHaveBeenCalled();
507+
});
486508
});

0 commit comments

Comments
 (0)