1
1
/* eslint-disable jsx-a11y/no-static-element-interactions */
2
2
import { clsx } from 'clsx' ;
3
3
import { useEffect , useRef , useState } from 'react' ;
4
- import { MoveHandler , NodeApi , NodeRendererProps , Tree } from 'react-arborist' ;
4
+ import {
5
+ CursorProps ,
6
+ MoveHandler ,
7
+ NodeApi ,
8
+ NodeRendererProps ,
9
+ Tree ,
10
+ } from 'react-arborist' ;
5
11
6
12
import { Box } from '../../Box' ;
7
13
import { Icon } from '../../Icon' ;
@@ -21,14 +27,19 @@ const addAllChildren = <T,>(
21
27
data : TreeViewDataType < T > [ ] ,
22
28
) : TreeViewDataType < T > [ ] => {
23
29
return data . map ( ( item ) => {
24
- return { ...item , children : item . children ?? [ ] } ;
30
+ console . log ( 'item' , item ) ;
31
+ return {
32
+ ...item ,
33
+ children : item . children ? addAllChildren ( item . children ) : [ ] ,
34
+ } ;
25
35
} ) ;
26
36
} ;
27
37
28
38
export type TreeViewProps < T > = {
29
39
data : TreeViewDataType < T > [ ] ;
40
+ width ?: number ;
30
41
allCanBeFolder ?: boolean ;
31
- rootNode : BaseType < T > ;
42
+
32
43
renderNode ?: ( node : TreeViewDataType < T > ) => React . ReactNode ;
33
44
loadChildren ?: ( node : TreeViewDataType < T > ) => Promise < TreeViewDataType < T > [ ] > ;
34
45
afterMove ?: (
@@ -38,12 +49,11 @@ export type TreeViewProps<T> = {
38
49
) => void ;
39
50
} ;
40
51
41
- type ArgumentTypes < T > = Parameters < MoveHandler < TreeViewDataType < T > > > ;
42
-
43
52
export const TreeView = < T , > ( {
44
53
data : initialData ,
54
+ width,
45
55
allCanBeFolder,
46
- rootNode ,
56
+
47
57
renderNode,
48
58
loadChildren,
49
59
afterMove,
@@ -52,47 +62,6 @@ export const TreeView = <T,>({
52
62
allCanBeFolder ? addAllChildren ( initialData ) : initialData ,
53
63
) ;
54
64
55
- const onMove3 = ( args : {
56
- dragIds : string [ ] ;
57
- dragNodes : NodeApi < BaseType < T > > [ ] ;
58
- parentId : string | null ;
59
- parentNode : NodeApi < BaseType < T > > | null ;
60
- index : number ;
61
- } ) : {
62
- targetNodeId : string ;
63
- mode : 'first-child' | 'last-child' | 'left' | 'right' ;
64
- } | null => {
65
- const newData = JSON . parse ( JSON . stringify ( data ) ) as TreeViewDataType < T > [ ] ;
66
-
67
- const newIndex = args . index ;
68
- const targetNodeId = args . parentId ?? rootNode . id ;
69
- const children = args . parentId
70
- ? ( args . parentNode ?. children ?? [ ] )
71
- : newData ;
72
-
73
- if ( newIndex === 0 ) {
74
- return { targetNodeId : targetNodeId , mode : 'first-child' } ;
75
- }
76
- if ( newIndex === children . length ) {
77
- return { targetNodeId : targetNodeId , mode : 'last-child' } ;
78
- }
79
-
80
- const siblingIndex = newIndex - 1 ;
81
- const sibling = children [ siblingIndex ] ;
82
-
83
- if ( sibling ) {
84
- return { targetNodeId : sibling . id , mode : 'right' } ;
85
- }
86
-
87
- const nextSiblingIndex = newIndex + 1 ;
88
- const nextSibling = children [ nextSiblingIndex ] ;
89
- if ( nextSibling ) {
90
- return { targetNodeId : nextSibling . id , mode : 'left' } ;
91
- }
92
-
93
- return null ;
94
- } ;
95
-
96
65
const onMove = ( args : {
97
66
dragIds : string [ ] ;
98
67
dragNodes : NodeApi < BaseType < T > > [ ] ;
@@ -182,7 +151,6 @@ export const TreeView = <T,>({
182
151
findParentAndInsert ( newData ) ;
183
152
}
184
153
185
- console . log ( 'onMove --- ' , onMove3 ( args ) ) ;
186
154
setData ( newData ) ;
187
155
} ;
188
156
@@ -192,30 +160,24 @@ export const TreeView = <T,>({
192
160
} , [ initialData , setData , allCanBeFolder ] ) ;
193
161
194
162
return (
195
- < Box className = { styles . container } >
196
- < Tree
197
- data = { data }
198
- openByDefault = { false }
199
- height = { 1000 }
200
- indent = { 20 }
201
- width = { 280 }
202
- rowHeight = { 28 }
203
- overscanCount = { 1 }
204
- paddingTop = { 30 }
205
- paddingBottom = { 10 }
206
- padding = { 25 }
207
- onMove = { onMove as MoveHandler < TreeViewDataType < T > > }
208
- >
209
- { ( props ) => (
210
- < Node
211
- { ...props }
212
- renderNode = { renderNode }
213
- loadChildren = { loadChildren }
214
- />
215
- ) }
216
- </ Tree >
217
- e
218
- </ Box >
163
+ < Tree
164
+ data = { data }
165
+ openByDefault = { false }
166
+ height = { 1000 }
167
+ indent = { 20 }
168
+ openByDefault = { true }
169
+ // width={280}
170
+ width = { width }
171
+ rowHeight = { 32 }
172
+ overscanCount = { 20 }
173
+ padding = { 25 }
174
+ renderCursor = { Cursor }
175
+ onMove = { onMove as MoveHandler < TreeViewDataType < T > > }
176
+ >
177
+ { ( props ) => (
178
+ < Node { ...props } renderNode = { renderNode } loadChildren = { loadChildren } />
179
+ ) }
180
+ </ Tree >
219
181
) ;
220
182
} ;
221
183
@@ -224,6 +186,18 @@ export type NodeProps<T> = NodeRendererProps<TreeViewDataType<T>> & {
224
186
loadChildren ?: ( node : TreeViewDataType < T > ) => Promise < TreeViewDataType < T > [ ] > ;
225
187
} ;
226
188
189
+ function Cursor ( { top, left } : CursorProps ) {
190
+ return (
191
+ < div
192
+ className = { styles . cursor }
193
+ style = { {
194
+ top,
195
+ left : left + 10 ,
196
+ } }
197
+ > </ div >
198
+ ) ;
199
+ }
200
+
227
201
export const Node = < T , > ( {
228
202
renderNode,
229
203
node,
@@ -275,6 +249,7 @@ export const Node = <T,>({
275
249
if ( timeoutRef . current && ! node . willReceiveDrop ) {
276
250
clearTimeout ( timeoutRef . current ) ;
277
251
}
252
+ // eslint-disable-next-line react-hooks/exhaustive-deps
278
253
} , [ node ] ) ;
279
254
280
255
return (
@@ -283,9 +258,11 @@ export const Node = <T,>({
283
258
className = { clsx ( styles . node , {
284
259
[ styles . willReceiveDrop ] : node . willReceiveDrop ,
285
260
[ styles . selected ] : node . isSelected ,
261
+ toto : true ,
286
262
} ) }
287
263
style = { {
288
264
...style ,
265
+ width : `calc(100% + ${ node . level * 20 } px)` ,
289
266
} }
290
267
ref = { dragHandle }
291
268
>
0 commit comments