From 323f43ff90a523ec0ed6d219f169ccd444efc69a Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" Date: Thu, 17 Mar 2022 16:23:48 -0300 Subject: [PATCH 1/2] fix: Don't allow duplicated tag values in the Select --- .../src/components/Select/Select.tsx | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/superset-frontend/src/components/Select/Select.tsx b/superset-frontend/src/components/Select/Select.tsx index 92e0ec8b3379d..f1c78ab09892e 100644 --- a/superset-frontend/src/components/Select/Select.tsx +++ b/superset-frontend/src/components/Select/Select.tsx @@ -385,31 +385,37 @@ 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, ) => { 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; + }); } 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 objectArray = valueAsArray(previousState); + // Tokenized values can contain duplicated values + if (!objectArray.some(object => object.value === selectedValue.label)) { + return [...objectArray, selectedValue as AntdLabeledValue]; + } + return previousState; + }); } setInputValue(''); }; From 05602ca3b9f2ff6440496cbe0bc41fe4910662b5 Mon Sep 17 00:00:00 2001 From: "Michael S. Molina" Date: Mon, 21 Mar 2022 16:31:13 -0300 Subject: [PATCH 2/2] Addresses comments and adds test --- .../src/components/Select/Select.test.tsx | 11 ++++++ .../src/components/Select/Select.tsx | 34 ++++++------------- 2 files changed, 21 insertions(+), 24 deletions(-) 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(