Skip to content

Commit 3340749

Browse files
committed
fixup! ✨(tree-view) load children on keyboard navigation and simplify toggle
1 parent ad0d7b0 commit 3340749

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

src/components/tree-view/TreeViewItem.tsx

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { NodeRendererProps } from "react-arborist";
22
import { TreeDataItem, TreeViewNodeTypeEnum } from "./types";
3-
import {
4-
PropsWithChildren,
5-
useCallback,
6-
useEffect,
7-
useRef,
8-
useState,
9-
} from "react";
3+
import { PropsWithChildren, useEffect, useRef, useState } from "react";
104
import clsx from "clsx";
115
import { Spinner } from "../loader/Spinner";
126
import { Droppable } from "../dnd/Droppable";
@@ -39,21 +33,6 @@ export const TreeViewItem = <T,>({
3933
const hasLoadedChildren = node.children?.length ?? 0 > 0;
4034

4135
const isLeaf = node.isLeaf || !hasChildren;
42-
const handleClick = useCallback(async () => {
43-
if (isLeaf) {
44-
return;
45-
}
46-
47-
if (hasLoadedChildren || node.data.value.hasLoadedChildren) {
48-
node.toggle();
49-
return;
50-
}
51-
52-
setIsLoading(true);
53-
await context?.treeData.handleLoadChildren(node.data.value.id);
54-
setIsLoading(false);
55-
node.open();
56-
}, [isLeaf, hasLoadedChildren, node, context?.treeData]);
5736

5837
useEffect(() => {
5938
if (isLeaf || hasLoadedChildren || node.data.value.hasLoadedChildren) {
@@ -75,24 +54,19 @@ export const TreeViewItem = <T,>({
7554
context?.treeData,
7655
]);
7756

78-
const handleOver = useCallback(
79-
(isOver: boolean) => {
80-
if (isOver && !node.isOpen && !node.isDragging) {
81-
timeoutRef.current = setTimeout(() => {
82-
void handleClick();
83-
}, 500);
84-
}
57+
useEffect(() => {
58+
const shouldOpenNode = isOver && !node.isOpen && !node.isDragging;
8559

86-
if (timeoutRef.current && !isOver) {
87-
clearTimeout(timeoutRef.current);
88-
}
89-
},
90-
[handleClick, node.isOpen, node.isDragging]
91-
);
60+
if (shouldOpenNode) {
61+
timeoutRef.current = setTimeout(() => {
62+
node.open();
63+
}, 500);
64+
}
9265

93-
useEffect(() => {
94-
handleOver(isOver);
95-
}, [isOver, handleOver]);
66+
if (timeoutRef.current && !isOver) {
67+
clearTimeout(timeoutRef.current);
68+
}
69+
}, [isOver, node.isOpen, node.isDragging, node]);
9670

9771
if (node.data.value.nodeType === TreeViewNodeTypeEnum.SEPARATOR) {
9872
return <div className="c__tree-view--node__separator" />;

0 commit comments

Comments
 (0)