You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by en...@apache.org on 2025/03/07 16:15:31 UTC

(superset) branch enxdev/refactor/antd5/create-wrappers updated: refactor(Tree): add wrapper, tests, and story for Ant Design 5 Tree component

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

enzomartellucci pushed a commit to branch enxdev/refactor/antd5/create-wrappers
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/enxdev/refactor/antd5/create-wrappers by this push:
     new 961b79c5bf refactor(Tree): add wrapper, tests, and story for Ant Design 5 Tree component
961b79c5bf is described below

commit 961b79c5bfde62b5faedf0db957d905344c98607
Author: Enzo Martellucci <en...@gmail.com>
AuthorDate: Fri Mar 7 17:15:08 2025 +0100

    refactor(Tree): add wrapper, tests, and story for Ant Design 5 Tree component
---
 .../components/AutoComplete/AutoComplete.test.tsx  |  19 ++-
 .../src/components/Tree/Tree.stories.tsx           | 163 +++++++++++++++++++++
 .../src/components/Tree/Tree.test.tsx              |  45 ++++++
 superset-frontend/src/components/Tree/index.tsx    |   7 +
 superset-frontend/src/components/index.ts          |   1 -
 .../components/menu/BackgroundStyleDropdown.tsx    |   6 +-
 .../FiltersConfigForm/FilterScope/ScopingTree.tsx  |   2 +-
 7 files changed, 232 insertions(+), 11 deletions(-)

diff --git a/superset-frontend/src/components/AutoComplete/AutoComplete.test.tsx b/superset-frontend/src/components/AutoComplete/AutoComplete.test.tsx
index 700d39870d..eb782c8419 100644
--- a/superset-frontend/src/components/AutoComplete/AutoComplete.test.tsx
+++ b/superset-frontend/src/components/AutoComplete/AutoComplete.test.tsx
@@ -17,7 +17,12 @@
  * under the License.
  */
 import { useState } from 'react';
-import { render, screen, userEvent, waitFor } from 'spec/helpers/testing-library';
+import {
+  render,
+  screen,
+  userEvent,
+  waitFor,
+} from 'spec/helpers/testing-library';
 import { AutoComplete, Input } from 'antd';
 
 const searchResult = (query: string): Array<{ value: string; label: string }> =>
