This is an automated email from the ASF dual-hosted git repository.

diegopucci pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new c8db435  fix(Explore): Clear filter value when changing columns 
(#16871)
c8db435 is described below

commit c8db435730d533a2c0dc1ef164b4a6905efd1dcd
Author: Geido <[email protected]>
AuthorDate: Thu Sep 30 17:18:08 2021 +0300

    fix(Explore): Clear filter value when changing columns (#16871)
    
    * Clear filter value onChange
    
    * Clear filter value on user change action
---
 .../index.tsx                                      | 75 ++++++++++++----------
 1 file changed, 42 insertions(+), 33 deletions(-)

diff --git 
a/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx
 
b/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx
index fd97963..64247b9 100644
--- 
a/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx
+++ 
b/superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterEditPopoverSimpleTabContent/index.tsx
@@ -211,41 +211,12 @@ const AdhocFilterEditPopoverSimpleTabContent: 
React.FC<Props> = props => {
     onComparatorChange,
   } = useSimpleTabFilterProps(props);
   const [suggestions, setSuggestions] = useState<Record<string, any>>([]);
+  const [comparator, setComparator] = useState(props.adhocFilter.comparator);
   const [
     loadingComparatorSuggestions,
     setLoadingComparatorSuggestions,
   ] = useState(false);
 
-  useEffect(() => {
-    const refreshComparatorSuggestions = () => {
-      const { datasource } = props;
-      const col = props.adhocFilter.subject;
-      const having = props.adhocFilter.clause === CLAUSES.HAVING;
-
-      if (col && datasource && datasource.filter_select && !having) {
-        const controller = new AbortController();
-        const { signal } = controller;
-        if (loadingComparatorSuggestions) {
-          controller.abort();
-        }
-        setLoadingComparatorSuggestions(true);
-        SupersetClient.get({
-          signal,
-          endpoint: 
`/superset/filter/${datasource.type}/${datasource.id}/${col}/`,
-        })
-          .then(({ json }) => {
-            setSuggestions(json);
-            setLoadingComparatorSuggestions(false);
-          })
-          .catch(() => {
-            setSuggestions([]);
-            setLoadingComparatorSuggestions(false);
-          });
-      }
-    };
-    refreshComparatorSuggestions();
-  }, [props.adhocFilter.subject]);
-
   const onInputComparatorChange = (
     event: React.ChangeEvent<HTMLInputElement>,
   ) => {
@@ -257,7 +228,6 @@ const AdhocFilterEditPopoverSimpleTabContent: 
React.FC<Props> = props => {
   );
 
   const getOptionsRemaining = () => {
-    const { comparator } = props.adhocFilter;
     // if select is multi/value is array, we show the options not selected
     const valuesFromSuggestionsLength = Array.isArray(comparator)
       ? comparator.filter(v => suggestions.includes(v)).length
@@ -270,13 +240,18 @@ const AdhocFilterEditPopoverSimpleTabContent: 
React.FC<Props> = props => {
     return optionsRemaining ? placeholder : '';
   };
 
+  const handleSubjectChange = (subject: string) => {
+    setComparator(undefined);
+    onSubjectChange(subject);
+  };
+
   let columns = props.options;
-  const { subject, operator, comparator, operatorId } = props.adhocFilter;
+  const { subject, operator, operatorId } = props.adhocFilter;
 
   const subjectSelectProps = {
     ariaLabel: t('Select subject'),
     value: subject ?? undefined,
-    onChange: onSubjectChange,
+    onChange: handleSubjectChange,
     notFoundContent: t(
       'No such column found. To filter on a metric, try the Custom SQL tab.',
     ),
@@ -345,6 +320,40 @@ const AdhocFilterEditPopoverSimpleTabContent: 
React.FC<Props> = props => {
     }
   `;
 
+  useEffect(() => {
+    const refreshComparatorSuggestions = () => {
+      const { datasource } = props;
+      const col = props.adhocFilter.subject;
+      const having = props.adhocFilter.clause === CLAUSES.HAVING;
+
+      if (col && datasource && datasource.filter_select && !having) {
+        const controller = new AbortController();
+        const { signal } = controller;
+        if (loadingComparatorSuggestions) {
+          controller.abort();
+        }
+        setLoadingComparatorSuggestions(true);
+        SupersetClient.get({
+          signal,
+          endpoint: 
`/superset/filter/${datasource.type}/${datasource.id}/${col}/`,
+        })
+          .then(({ json }) => {
+            setSuggestions(json);
+            setLoadingComparatorSuggestions(false);
+          })
+          .catch(() => {
+            setSuggestions([]);
+            setLoadingComparatorSuggestions(false);
+          });
+      }
+    };
+    refreshComparatorSuggestions();
+  }, [props.adhocFilter.subject]);
+
+  useEffect(() => {
+    setComparator(props.adhocFilter.comparator);
+  }, [props.adhocFilter.comparator]);
+
   return (
     <>
       <Select

Reply via email to