fix: Don't allow duplicated tag values in the Select (#19283)

* fix: Don't allow duplicated tag values in the Select

* Addresses comments and adds test
This commit is contained in:
Michael S. Molina 2022-03-22 09:05:22 -03:00 committed by GitHub
parent a8a48af7fa
commit d3ce398448
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 23 deletions

View File

@ -314,6 +314,17 @@ test('searches for custom fields', async () => {
expect(screen.getByText(NO_DATA)).toBeInTheDocument();
});
test('removes duplicated values', async () => {
render(<Select {...defaultProps} mode="multiple" allowNewOptions />);
await type('a,b,b,b,c,d,d');
const values = await findAllSelectValues();
expect(values.length).toBe(4);
expect(values[0]).toHaveTextContent('a');
expect(values[1]).toHaveTextContent('b');
expect(values[2]).toHaveTextContent('c');
expect(values[3]).toHaveTextContent('d');
});
test('renders a custom label', async () => {
const options = [
{ label: 'John', value: 1, customLabel: <h1>John</h1> },

View File

@ -385,31 +385,23 @@ const Select = (
const hasCustomLabels = fullSelectOptions.some(opt => !!opt?.customLabel);
const handleOnSelect = (
selectedValue: string | number | AntdLabeledValue,
) => {
const handleOnSelect = (selectedItem: string | number | AntdLabeledValue) => {
if (isSingleMode) {
setSelectValue(selectedValue);
setSelectValue(selectedItem);
} else {
const currentSelected = selectValue
? Array.isArray(selectValue)
? selectValue
: [selectValue]
: [];
if (
typeof selectedValue === 'number' ||
typeof selectedValue === 'string'
) {
setSelectValue([
...(currentSelected as (string | number)[]),
selectedValue as string | number,
]);
} else {
setSelectValue([
...(currentSelected as AntdLabeledValue[]),
selectedValue as AntdLabeledValue,
]);
}
setSelectValue(previousState => {
const array = ensureIsArray(previousState);
const isObject = typeof selectedItem === 'object';
const value = isObject ? selectedItem.value : selectedItem;
// Tokenized values can contain duplicated values
if (!hasOption(value, array)) {
const result = [...array, selectedItem];
return isObject
? (result as AntdLabeledValue[])
: (result as (string | number)[]);
}
return previousState;
});
}
setInputValue('');
};