refactor: Tree support generic (#32992)

* chore: bump rc-tree

* test: add test case

* test: Add ts check
This commit is contained in:
二货机器人 2021-11-23 23:05:49 +08:00 committed by GitHub
parent b926c535ea
commit 3886ad6462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 10 deletions

View File

@ -1,6 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import HolderOutlined from '@ant-design/icons/HolderOutlined'; import HolderOutlined from '@ant-design/icons/HolderOutlined';
import RcTree, { TreeNode, TreeProps as RcTreeProps } from 'rc-tree'; import RcTree, { TreeNode, TreeProps as RcTreeProps, BasicDataNode } from 'rc-tree';
import classNames from 'classnames'; import classNames from 'classnames';
import { DataNode, Key } from 'rc-tree/lib/interface'; import { DataNode, Key } from 'rc-tree/lib/interface';
import DirectoryTree from './DirectoryTree'; import DirectoryTree from './DirectoryTree';
@ -98,8 +98,8 @@ interface DraggableConfig {
nodeDraggable?: DraggableFn; nodeDraggable?: DraggableFn;
} }
export interface TreeProps export interface TreeProps<T extends BasicDataNode = DataNode>
extends Omit<RcTreeProps, 'prefixCls' | 'showLine' | 'direction' | 'draggable'> { extends Omit<RcTreeProps<T>, 'prefixCls' | 'showLine' | 'direction' | 'draggable'> {
showLine?: boolean | { showLeafIcon: boolean }; showLine?: boolean | { showLeafIcon: boolean };
className?: string; className?: string;
/** 是否支持多选 */ /** 是否支持多选 */
@ -143,11 +143,13 @@ export interface TreeProps
blockNode?: boolean; blockNode?: boolean;
} }
interface CompoundedComponent type CompoundedComponent = (<T extends BasicDataNode | DataNode = DataNode>(
extends React.ForwardRefExoticComponent<TreeProps & React.RefAttributes<RcTree>> { props: React.PropsWithChildren<TreeProps<T>> & { ref?: React.Ref<RcTree> },
) => React.ReactElement) & {
defaultProps: Partial<React.PropsWithChildren<TreeProps<any>>>;
TreeNode: typeof TreeNode; TreeNode: typeof TreeNode;
DirectoryTree: typeof DirectoryTree; DirectoryTree: typeof DirectoryTree;
} };
const Tree = React.forwardRef<RcTree, TreeProps>((props, ref) => { const Tree = React.forwardRef<RcTree, TreeProps>((props, ref) => {
const { getPrefixCls, direction, virtual } = React.useContext(ConfigContext); const { getPrefixCls, direction, virtual } = React.useContext(ConfigContext);
@ -223,7 +225,7 @@ const Tree = React.forwardRef<RcTree, TreeProps>((props, ref) => {
{children} {children}
</RcTree> </RcTree>
); );
}) as CompoundedComponent; }) as unknown as CompoundedComponent;
Tree.TreeNode = TreeNode; Tree.TreeNode = TreeNode;

View File

@ -0,0 +1,51 @@
import * as React from 'react';
import { mount } from 'enzyme';
import type { BasicDataNode } from 'rc-tree';
import Tree from '../index';
describe('Tree.TypeScript', () => {
it('without generic', () => {
const wrapper = mount(
<Tree
treeData={[
{
title: 'Bamboo',
key: 'bamboo',
children: [
{
title: 'Little',
key: 'little',
},
],
},
]}
/>,
);
expect(wrapper).toBeTruthy();
});
it('support generic', () => {
interface MyDataNode extends BasicDataNode {
bamboo: string;
list?: MyDataNode[];
}
const wrapper = mount(
<Tree<MyDataNode>
treeData={[
{
bamboo: 'good',
list: [
{
bamboo: 'well',
},
],
},
]}
/>,
);
expect(wrapper).toBeTruthy();
});
});

View File

@ -120,7 +120,7 @@
"copy-to-clipboard": "^3.2.0", "copy-to-clipboard": "^3.2.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.25.3", "moment": "^2.25.3",
"rc-cascader": "~2.1.0", "rc-cascader": "~2.2.0",
"rc-checkbox": "~2.3.0", "rc-checkbox": "~2.3.0",
"rc-collapse": "~3.1.0", "rc-collapse": "~3.1.0",
"rc-dialog": "~8.6.0", "rc-dialog": "~8.6.0",
@ -146,8 +146,8 @@
"rc-tabs": "~11.10.0", "rc-tabs": "~11.10.0",
"rc-textarea": "~0.3.0", "rc-textarea": "~0.3.0",
"rc-tooltip": "~5.1.1", "rc-tooltip": "~5.1.1",
"rc-tree": "~5.2.0", "rc-tree": "~5.3.0",
"rc-tree-select": "~4.6.0", "rc-tree-select": "~4.7.0",
"rc-trigger": "^5.2.10", "rc-trigger": "^5.2.10",
"rc-upload": "~4.3.0", "rc-upload": "~4.3.0",
"rc-util": "^5.14.0", "rc-util": "^5.14.0",