Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix DirectoryTree miss ref.scrollTo function #26129

Merged
merged 2 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions components/tree/DirectoryTree.tsx
@@ -1,5 +1,6 @@
import * as React from 'react';
import classNames from 'classnames';
import RcTree from 'rc-tree';
import debounce from 'lodash/debounce';
import { conductExpandParent } from 'rc-tree/lib/util';
import { EventDataNode, DataNode, Key } from 'rc-tree/lib/interface';
Expand Down Expand Up @@ -35,18 +36,18 @@ function getTreeData({ treeData, children }: DirectoryTreeProps) {
return treeData || convertTreeToData(children);
}

const DirectoryTree: React.FC<DirectoryTreeProps> = ({
defaultExpandAll,
defaultExpandParent,
defaultExpandedKeys,
...props
}) => {
const DirectoryTree: React.ForwardRefRenderFunction<RcTree, DirectoryTreeProps> = (
{ defaultExpandAll, defaultExpandParent, defaultExpandedKeys, ...props },
ref,
) => {
// Shift click usage
const lastSelectedKey = React.useRef<Key>();

const cachedSelectedKeys = React.useRef<Key[]>();

const ref = React.createRef<any>();
const treeRef = React.createRef<RcTree>();

React.useImperativeHandle(ref, () => treeRef.current!);

const getInitExpandedKeys = () => {
const { keyEntities } = convertDataToEntities(getTreeData(props));
Expand Down Expand Up @@ -93,7 +94,7 @@ const DirectoryTree: React.FC<DirectoryTreeProps> = ({

// Call internal rc-tree expand function
// https://github.com/ant-design/ant-design/issues/12567
ref.current.onNodeExpand(event, node);
treeRef.current!.onNodeExpand(event as any, node);
};

const onDebounceExpand = debounce(expandFolderNode, 200, {
Expand Down Expand Up @@ -220,7 +221,7 @@ const DirectoryTree: React.FC<DirectoryTreeProps> = ({
return (
<Tree
icon={getIcon}
ref={ref}
ref={treeRef}
blockNode
{...otherProps}
prefixCls={prefixCls}
Expand All @@ -235,9 +236,12 @@ const DirectoryTree: React.FC<DirectoryTreeProps> = ({
);
};

DirectoryTree.defaultProps = {
const ForwardDirectoryTree = React.forwardRef(DirectoryTree);
ForwardDirectoryTree.displayName = 'DirectoryTree';

ForwardDirectoryTree.defaultProps = {
showIcon: true,
expandAction: 'click' as DirectoryTreeProps['expandAction'],
};

export default DirectoryTree;
export default ForwardDirectoryTree;
7 changes: 7 additions & 0 deletions components/tree/__tests__/directory.test.js
Expand Up @@ -250,4 +250,11 @@ describe('Directory Tree', () => {
expect.objectContaining({ event: 'select', nativeEvent: expect.anything() }),
);
});

it('ref support', () => {
const treeRef = React.createRef();
mount(createTree({ ref: treeRef }));

expect('scrollTo' in treeRef.current).toBeTruthy();
});
});