diff --git a/superset-frontend/src/components/Select/Select.test.tsx b/superset-frontend/src/components/Select/Select.test.tsx
index 4701af236ceba..b247950abafb8 100644
--- a/superset-frontend/src/components/Select/Select.test.tsx
+++ b/superset-frontend/src/components/Select/Select.test.tsx
@@ -314,6 +314,17 @@ test('searches for custom fields', async () => {
expect(screen.getByText(NO_DATA)).toBeInTheDocument();
});
+test('removes duplicated values', async () => {
+ render();
+ 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:
John
},
diff --git a/superset-frontend/src/components/Select/Select.tsx b/superset-frontend/src/components/Select/Select.tsx
index f1c78ab09892e..fde08fdf3d336 100644
--- a/superset-frontend/src/components/Select/Select.tsx
+++ b/superset-frontend/src/components/Select/Select.tsx
@@ -385,34 +385,20 @@ const Select = (
const hasCustomLabels = fullSelectOptions.some(opt => !!opt?.customLabel);
- const valueAsArray = (value: any): T[] => {
- const array = value ? (Array.isArray(value) ? value : [value]) : [];
- return array as T[];
- };
-
- const handleOnSelect = (
- selectedValue: string | number | AntdLabeledValue,
- ) => {
+ const handleOnSelect = (selectedItem: string | number | AntdLabeledValue) => {
if (isSingleMode) {
- setSelectValue(selectedValue);
- } else if (
- typeof selectedValue === 'number' ||
- typeof selectedValue === 'string'
- ) {
- setSelectValue(previousState => {
- const primitiveArray = valueAsArray(previousState);
- // Tokenized values can contain duplicated values
- if (!primitiveArray.some(value => value === selectedValue)) {
- return [...primitiveArray, selectedValue as string | number];
- }
- return previousState;
- });
+ setSelectValue(selectedItem);
} else {
setSelectValue(previousState => {
- const objectArray = valueAsArray(previousState);
+ const array = ensureIsArray(previousState);
+ const isObject = typeof selectedItem === 'object';
+ const value = isObject ? selectedItem.value : selectedItem;
// Tokenized values can contain duplicated values
- if (!objectArray.some(object => object.value === selectedValue.label)) {
- return [...objectArray, selectedValue as AntdLabeledValue];
+ if (!hasOption(value, array)) {
+ const result = [...array, selectedItem];
+ return isObject
+ ? (result as AntdLabeledValue[])
+ : (result as (string | number)[]);
}
return previousState;
});