@@ -27,7 +32,9 @@ const searchResult = (query: string): Array<{ value: string; label: string }> =>
   }));
 
 const AutoCompleteTest = () => {
-  const [options, setOptions] = useState<{ value: string; label: string }[]>([]);
+  const [options, setOptions] = useState<{ value: string; label: string }[]>(
+    [],
+  );
 
   const handleSearch = (value: string) => {
     setOptions(value ? searchResult(value) : []);
@@ -43,7 +50,9 @@ const AutoCompleteTest = () => {
 describe('AutoComplete Component', () => {
   test('renders input field', () => {
     render(<AutoCompleteTest />);
-    expect(screen.getByPlaceholderText('Type to search...')).toBeInTheDocument();
+    expect(
+      screen.getByPlaceholderText('Type to search...'),
+    ).toBeInTheDocument();
   });
 
   test('shows options when user types', async () => {
@@ -63,7 +72,9 @@ describe('AutoComplete Component', () => {
     const input = screen.getByPlaceholderText('Type to search...');
     userEvent.type(input, 'test');
 
-    await waitFor(() => expect(screen.getByText('test result 0')).toBeInTheDocument());
+    await waitFor(() =>
+      expect(screen.getByText('test result 0')).toBeInTheDocument(),
+    );
 
     userEvent.clear(input);
 
diff --git a/superset-frontend/src/components/Tree/Tree.stories.tsx b/superset-frontend/src/components/Tree/Tree.stories.tsx
new file mode 100644
index 0000000000..49794cdb86
--- /dev/null
+++ b/superset-frontend/src/components/Tree/Tree.stories.tsx
@@ -0,0 +1,163 @@
+import { Meta, ArgTypes } from '@storybook/react';
+import Icons from 'src/components/Icons';
+import Tree, { TreeProps } from './index';
+import type { TreeDataNode } from './index';
+
+const meta: Meta<TreeProps> = {
+  title: 'Components/Tree',
+  component: Tree,
+  tags: ['autodocs'],
+};
+
+export default meta;
+
+const treeData: TreeDataNode[] = [
+  {
+    title: 'parent 1',
+    key: '0-0',
+    children: [
+      {
+        title: 'parent 1-0',
+        key: '0-0-0',
+        icon: <Icons.FileImageOutlined />,
+        children: [
+          {
+            title: 'leaf',
+            key: '0-0-0-0',
+          },
+          {
+            title: 'leaf',
+            key: '0-0-0-1',
+          },
+          {
+            title: 'leaf',
+            key: '0-0-0-2',
+          },
+        ],
+      },
+      {
+        title: 'parent 1-1',
+        key: '0-0-1',
+        icon: <Icons.FileImageOutlined />,
+        children: [
+          {
+            title: 'leaf',
+            key: '0-0-1-0',
+          },
+        ],
+      },
+      {
+        title: 'parent 1-2',
+        key: '0-0-2',
+        icon: <Icons.FileImageOutlined />,
+        children: [
+          {
+            title: 'leaf',
+            key: '0-0-2-0',
+          },
+          {
+            title: 'leaf',
+            key: '0-0-2-1',
+          },
+        ],
+      },
+    ],
+  },
+];
+const TreeArgsType: ArgTypes<TreeProps> = {
+  autoExpandParent: {
+    control: { type: 'boolean' },
+    description: 'Whether to automatically expand a parent treeNode',
+  },
+  defaultExpandAll: {
+    control: { type: 'boolean' },
+    description: 'Whether to expand all treeNodes by default',
+  },
+  multiple: {
+    control: { type: 'boolean' },
+    description: 'Allows selecting multiple treeNodes',
+  },
+  checkable: {
+    control: { type: 'boolean' },
+    desciption: 'Add a Checkbox before the treeNodes',
+  },
+  selectable: {
+    control: { type: 'boolean' },
+    descrtiption: 'Whether can be selected',
+  },
+  draggable: {
+    control: { type: 'boolean' },
+    description:
+      'Specifies whether this Tree or the node is draggable. Use icon: false to disable drag handler icon',
+  },
+  showLine: {
+    control: { type: 'boolean' },
+    description: 'Shows a connecting line',
+  },
+  blockNode: {
+    control: { type: 'boolean' },
+    description: 'Whether treeNode fill remaining horizontal space',
+  },
+  checkStrictly: {
+    control: { type: 'boolean' },
+    description:
+      'Check treeNode precisely; parent treeNode and children treeNodes are not associated',
+  },
+  defaultExpandParent: {
+    control: { type: 'boolean' },
+    description: 'If auto expand parent treeNodes when init',
+  },
+  showIcon: {
+    control: { type: 'boolean' },
+    description: 'Controls whether to display the icon node, no default style',
+  },
+  virtual: {
+    control: { type: 'boolean' },
+    description: 'Disable virtual scroll when set to false',
+  },
+  titleRender: {
+    control: { type: 'text' },
+    description: 'Customize the title of the treeNode',
+  },
+  switcherLoadingIcon: {
+    control: { type: 'text' },
+    description: 'Customize the loading icon',
+  },
+  switcherIcon: {
+    control: { type: 'boolean' },
+    description: 'Customize the switcher icon',
+  },
+  treeData: { control: { type: 'object' }, description: 'TreeNodes data' },
+  loadData: {
+    control: { type: 'object' },
+    description: 'Load data asynchronously',
+  },
+  defaultExpandedKeys: {
+    control: { type: 'object' },
+    description: 'Default expanded treeNodes',
+  },
+  defaultSelectedKeys: {
+    control: { type: 'object' },
+    description: 'Default selected treeNodes',
+  },
+};
+
+export const TreeStory = (args: TreeProps) => <Tree {...args} />;
+
+TreeStory.args = {
+  multiple: false,
+  checkable: false,
+  selectable: true,
+  draggable: false,
+  showLine: false,
+  blockNode: false,
+  defaultExpandAll: false,
+  checkStrictly: false,
+  defaultExpandParent: false,
+  showIcon: false,
+  autoExpandParent: true,
+  defaultExpandedKeys: ['0-0-0', '0-0-1'],
+  defaultSelectedKeys: ['0-0-1'],
+  treeData,
+};
+TreeStory.argTypes = TreeArgsType;
diff --git a/superset-frontend/src/components/Tree/Tree.test.tsx b/superset-frontend/src/components/Tree/Tree.test.tsx
new file mode 100644
index 0000000000..64bd311e63
--- /dev/null
+++ b/superset-frontend/src/components/Tree/Tree.test.tsx
@@ -0,0 +1,45 @@
+import { render, screen, fireEvent } from '@testing-library/react';
+import Tree from './index';
+
+const treeData = [
+  {
+    title: 'Parent 1',
+    key: '0-0',
+    children: [
+      { title: 'Child 1', key: '0-0-0' },
+      { title: 'Child 2', key: '0-0-1' },
+    ],
+  },
+];
+
+describe('Tree Component', () => {
+  test('renders tree component correctly', () => {
+    render(<Tree treeData={treeData} />);
+    expect(screen.getByText('Parent 1')).toBeInTheDocument();
+    expect(screen.getByText('Child 1')).toBeInTheDocument();
+    expect(screen.getByText('Child 2')).toBeInTheDocument();
+  });
+
+  test('expands and collapses nodes on click', () => {
+    render(<Tree treeData={treeData} defaultExpandAll={false} />);
+
+    const parentNode = screen.getByText('Parent 1');
+    expect(screen.queryByText('Child 1')).not.toBeInTheDocument();
+
+    fireEvent.click(parentNode);
+    expect(screen.getByText('Child 1')).toBeInTheDocument();
+
+    fireEvent.click(parentNode);
+    expect(screen.queryByText('Child 1')).not.toBeInTheDocument();
+  });
+
+  test('selects a node when clicked', () => {
+    const onSelect = jest.fn();
+    render(<Tree treeData={treeData} onSelect={onSelect} />);
+
+    const childNode = screen.getByText('Child 1');
+    fireEvent.click(childNode);
+
+    expect(onSelect).toHaveBeenCalled();
+  });
+});
diff --git a/superset-frontend/src/components/Tree/index.tsx b/superset-frontend/src/components/Tree/index.tsx
new file mode 100644
index 0000000000..2f2bb65cc8
--- /dev/null
+++ b/superset-frontend/src/components/Tree/index.tsx
@@ -0,0 +1,7 @@
+import Tree from 'antd-v5/es/tree/Tree';
+import type { TreeProps, DataNode as TreeDataNode } from 'antd-v5/es/tree';
+import DirectoryTree from 'antd-v5/es/tree/DirectoryTree';
+
+export type { TreeProps };
+export { TreeDataNode, DirectoryTree };
+export default Tree;
diff --git a/superset-frontend/src/components/index.ts b/superset-frontend/src/components/index.ts
index c7b42ecea8..e2f5997a16 100644
--- a/superset-frontend/src/components/index.ts
+++ b/superset-frontend/src/components/index.ts
@@ -35,7 +35,6 @@ export {
   Row,
   Skeleton,
   Tag,
-  Tree,
   TreeSelect,
   Typography,
 } from 'antd-v5';
diff --git a/superset-frontend/src/dashboard/components/menu/BackgroundStyleDropdown.tsx b/superset-frontend/src/dashboard/components/menu/BackgroundStyleDropdown.tsx
index ccf11bc854..eb44ee81c0 100644
--- a/superset-frontend/src/dashboard/components/menu/BackgroundStyleDropdown.tsx
+++ b/superset-frontend/src/dashboard/components/menu/BackgroundStyleDropdown.tsx
@@ -62,11 +62,7 @@ const BackgroundStyleOption = styled.div`
           ${theme.colors.text.label} 25%,
           transparent 25%
         ),
-        linear-gradient(
-          -45deg,
-          ${theme.colors.text.label} 25%,
-          transparent 25%
-        ),
+        linear-gradient(-45deg, ${theme.colors.text.label} 25%, transparent 25%),
         linear-gradient(45deg, transparent 75%, ${theme.colors.text.label} 75%),
         linear-gradient(-45deg, transparent 75%, ${theme.colors.text.label} 75%);
       background-size: ${theme.gridUnit * 2}px ${theme.gridUnit * 2}px;
diff --git a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/ScopingTree.tsx b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/ScopingTree.tsx
index c6f34af48c..624ed851cb 100644
--- a/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/ScopingTree.tsx
+++ b/superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FilterScope/ScopingTree.tsx
@@ -19,7 +19,7 @@
 
 import { FC, useMemo, useState, memo } from 'react';
 import { NativeFilterScope } from '@superset-ui/core';
-import { Tree } from 'src/components';
+import Tree from 'src/components/Tree';
 import { DASHBOARD_ROOT_ID } from 'src/dashboard/util/constants';
 import { Tooltip } from 'src/components/Tooltip';
 import Icons from 'src/components/Icons';