diff --git a/src/Select.tsx b/src/Select.tsx index e8c6d5cc..41bd66cc 100644 --- a/src/Select.tsx +++ b/src/Select.tsx @@ -459,6 +459,10 @@ const Select = React.forwardRef item[mergedFieldNames.value] === mergedSearchValue)) { return filteredOptions; } + // Skip creating temp tag option if it matches a disabled option value + if (valueOptions.get(mergedSearchValue)?.disabled) { + return filteredOptions; + } // Fill search value as option return [createTagOption(mergedSearchValue), ...filteredOptions]; }, [ @@ -468,6 +472,7 @@ const Select = React.forwardRef { const sortedOptions = [...inputOptions].sort((a, b) => @@ -628,6 +633,12 @@ const Select = React.forwardRef([...rawValues, formatted])); triggerChange(newRawValues); triggerSelect(formatted, true); @@ -658,6 +669,11 @@ const Select = React.forwardRef val !== undefined); } + // Filter out disabled option values in tags mode + if (mode === 'tags') { + patchValues = patchValues.filter((val) => !valueOptions.get(val)?.disabled); + } + const newRawValues = Array.from(new Set([...rawValues, ...patchValues])); triggerChange(newRawValues); newRawValues.forEach((newRawValue) => { diff --git a/tests/Tags.test.tsx b/tests/Tags.test.tsx index cfee62b3..5a4ad6e6 100644 --- a/tests/Tags.test.tsx +++ b/tests/Tags.test.tsx @@ -59,6 +59,54 @@ describe('Select.Tags', () => { expect(onChange).toHaveBeenCalledWith(['foo'], [{}]); }); + // https://github.com/ant-design/ant-design/issues/30878 + it('should not create tag when input matches disabled option on blur', () => { + const onChange = jest.fn(); + const { container } = render( + false} + />, + ); + + fireEvent.change(container.querySelector('input'), { target: { value: 'orange' } }); + fireEvent.keyDown(container.querySelector('input'), { key: 'Enter' }); + + jest.runAllTimers(); + // Should not create tag or show temp option in dropdown + expect(findSelection(container).textContent).toBe(''); + expect(onChange).not.toHaveBeenCalled(); + }); + it('tokenize input', () => { const handleChange = jest.fn(); const handleSelect = jest.fn(); @@ -82,6 +130,28 @@ describe('Select.Tags', () => { expectOpen(container, false); }); + // https://github.com/ant-design/ant-design/issues/30878 + it('should not create tag when token matches disabled option with tokenSeparators', () => { + const handleChange = jest.fn(); + const { container } = render( +