Skip to content
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

fix: ignore when search input value equal option item value #969

Merged
merged 2 commits into from
Aug 2, 2023
Merged
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
14 changes: 12 additions & 2 deletions src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,20 @@ const Select = React.forwardRef(
) {
return filteredOptions;
}

// ignore when search value equal select input value
if (filteredOptions.some((item) => item[mergedFieldNames.value] === mergedSearchValue)) {
return filteredOptions;
}
// Fill search value as option
return [createTagOption(mergedSearchValue), ...filteredOptions];
}, [createTagOption, optionFilterProp, mode, filteredOptions, mergedSearchValue]);
}, [
createTagOption,
optionFilterProp,
mode,
filteredOptions,
mergedSearchValue,
mergedFieldNames,
]);

const orderedFilteredOptions = React.useMemo(() => {
if (!filterSort) {
Expand Down
22 changes: 22 additions & 0 deletions tests/Tags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,26 @@ describe('Select.Tags', () => {

expect(preventDefault).toHaveBeenCalled();
});

// https://github.com/ant-design/ant-design/issues/43954
it('when insert a same input is one of options value, should not create a tag option', () => {
const errSpy = jest.spyOn(console, 'error');

const wrapper = mount(
<Select
mode="tags"
options={[
{ label: 'US-美国', value: 'US' },
{ label: 'CN-中国', value: 'CN' },
]}
optionFilterProp="label"
/>,
);
toggleOpen(wrapper);
expect(wrapper.find('.rc-select-item-option').length).toBe(2);
wrapper.find('input').simulate('change', { target: { value: 'US' } });

expect(wrapper.find('.rc-select-item-option').length).toBe(1);
expect(errSpy).not.toHaveBeenCalled();
ZWkang marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading