diff --git a/components/tree/DirectoryTree.tsx b/components/tree/DirectoryTree.tsx index 85ad8b7233..7355e3328a 100644 --- a/components/tree/DirectoryTree.tsx +++ b/components/tree/DirectoryTree.tsx @@ -65,7 +65,7 @@ const DirectoryTree: React.ForwardRefRenderFunction keyEntities, ); } else { - initExpandedKeys = (props.expandedKeys || defaultExpandedKeys)!; + initExpandedKeys = props.expandedKeys || defaultExpandedKeys || []; } return initExpandedKeys; }; diff --git a/components/tree/__tests__/directory.test.tsx b/components/tree/__tests__/directory.test.tsx index e1503b9350..3e6179b82e 100644 --- a/components/tree/__tests__/directory.test.tsx +++ b/components/tree/__tests__/directory.test.tsx @@ -123,6 +123,30 @@ describe('Directory Tree', () => { expect(asFragment().firstChild).toMatchSnapshot(); }); + it('select multi nodes when shift key down', () => { + const treeData = [ + { title: 'leaf 0-0', key: '0-0-0', isLeaf: true }, + { title: 'leaf 0-1', key: '0-0-1', isLeaf: true }, + { title: 'leaf 1-0', key: '0-1-0', isLeaf: true }, + { title: 'leaf 1-1', key: '0-1-1', isLeaf: true }, + ]; + const { container } = render( + , + ); + expect(container.querySelectorAll('.ant-tree-node-content-wrapper').length).toBe(4); + expect(container.querySelectorAll('.ant-tree-node-selected').length).toBe(0); + const leaf0 = container.querySelectorAll('.ant-tree-node-content-wrapper')[0]; + const leaf1 = container.querySelectorAll('.ant-tree-node-content-wrapper')[1]; + const leaf2 = container.querySelectorAll('.ant-tree-node-content-wrapper')[2]; + const leaf3 = container.querySelectorAll('.ant-tree-node-content-wrapper')[3]; + fireEvent.click(leaf2); + fireEvent.click(leaf0, { shiftKey: true }); + expect(leaf0).toHaveClass('ant-tree-node-selected'); + expect(leaf1).toHaveClass('ant-tree-node-selected'); + expect(leaf2).toHaveClass('ant-tree-node-selected'); + expect(leaf3).not.toHaveClass('ant-tree-node-selected'); + }); + it('DirectoryTree should expend all when use treeData and defaultExpandAll is true', () => { const treeData = [ {