Skip to content

Commit

Permalink
fix: Don't allow duplicated tag values in the Select (apache#19283)
Browse files Browse the repository at this point in the history
* fix: Don't allow duplicated tag values in the Select

* Addresses comments and adds test
  • Loading branch information
michael-s-molina authored and michael_hoffman committed Mar 23, 2022
1 parent c068939 commit c5b61e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
11 changes: 11 additions & 0 deletions superset-frontend/src/components/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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> },
Expand Down
38 changes: 15 additions & 23 deletions superset-frontend/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
};
Expand Down

0 comments on commit c5b61e8

Please sign in to comment.