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



##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterBar.test.tsx
##########
@@ -219,11 +221,16 @@ describe('FilterBar', () => {
   });
 
   const renderWrapper = (props = closedBarProps, state?: object) =>
-    render(<FilterBar {...props} width={280} height={400} offset={0} />, {
-      useRedux: true,
-      initialState: state,
-      useRouter: true,
-    });
+    render(
+      <DndProvider backend={HTML5Backend}>
+        <FilterBar {...props} width={280} height={400} offset={0} />
+      </DndProvider>,
+      {
+        useRedux: true,
+        initialState: state,
+        useRouter: true,
+      },
+    );

Review comment:
       ```suggestion
       render(<FilterBar {...props} width={280} height={400} offset={0} />, {
         useRedux: true,
         initialState: state,
         useRouter: true,
         useDnd: true,
       });
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigModal.test.tsx
##########
@@ -30,6 +30,8 @@ import {
 import { render, screen, waitFor } from 'spec/helpers/testing-library';
 import mockDatasource, { id, datasourceId } from 
'spec/fixtures/mockDatasource';
 import chartQueries from 'spec/fixtures/mockChartQueries';
+import { DndProvider } from 'react-dnd';
+import { HTML5Backend } from 'react-dnd-html5-backend';

Review comment:
       ```suggestion
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigModal.test.tsx
##########
@@ -149,10 +151,15 @@ beforeAll(() => {
 });
 
 function defaultRender(initialState = defaultState()) {
-  return render(<FiltersConfigModal {...props} />, {
-    useRedux: true,
-    initialState,
-  });
+  return render(
+    <DndProvider backend={HTML5Backend}>
+      <FiltersConfigModal {...props} />
+    </DndProvider>,
+    {
+      useRedux: true,
+      initialState,
+    },
+  );

Review comment:
       ```suggestion
     return render(<FiltersConfigModal {...props} />, {
       useRedux: true,
       useDnd: true,
       initialState,
     });
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterTabTitle.tsx
##########
@@ -0,0 +1,196 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { styled, t } from '@superset-ui/core';
+import React, { useRef } from 'react';
+import { DropTargetMonitor, useDrag, useDrop, XYCoord } from 'react-dnd';
+import Icons from 'src/components/Icons';
+import { REMOVAL_DELAY_SECS } from './utils';
+
+const FILTER_TYPE = 'FILTER';
+const StyledFilterTitle = styled.span`
+  white-space: normal;
+  color: ${({ theme }) => theme.colors.grayscale.dark1};
+`;
+const TabTitleContainer = styled.div`
+  display: flex;
+  width: 100%;
+  border-radius: ${({ theme }) => theme.gridUnit}px;
+`;
+const StyledFilterTabTitle = styled.span`
+  transition: color ${({ theme }) => theme.transitionTiming}s;
+  width: 100%;
+  display: flex;
+  flex-direction: row;
+  justify-content: space-between;
+
+  @keyframes tabTitleRemovalAnimation {
+    0%,
+    90% {
+      opacity: 1;
+    }
+    95%,
+    100% {
+      opacity: 0;
+    }
+  }
+
+  &.removed {
+    color: ${({ theme }) => theme.colors.warning.dark1};
+    transform-origin: top;
+    animation-name: tabTitleRemovalAnimation;
+    animation-duration: ${REMOVAL_DELAY_SECS}s;
+  }
+`;
+const StyledSpan = styled.span`
+  cursor: pointer;
+  color: ${({ theme }) => theme.colors.primary.dark1};
+  &:hover {
+    color: ${({ theme }) => theme.colors.primary.dark2};
+  }
+`;
+
+const StyledTrashIcon = styled(Icons.Trash)`
+  color: ${({ theme }) => theme.colors.grayscale.light3};
+  &:hover {
+    color: ${({ theme }) => theme.colors.primary.dark2};
+  }
+`;
+
+interface FilterTabTitleProps {
+  id: string;
+  isRemoved: boolean;
+  index: number;
+  getFilterTitle: (id: string) => string;
+  restoreFilter: (id: string) => void;
+  onRearrage: (itemId: string, targetIndex: number) => void;
+  onRemove: (id: string) => void;
+}
+
+interface DragItem {
+  index: number;
+  id: string;
+  type: string;
+}
+
+export const FilterTabTitle: React.FC<FilterTabTitleProps> = ({
+  id,
+  index,
+  isRemoved,
+  getFilterTitle,
+  restoreFilter,
+  onRearrage,
+  onRemove,
+}) => {
+  const ref = useRef<HTMLDivElement>(null);
+  const [{ isDragging }, drag] = useDrag({
+    item: { id, type: FILTER_TYPE, index },
+    collect: (monitor: any) => ({
+      isDragging: monitor.isDragging(),
+    }),
+  });
+  const [, drop] = useDrop({
+    accept: FILTER_TYPE,
+    hover: (item: DragItem, monitor: DropTargetMonitor) => {
+      if (!ref.current) {
+        return;
+      }
+
+      const dragIndex = item.index;
+      const hoverIndex = index;
+
+      // Don't replace items with themselves
+      if (dragIndex === hoverIndex) {
+        return;
+      }
+      // Determine rectangle on screen
+      const hoverBoundingRect = ref.current?.getBoundingClientRect();
+
+      // Get vertical middle
+      const hoverMiddleY =
+        (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
+
+      // Determine mouse position
+      const clientOffset = monitor.getClientOffset();
+
+      // Get pixels to the top
+      const hoverClientY = (clientOffset as XYCoord).y - hoverBoundingRect.top;
+
+      // Only perform the move when the mouse has crossed half of the items 
height
+      // When dragging downwards, only move when the cursor is below 50%
+      // When dragging upwards, only move when the cursor is above 50%
+
+      // Dragging downwards
+      if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
+        return;
+      }
+
+      // Dragging upwards
+      if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
+        return;
+      }
+
+      onRearrage(item.id, index);
+      // Note: we're mutating the monitor item here.
+      // Generally it's better to avoid mutations,
+      // but it's good here for the sake of performance
+      // to avoid expensive index searches.
+      // eslint-disable-next-line no-param-reassign
+      item.index = hoverIndex;
+    },
+  });
+  drag(drop(ref));
+  return (
+    <TabTitleContainer
+      ref={ref}
+      style={{

Review comment:
       It would be nice to pass `isDragging` to `TabTitleContainer` to keep all 
the styles in one place.

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterBar.test.tsx
##########
@@ -29,6 +29,8 @@ import { TimeFilterPlugin, SelectFilterPlugin } from 
'src/filters/components';
 import { DATE_FILTER_CONTROL_TEST_ID } from 
'src/explore/components/controls/DateFilterControl/DateFilterLabel';
 import fetchMock from 'fetch-mock';
 import { waitFor } from '@testing-library/react';
+import { DndProvider } from 'react-dnd';
+import { HTML5Backend } from 'react-dnd-html5-backend';

Review comment:
       ```suggestion
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterTabTitle.tsx
##########
@@ -0,0 +1,196 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { styled, t } from '@superset-ui/core';
+import React, { useRef } from 'react';
+import { DropTargetMonitor, useDrag, useDrop, XYCoord } from 'react-dnd';
+import Icons from 'src/components/Icons';
+import { REMOVAL_DELAY_SECS } from './utils';
+
+const FILTER_TYPE = 'FILTER';
+const StyledFilterTitle = styled.span`
+  white-space: normal;
+  color: ${({ theme }) => theme.colors.grayscale.dark1};
+`;
+const TabTitleContainer = styled.div`
+  display: flex;
+  width: 100%;
+  border-radius: ${({ theme }) => theme.gridUnit}px;
+`;
+const StyledFilterTabTitle = styled.span`
+  transition: color ${({ theme }) => theme.transitionTiming}s;
+  width: 100%;
+  display: flex;
+  flex-direction: row;
+  justify-content: space-between;
+
+  @keyframes tabTitleRemovalAnimation {
+    0%,
+    90% {
+      opacity: 1;
+    }
+    95%,
+    100% {
+      opacity: 0;
+    }
+  }
+
+  &.removed {
+    color: ${({ theme }) => theme.colors.warning.dark1};
+    transform-origin: top;
+    animation-name: tabTitleRemovalAnimation;
+    animation-duration: ${REMOVAL_DELAY_SECS}s;
+  }
+`;
+const StyledSpan = styled.span`
+  cursor: pointer;
+  color: ${({ theme }) => theme.colors.primary.dark1};
+  &:hover {
+    color: ${({ theme }) => theme.colors.primary.dark2};
+  }
+`;
+
+const StyledTrashIcon = styled(Icons.Trash)`
+  color: ${({ theme }) => theme.colors.grayscale.light3};
+  &:hover {
+    color: ${({ theme }) => theme.colors.primary.dark2};
+  }
+`;
+
+interface FilterTabTitleProps {
+  id: string;
+  isRemoved: boolean;
+  index: number;
+  getFilterTitle: (id: string) => string;
+  restoreFilter: (id: string) => void;
+  onRearrage: (itemId: string, targetIndex: number) => void;
+  onRemove: (id: string) => void;
+}
+
+interface DragItem {
+  index: number;
+  id: string;
+  type: string;
+}
+
+export const FilterTabTitle: React.FC<FilterTabTitleProps> = ({
+  id,
+  index,
+  isRemoved,
+  getFilterTitle,
+  restoreFilter,
+  onRearrage,
+  onRemove,
+}) => {
+  const ref = useRef<HTMLDivElement>(null);
+  const [{ isDragging }, drag] = useDrag({
+    item: { id, type: FILTER_TYPE, index },
+    collect: (monitor: any) => ({
+      isDragging: monitor.isDragging(),
+    }),
+  });
+  const [, drop] = useDrop({
+    accept: FILTER_TYPE,
+    hover: (item: DragItem, monitor: DropTargetMonitor) => {
+      if (!ref.current) {
+        return;
+      }
+
+      const dragIndex = item.index;
+      const hoverIndex = index;
+
+      // Don't replace items with themselves
+      if (dragIndex === hoverIndex) {
+        return;
+      }
+      // Determine rectangle on screen
+      const hoverBoundingRect = ref.current?.getBoundingClientRect();
+
+      // Get vertical middle
+      const hoverMiddleY =
+        (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
+
+      // Determine mouse position
+      const clientOffset = monitor.getClientOffset();
+
+      // Get pixels to the top
+      const hoverClientY = (clientOffset as XYCoord).y - hoverBoundingRect.top;
+
+      // Only perform the move when the mouse has crossed half of the items 
height
+      // When dragging downwards, only move when the cursor is below 50%
+      // When dragging upwards, only move when the cursor is above 50%
+
+      // Dragging downwards
+      if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
+        return;
+      }
+
+      // Dragging upwards
+      if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
+        return;
+      }
+
+      onRearrage(item.id, index);
+      // Note: we're mutating the monitor item here.
+      // Generally it's better to avoid mutations,
+      // but it's good here for the sake of performance
+      // to avoid expensive index searches.
+      // eslint-disable-next-line no-param-reassign
+      item.index = hoverIndex;
+    },
+  });
+  drag(drop(ref));
+  return (
+    <TabTitleContainer
+      ref={ref}
+      style={{
+        opacity: isDragging ? 0.5 : 1,
+      }}
+    >
+      <StyledFilterTabTitle className={isRemoved ? 'removed' : ''}>
+        <StyledFilterTitle>
+          {isRemoved ? t('(Removed)') : getFilterTitle(id)}
+        </StyledFilterTitle>
+        {isRemoved && (
+          <StyledSpan
+            role="button"
+            data-test="undo-button"
+            tabIndex={0}
+            onClick={() => restoreFilter(id)}
+          >
+            {t('Undo?')}
+          </StyledSpan>
+        )}
+      </StyledFilterTabTitle>
+      <div style={{ alignSelf: 'flex-end' }}>

Review comment:
       Avoid using `style`. Currently, we are using Emotion `styled` or `css` 
depending on the situation. You can check this 
[guide](https://github.com/apache/superset/issues/15264) for more information.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to