close
Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/examples/basic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Demo extends React.Component {
className="myCls"
showLine
checkable
height={150}
defaultExpandAll
defaultExpandedKeys={this.state.defaultExpandedKeys}
onExpand={this.onExpand}
Expand Down
6 changes: 6 additions & 0 deletions src/NodeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ interface NodeListProps<TreeDataType extends BasicDataNode> {
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
onFocus?: React.FocusEventHandler<HTMLDivElement>;
onBlur?: React.FocusEventHandler<HTMLDivElement>;
onMouseDown?: React.MouseEventHandler<HTMLDivElement>;
onMouseUp?: React.MouseEventHandler<HTMLDivElement>;
onActiveChange: (key: Key) => void;

onListChangeStart: () => void;
Expand Down Expand Up @@ -144,6 +146,8 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
onKeyDown,
onFocus,
onBlur,
onMouseDown,
onMouseUp,
onActiveChange,

onListChangeStart,
Expand Down Expand Up @@ -292,6 +296,8 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
onKeyDown={onKeyDown}
onFocus={onFocus}
onBlur={onBlur}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
onVisibleChange={originList => {
// The best match is using `fullList` - `originList` = `restList`
// and check the `restList` to see if has the MOTION_KEY node
Expand Down
21 changes: 20 additions & 1 deletion src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export interface TreeProps<TreeDataType extends BasicDataNode = DataNode> {
allowDrop?: AllowDrop<TreeDataType>;
titleRender?: (node: TreeDataType) => React.ReactNode;
dropIndicatorRender?: (props: DropIndicatorProps) => React.ReactNode;
onMouseDown?: React.MouseEventHandler<HTMLDivElement>;
onMouseUp?: React.MouseEventHandler<HTMLDivElement>;
onFocus?: React.FocusEventHandler<HTMLDivElement>;
onBlur?: React.FocusEventHandler<HTMLDivElement>;
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
Expand Down Expand Up @@ -312,6 +314,8 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea

currentMouseOverDroppableNodeKey = null;

focusedByMouse = false;

listRef = React.createRef<NodeListRef>();

componentDidMount(): void {
Expand Down Expand Up @@ -1063,11 +1067,23 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea
}
};

onMouseDown: React.MouseEventHandler<HTMLDivElement> = event => {
this.focusedByMouse = true;
const { onMouseDown } = this.props;
onMouseDown?.(event);
};

onMouseUp: React.MouseEventHandler<HTMLDivElement> = event => {
this.focusedByMouse = false;
const { onMouseUp } = this.props;
onMouseUp?.(event);
};

onFocus: React.FocusEventHandler<HTMLDivElement> = (...args) => {
const { onFocus, disabled } = this.props;
const { activeKey, selectedKeys, flattenNodes } = this.state;

if (!disabled && activeKey === null) {
if (!this.focusedByMouse && !disabled && activeKey === null) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果点击事件区分才执行下面逻辑的话,看起来下面逻辑就是不充分的。那岂不也可以直接去掉交给点击和键盘自行操作么

const visibleSelectedKey = selectedKeys.find(key => {
return flattenNodes.some(nodeItem => nodeItem.key === key);
});
Expand All @@ -1083,6 +1099,7 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea
};

onBlur: React.FocusEventHandler<HTMLDivElement> = (...args) => {
this.focusedByMouse = false;
const { onBlur } = this.props;
this.onActiveChange(null);

Expand Down Expand Up @@ -1521,6 +1538,8 @@ class Tree<TreeDataType extends DataNode | BasicDataNode = DataNode> extends Rea
tabIndex={tabIndex}
activeItem={this.getActiveItem()}
onFocus={this.onFocus}
onMouseDown={this.onMouseDown}
Comment thread
aojunhao123 marked this conversation as resolved.
onMouseUp={this.onMouseUp}
onBlur={this.onBlur}
onKeyDown={this.onKeyDown}
onActiveChange={this.onActiveChange}
Expand Down
26 changes: 24 additions & 2 deletions tests/Tree.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ describe('Tree Basic', () => {
// trigger click to expand node
fireEvent.click(container.querySelector('.rc-tree-switcher'));
expect(container.querySelector('.rc-tree-switcher')).toHaveClass(OPEN_CLASSNAME);
expect(onExpand).toBeCalled();
expect(onExpand).toHaveBeenCalled();

// click again
onExpand.mockReset();
Expand Down Expand Up @@ -1201,7 +1201,7 @@ describe('Tree Basic', () => {
<Tree itemHeight={10} height={100} treeData={data} onScroll={onScroll} />,
);
fireEvent.scroll(container.querySelector('.rc-tree-list-holder'));
expect(onScroll).toBeCalled();
expect(onScroll).toHaveBeenCalled();
});

it('should support rootStyle and rootClassName', () => {
Expand Down Expand Up @@ -1411,4 +1411,26 @@ describe('Tree Basic', () => {
expect(title).toHaveClass(testClassNames.itemTitle);
expect(item).toHaveStyle(testStyles.item);
});

it('should not scroll to top when click node and tree is focused', () => {
const data = [
{ key: '0', title: '0' },
{ key: '1', title: '1' },
{ key: '2', title: '2' },
];
const treeRef = React.createRef<any>();
const { container } = render(<Tree ref={treeRef} treeData={data} />);
const treeContainer = container.querySelector('.rc-tree-list');
const scrollToSpy = jest.spyOn(treeRef.current, 'scrollTo');

// Simulate pointer focus without existing selectedKeys
fireEvent.mouseDown(treeContainer);
expect(treeRef.current.focusedByMouse).toBe(true);
fireEvent.focus(treeContainer);
fireEvent.mouseUp(treeContainer);

expect(treeRef.current.focusedByMouse).toBe(false);
expect(scrollToSpy).not.toHaveBeenCalled();
scrollToSpy.mockRestore();
});
Comment on lines +1415 to +1435
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsdom 里模拟不了真实滚动行为,所以这里的 case 测了内部的实现细节

});