Skip to content

Commit

Permalink
fix: tree hooks重构
Browse files Browse the repository at this point in the history
  • Loading branch information
wenxinping committed Dec 30, 2021
1 parent b655402 commit 6034a60
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 57 deletions.
12 changes: 6 additions & 6 deletions source/components/Tree/DirectoryTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import classNames from 'classnames';
import omit from 'omit.js';
import debounce from 'lodash/debounce';
import { polyfill } from 'react-lifecycles-compat';
import RcTree from '../TreeSelect/rcTree';
import { conductExpandParent, convertTreeToEntities } from '../TreeSelect/rcTree/util';

import Tree, {
Expand Down Expand Up @@ -43,7 +44,7 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta

state: DirectoryTreeState;

tree: Tree;
tree: React.RefObject<RcTree>;

onDebounceExpand: (event: React.MouseEvent<HTMLElement>, node: FishdTreeNode) => void;

Expand All @@ -65,7 +66,7 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta

constructor(props: DirectoryTreeProps) {
super(props);

this.tree = React.createRef<RcTree>();
const { defaultExpandAll, defaultExpandParent, expandedKeys, defaultExpandedKeys, children } =
props;
const { keyEntities } = convertTreeToEntities(children);
Expand Down Expand Up @@ -181,11 +182,10 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta
}

// Get internal rc-tree
const internalTree = this.tree.tree;

const internalTree = this.tree;
// Call internal rc-tree expand function
// https://github.com/ant-design/ant-design/issues/12567
internalTree.onNodeExpand(event, node);
internalTree.current.onNodeExpand(event, node);
};

setUncontrolledState = (state: DirectoryTreeState) => {
Expand All @@ -203,7 +203,7 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta
return (
<Tree
icon={getIcon}
ref={(node: Tree) => (this.tree = node)}
ref={this.tree}
{...props}
prefixCls={prefixCls}
className={connectClassName}
Expand Down
102 changes: 51 additions & 51 deletions source/components/Tree/Tree.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import RcTree, { TreeNode } from '../TreeSelect/rcTree';
import DirectoryTree from './DirectoryTree';
import DirectoryTree, {DirectoryTreeProps} from './DirectoryTree';
import classNames from 'classnames';
import animation from '../../utils/openAnimation';
import Icon from '../Icon';
Expand Down Expand Up @@ -113,6 +113,8 @@ export interface TreeProps {
expandedKeys?: string[];
/** (受控)选中复选框的树节点 */
checkedKeys?: string[] | { checked: string[]; halfChecked: string[] };
/** 默认选中所有子节点,子节点全部选中时,不会选中父节点 */
checkType?: 'countDown';
/** 默认选中复选框的树节点 */
defaultCheckedKeys?: string[];
/** (受控)设置选中的树节点 */
Expand Down Expand Up @@ -163,38 +165,12 @@ export interface TreeProps {
/* 单选模式下是否为必选 */
required?: boolean;
}
const InternalTree: React.ForwardRefRenderFunction<unknown, TreeProps> = (props, ref) => {
const { prefixCls, className, style, showIcon, required } = props;
let checkable = props.checkable;
const renderSwitcherIcon = ({ isLeaf, expanded, loading }: FishdTreeNodeProps) => {
const { prefixCls, showLine } = props;

export default class Tree extends React.Component<TreeProps, any> {
static TreeNode: React.ComponentClass<FishdTreeNodeProps> = TreeNode;

static DirectoryTree = DirectoryTree;

static defaultProps = {
autoExpandParent: true,
checkable: false,
defaultExpandAll: false,
defaultExpandParent: true,
required: false,
openAnimation: {
...animation,
appear: null,
},
prefixCls: 'fishd-tree',
showIcon: false,
};

tree: any;

renderSwitcherIcon = ({ isLeaf, expanded, loading }: FishdTreeNodeProps) => {
const { prefixCls, showLine } = this.props;
// if (loading) {
// return (
// <Icon
// type="load-line" spin={true}
// className={`${prefixCls}-switcher-loading-icon`}
// />
// );
// }
if (showLine) {
if (isLeaf) {
return <Icon type="file-line" className={`${prefixCls}-switcher-line-icon`} />;
Expand All @@ -212,23 +188,47 @@ export default class Tree extends React.Component<TreeProps, any> {
return <Icon type="down-fill" className={`${prefixCls}-switcher-icon`} />;
}
};

render() {
const props = this.props;
const { prefixCls, className, style, showIcon, required } = props;
let checkable = props.checkable;
return (
<RcTree
{...props}
ref={(node: Tree) => (this.tree = node)}
className={classNames(!showIcon && `${prefixCls}-icon-hide`, className)}
checkable={checkable ? <span className={`${prefixCls}-checkbox-inner`} /> : checkable}
switcherIcon={this.renderSwitcherIcon}
required={required}
style={style}
>
{this.props.children}
</RcTree>
);
}
return (
<RcTree
{...props}
openAnimation= {{
...animation,
appear: null,
}}
ref={ref}
className={classNames(!showIcon && `${prefixCls}-icon-hide`, className)}
checkable={checkable ? <span className={`${prefixCls}-checkbox-inner`} /> : checkable}
switcherIcon={renderSwitcherIcon}
required={required}
style={style}
>
{props.children}
</RcTree>
);
};
const TreeRef = React.forwardRef<unknown, TreeProps>(InternalTree);
type TreeRefInterface = typeof TreeRef;
interface TreeInterface extends TreeRefInterface {
DirectoryTree: typeof DirectoryTree;
TreeNode: React.ComponentClass<FishdTreeNodeProps>;
__FISHD_TREE: boolean;
}

const Tree = TreeRef as TreeInterface;
Tree.displayName = 'Button';

Tree.defaultProps = {
autoExpandParent: true,
checkable: false,
defaultExpandAll: false,
defaultExpandParent: true,
required: false,
prefixCls: 'fishd-tree',
showIcon: false,
};

Tree.DirectoryTree = DirectoryTree;
Tree.TreeNode = TreeNode;
Tree.__FISHD_TREE = true;

export default Tree;
1 change: 1 addition & 0 deletions source/components/TreeSelect/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface TreeSelectProps extends AbstractSelectProps {
treeCheckable?: boolean | React.ReactNode;
treeCheckStrictly?: boolean;
treeData?: Array<TreeData>;
treeCheckType?: 'countDown';
treeDataSimpleMode?: boolean | Object;
treeDefaultExpandAll?: boolean;
treeDefaultExpandedKeys?: Array<string>;
Expand Down

0 comments on commit 6034a60

Please sign in to comment.