From 60944db9895cf248ce885a40b5e9cb8ed71cbe50 Mon Sep 17 00:00:00 2001 From: KotoriK <52659125+KotoriK@users.noreply.github.com> Date: Mon, 7 Nov 2022 16:06:17 +0800 Subject: [PATCH] refactor(Tree): solve circular import --- components/tree/Tree.tsx | 24 ++++++------------------ components/tree/index.tsx | 26 +++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/components/tree/Tree.tsx b/components/tree/Tree.tsx index b90ecf9b8840..1f9c1d20a1f3 100644 --- a/components/tree/Tree.tsx +++ b/components/tree/Tree.tsx @@ -1,12 +1,11 @@ import HolderOutlined from '@ant-design/icons/HolderOutlined'; import classNames from 'classnames'; import type { BasicDataNode, TreeProps as RcTreeProps } from 'rc-tree'; -import RcTree, { TreeNode } from 'rc-tree'; +import RcTree from 'rc-tree'; import type { DataNode, Key } from 'rc-tree/lib/interface'; import * as React from 'react'; import { ConfigContext } from '../config-provider'; import collapseMotion from '../_util/motion'; -import DirectoryTree from './DirectoryTree'; import dropIndicatorRender from './utils/dropIndicator'; import renderSwitcherIcon from './utils/iconUtil'; @@ -52,7 +51,7 @@ export interface AntTreeNodeProps { [customProp: string]: any; } -export interface AntTreeNode extends React.Component {} +export interface AntTreeNode extends React.Component { } export interface AntTreeNodeBaseEvent { node: AntTreeNode; @@ -145,22 +144,15 @@ export interface TreeProps style?: React.CSSProperties; showIcon?: boolean; icon?: - | ((nodeProps: AntdTreeNodeAttribute) => React.ReactNode) - | React.ReactNode - | RcTreeProps['icon']; + | ((nodeProps: AntdTreeNodeAttribute) => React.ReactNode) + | React.ReactNode + | RcTreeProps['icon']; switcherIcon?: SwitcherIcon | RcTreeProps['switcherIcon']; prefixCls?: string; children?: React.ReactNode; blockNode?: boolean; } -type CompoundedComponent = (( - props: React.PropsWithChildren> & { ref?: React.Ref }, -) => React.ReactElement) & { - TreeNode: typeof TreeNode; - DirectoryTree: typeof DirectoryTree; -}; - const Tree = React.forwardRef((props, ref) => { const { getPrefixCls, direction, virtual } = React.useContext(ConfigContext); const { @@ -242,10 +234,6 @@ const Tree = React.forwardRef((props, ref) => { {children} ); -}) as unknown as CompoundedComponent; - -Tree.TreeNode = TreeNode; - -Tree.DirectoryTree = DirectoryTree; +}); export default Tree; diff --git a/components/tree/index.tsx b/components/tree/index.tsx index e82216841283..e1c161372115 100644 --- a/components/tree/index.tsx +++ b/components/tree/index.tsx @@ -1,6 +1,14 @@ -import Tree from './Tree'; +import type RcTree from 'rc-tree'; +import { TreeNode } from 'rc-tree'; +import type { BasicDataNode } from 'rc-tree'; +import type { DataNode } from 'rc-tree/lib/interface'; -export { DataNode, EventDataNode } from 'rc-tree/lib/interface'; +import type { TreeProps } from './Tree'; +import TreePure from './Tree'; +import DirectoryTree from './DirectoryTree' + +export { DataNode } +export { EventDataNode } from 'rc-tree/lib/interface'; export { DirectoryTreeProps, ExpandAction as DirectoryTreeExpandAction } from './DirectoryTree'; export { AntdTreeNodeAttribute, @@ -13,4 +21,16 @@ export { TreeProps, } from './Tree'; -export default Tree; + +type CompoundedComponent = (( + props: React.PropsWithChildren> & { ref?: React.Ref }, +) => React.ReactElement) & { + TreeNode: typeof TreeNode; + DirectoryTree: typeof DirectoryTree; +}; + +const Tree = TreePure as unknown as CompoundedComponent +Tree.DirectoryTree = DirectoryTree +Tree.TreeNode = TreeNode + +export default Tree; \ No newline at end of file