@@ -7,28 +7,26 @@ import type { } from 'type-fest';
7
7
const forwardRef = React . forwardRef as FixedForwardRef
8
8
9
9
// types ==================================
10
- type IDType = string | number
11
-
12
- export interface StatBase {
13
- id : IDType ,
14
- pid : IDType | null ,
15
- childIds : IDType [ ] ,
16
- siblingIds : IDType [ ] ,
10
+ export type Id = string | number
11
+ export type Checked = boolean | null
12
+ export interface Stat < T > {
13
+ id : Id ,
14
+ pid : Id | null ,
15
+ childIds : Id [ ] ,
16
+ siblingIds : Id [ ] ,
17
17
index : number ,
18
18
level : number ,
19
- open : boolean ,
20
- checked : boolean | null ,
21
- draggable : boolean ,
22
- _isStat ?: boolean ,
23
- }
24
- export interface Stat < T > extends StatBase {
25
19
node : T ,
26
20
parent : T | null ,
27
21
parentStat : Stat < T > | null ,
28
22
children : T [ ] ,
29
23
childStats : Stat < T > [ ] ,
30
24
siblings : T [ ] ,
31
25
siblingStats : Stat < T > [ ] ,
26
+ _isStat ?: boolean ,
27
+ open : boolean ,
28
+ checked : Checked ,
29
+ draggable : boolean ,
32
30
}
33
31
34
32
// single instance ==================================
@@ -94,17 +92,17 @@ export function useTree<T extends Record<string, any>>(
94
92
// mainCache ==================================
95
93
const mainCache = useMemo (
96
94
( ) => {
97
- const stats : Record < IDType , Stat < T > > = { }
98
- const nodes : Record < IDType , T > = { }
99
- const openedIds : IDType [ ] = [ ]
100
- const checkedIds : IDType [ ] = [ ]
101
- const semiCheckedIds : IDType [ ] = [ ]
102
- const rootIds : StatBase [ 'childIds' ] = [ ]
95
+ const stats : Record < Id , Stat < T > > = { }
96
+ const nodes : Record < Id , T > = { }
97
+ const openedIds : Id [ ] = [ ]
98
+ const checkedIds : Id [ ] = [ ]
99
+ const semiCheckedIds : Id [ ] = [ ]
100
+ const rootIds : Id [ ] = [ ]
103
101
const rootNodes : T [ ] = [ ]
104
102
const rootNodeStats : Stat < T > [ ] = [ ]
105
103
//
106
- const tasks : Record < IDType , ( parentStat : Stat < T > ) => void > = { }
107
- const addTask = ( pid : IDType , task : ( parentStat ?: Stat < T > ) => void ) => {
104
+ const tasks : Record < Id , ( parentStat : Stat < T > ) => void > = { }
105
+ const addTask = ( pid : Id , task : ( parentStat ?: Stat < T > ) => void ) => {
108
106
const parentStat = stats [ pid ]
109
107
if ( parentStat ) {
110
108
task ( parentStat )
@@ -116,20 +114,20 @@ export function useTree<T extends Record<string, any>>(
116
114
}
117
115
}
118
116
}
119
- const removeTask = ( id : IDType ) => {
117
+ const removeTask = ( id : Id ) => {
120
118
tasks [ id ] ?.( stats [ id ] )
121
119
delete tasks [ id ]
122
120
}
123
121
//
124
122
for ( const node of props . data ) {
125
- const id = node [ ID ] as IDType
126
- const pid = node [ PID ] as IDType
123
+ const id = node [ ID ] as Id
124
+ const pid = node [ PID ] as Id
127
125
const parent = nodes [ pid ] || null
128
126
addTask ( pid , ( ) => {
129
127
130
128
} )
131
129
const parentStat = stats [ pid ] || null ;
132
- const childIds : StatBase [ 'childIds' ] = [ ]
130
+ const childIds : Id [ ] = [ ]
133
131
const children : T [ ] = [ ]
134
132
const childStats : Stat < T > [ ] = [ ]
135
133
let siblingIds , siblings , siblingStats
@@ -195,8 +193,8 @@ export function useTree<T extends Record<string, any>>(
195
193
196
194
}
197
195
}
198
- const getStat = ( nodeOrStatOrId : T | Stat < T > | IDType ) => {
199
- let id : IDType
196
+ const getStat = ( nodeOrStatOrId : T | Stat < T > | Id ) => {
197
+ let id : Id
200
198
if ( typeof nodeOrStatOrId === 'object' ) {
201
199
// @ts -ignore
202
200
id = nodeOrStatOrId . _isStat ? nodeOrStatOrId . id : nodeOrStatOrId [ ID ]
@@ -210,7 +208,7 @@ export function useTree<T extends Record<string, any>>(
210
208
const skip = ( ) => { _skip = true }
211
209
yield * walk ( node )
212
210
function * walk ( node ?: T | null ) : Generator < { node : T , stat : Stat < T > , skip : ( ) => void } > {
213
- let childIds : IDType [ ]
211
+ let childIds : Id [ ]
214
212
if ( node ) {
215
213
const stat = stats [ node [ ID ] ]
216
214
yield { node, stat, skip }
@@ -238,8 +236,8 @@ export function useTree<T extends Record<string, any>>(
238
236
function resolveChecked ( node : T , checked : boolean ) {
239
237
const ckSet = new Set ( checkedIds ) ;
240
238
const semiSet = new Set ( semiCheckedIds ) ;
241
- const t : Record < IDType , StatBase [ 'checked' ] > = { }
242
- const update = ( id : IDType , checked : StatBase [ 'checked' ] ) => {
239
+ const t : Record < Id , Checked > = { }
240
+ const update = ( id : Id , checked : Checked ) => {
243
241
t [ id ] = checked
244
242
if ( checked === null ) {
245
243
ckSet . delete ( id )
@@ -298,7 +296,7 @@ export function useTree<T extends Record<string, any>>(
298
296
const isExternal = ! draggedStat
299
297
const cacheForVisible = useMemo (
300
298
( ) => {
301
- const visibleIds : IDType [ ] = [ ]
299
+ const visibleIds : Id [ ] = [ ]
302
300
const attrsList : ( React . HTMLProps < HTMLDivElement > & { 'data-key' : string , 'data-level' : string , 'data-node-box' : boolean , 'data-drag-placeholder' ?: boolean } ) [ ] = [ ]
303
301
for ( const { stat, skip } of traverseChildNodesIncludingSelf ( ) ) {
304
302
const attr = createAttrs ( stat )
@@ -587,7 +585,7 @@ export function useTree<T extends Record<string, any>>(
587
585
let closest = stat
588
586
let index = visibleIds . indexOf ( stat . id ) // index of closest node
589
587
let atTop = false
590
- const isPlaceholderOrDraggedNode = ( id : IDType ) => id === props . placeholderId || getStat ( id ) === draggedStat
588
+ const isPlaceholderOrDraggedNode = ( id : Id ) => id === props . placeholderId || getStat ( id ) === draggedStat
591
589
const find = ( startIndex : number , step : number ) => {
592
590
let i = startIndex , cur
593
591
do {
@@ -650,7 +648,7 @@ export const HeTree = <T extends Record<string, any>,>(props: ReturnType<typeof
650
648
//
651
649
return (
652
650
< div className = "he-tree" onDragOver = { props . onDragOverRoot } onDrop = { props . onDropToRoot } >
653
- < VirtualList < IDType > ref = { props . virtualList } items = { props . visibleIds } virtual = { false } persistentIndices = { persistentIndices }
651
+ < VirtualList < Id > ref = { props . virtualList } items = { props . visibleIds } virtual = { false } persistentIndices = { persistentIndices }
654
652
renderItem = { ( id , index ) => (
655
653
< div { ...props . attrsList [ index ] } >
656
654
{
@@ -694,4 +692,6 @@ export function* traverseTreeData<T>(
694
692
695
693
function calculateDistance ( x1 : number , y1 : number , x2 : number , y2 : number ) {
696
694
return Math . sqrt ( Math . pow ( ( x2 - x1 ) , 2 ) + Math . pow ( ( y2 - y1 ) , 2 ) ) ;
697
- }
695
+ }
696
+
697
+
0 commit comments