1
1
import { NodeRendererProps } from "react-arborist" ;
2
2
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" ;
10
4
import clsx from "clsx" ;
11
5
import { Spinner } from "../loader/Spinner" ;
12
6
import { Droppable } from "../dnd/Droppable" ;
@@ -39,21 +33,6 @@ export const TreeViewItem = <T,>({
39
33
const hasLoadedChildren = node . children ?. length ?? 0 > 0 ;
40
34
41
35
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 ] ) ;
57
36
58
37
useEffect ( ( ) => {
59
38
if ( isLeaf || hasLoadedChildren || node . data . value . hasLoadedChildren ) {
@@ -75,24 +54,19 @@ export const TreeViewItem = <T,>({
75
54
context ?. treeData ,
76
55
] ) ;
77
56
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 ;
85
59
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
+ }
92
65
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 ] ) ;
96
70
97
71
if ( node . data . value . nodeType === TreeViewNodeTypeEnum . SEPARATOR ) {
98
72
return < div className = "c__tree-view--node__separator" /> ;
0 commit comments