You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/03/21 19:33:15 UTC

[GitHub] [superset] michael-s-molina commented on a change in pull request #19283: fix: Don't allow duplicated tag values in the Select

michael-s-molina commented on a change in pull request #19283:
URL: https://github.com/apache/superset/pull/19283#discussion_r831474519



##########
File path: superset-frontend/src/components/Select/Select.tsx
##########
@@ -385,31 +385,37 @@ const Select = (
 
   const hasCustomLabels = fullSelectOptions.some(opt => !!opt?.customLabel);
 
+  const valueAsArray = <T,>(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<string | number>(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<AntdLabeledValue>(previousState);
+        // Tokenized values can contain duplicated values
+        if (!objectArray.some(object => object.value === selectedValue.label)) {

Review comment:
       :see_no_evil: Lack of coffee




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org