@@ -90,8 +90,8 @@ const set = (state: State, action: Action) => ({
90
90
91
91
const ADD_BY_ID = 'ADD_BY_ID' ;
92
92
const REMOVE_BY_ID = 'REMOVE_BY_ID' ;
93
- const ADD_BY_ID_RECURSIVELY = 'ADD_BY_ID_RECURSIVELY ' ;
94
- const REMOVE_BY_ID_RECURSIVELY = 'REMOVE_BY_ID_RECURSIVELY ' ;
93
+ const ADD_BY_IDS = 'ADD_BY_IDS ' ;
94
+ const REMOVE_BY_IDS = 'REMOVE_BY_IDS ' ;
95
95
const ADD_BY_ID_EXCLUSIVELY = 'ADD_BY_ID_EXCLUSIVELY' ;
96
96
const REMOVE_BY_ID_EXCLUSIVELY = 'REMOVE_BY_ID_EXCLUSIVELY' ;
97
97
const ADD_ALL = 'ADD_ALL' ;
@@ -106,10 +106,10 @@ const reducer = (state: State, action: Action) => {
106
106
case REMOVE_BY_ID : {
107
107
return removeById ( state , action ) ;
108
108
}
109
- case ADD_BY_ID_RECURSIVELY : {
109
+ case ADD_BY_IDS : {
110
110
return addByIdRecursively ( state , action ) ;
111
111
}
112
- case REMOVE_BY_ID_RECURSIVELY : {
112
+ case REMOVE_BY_IDS : {
113
113
return removeByIdRecursively ( state , action ) ;
114
114
}
115
115
case ADD_BY_ID_EXCLUSIVELY : {
@@ -142,6 +142,12 @@ const getMergedOptions = (options: Record<string, any>) => ({
142
142
...options ,
143
143
} ) ;
144
144
145
+ const getRecursiveIds = ( id : string , nodes : TableNode [ ] ) => {
146
+ const node = findNodeById ( nodes , id ) ;
147
+
148
+ return [ node , ...fromTreeToList ( node ?. nodes ) ] . map ( ( item ) => item ! . id ) ;
149
+ } ;
150
+
145
151
const useIdReducer = (
146
152
data : Data ,
147
153
controlledState : State ,
@@ -203,12 +209,12 @@ const useIdReducer = (
203
209
[ state , onAddById , onRemoveById ] ,
204
210
) ;
205
211
206
- const onAddByIdRecursively = React . useCallback (
212
+ const onAddByIds = React . useCallback (
207
213
( ids , options ) => {
208
214
const mergedOptions = getMergedOptions ( options ) ;
209
215
210
216
dispatchWithMiddleware ( {
211
- type : ADD_BY_ID_RECURSIVELY ,
217
+ type : ADD_BY_IDS ,
212
218
payload : {
213
219
ids,
214
220
options : mergedOptions ,
@@ -218,10 +224,10 @@ const useIdReducer = (
218
224
[ dispatchWithMiddleware ] ,
219
225
) ;
220
226
221
- const onRemoveByIdRecursively = React . useCallback (
227
+ const onRemoveByIds = React . useCallback (
222
228
( ids ) => {
223
229
dispatchWithMiddleware ( {
224
- type : REMOVE_BY_ID_RECURSIVELY ,
230
+ type : REMOVE_BY_IDS ,
225
231
payload : { ids } ,
226
232
} ) ;
227
233
} ,
@@ -232,30 +238,48 @@ const useIdReducer = (
232
238
( id , options ) => {
233
239
const mergedOptions = getMergedOptions ( options ) ;
234
240
235
- const node = findNodeById ( data . nodes , id ) ;
236
-
237
- const ids = [ node , ...fromTreeToList ( node ?. nodes ) ] . map ( ( item ) => item ! . id ) ;
241
+ const ids = getRecursiveIds ( id , data . nodes ) ;
238
242
239
243
if ( ! mergedOptions . isPartialToAll ) {
240
244
if ( includesNone ( ids , state . ids ) ) {
241
- onAddByIdRecursively ( ids , mergedOptions ) ;
245
+ onAddByIds ( ids , mergedOptions ) ;
242
246
} else {
243
- onRemoveByIdRecursively ( ids ) ;
247
+ onRemoveByIds ( ids ) ;
244
248
}
245
249
}
246
250
247
251
if ( mergedOptions . isPartialToAll ) {
248
252
if ( includesAll ( ids , state . ids ) ) {
249
- onRemoveByIdRecursively ( ids ) ;
253
+ onRemoveByIds ( ids ) ;
250
254
} else {
251
- onAddByIdRecursively ( ids , mergedOptions ) ;
255
+ onAddByIds ( ids , mergedOptions ) ;
252
256
}
253
257
}
254
258
255
259
shiftToggle . current . lastToggledId = id ;
256
260
shiftToggle . current . currentShiftIds = [ ] ;
257
261
} ,
258
- [ data . nodes , state . ids , onAddByIdRecursively , onRemoveByIdRecursively ] ,
262
+ [ data . nodes , state . ids , onAddByIds , onRemoveByIds ] ,
263
+ ) ;
264
+
265
+ const onAddByIdRecursively = React . useCallback (
266
+ ( id , options ) => {
267
+ const mergedOptions = getMergedOptions ( options ) ;
268
+
269
+ const ids = getRecursiveIds ( id , data . nodes ) ;
270
+
271
+ onAddByIds ( ids , mergedOptions ) ;
272
+ } ,
273
+ [ data . nodes , onAddByIds ] ,
274
+ ) ;
275
+
276
+ const onRemoveByIdRecursively = React . useCallback (
277
+ ( id ) => {
278
+ const ids = getRecursiveIds ( id , data . nodes ) ;
279
+
280
+ onRemoveByIds ( ids ) ;
281
+ } ,
282
+ [ data . nodes , onRemoveByIds ] ,
259
283
) ;
260
284
261
285
const onAddByIdExclusively = React . useCallback (
@@ -334,7 +358,7 @@ const useIdReducer = (
334
358
const mergedOptions = getMergedOptions ( options ) ;
335
359
336
360
if ( shiftToggle . current . currentShiftIds . length ) {
337
- onRemoveByIdRecursively ( shiftToggle . current . currentShiftIds ) ;
361
+ onRemoveByIds ( shiftToggle . current . currentShiftIds ) ;
338
362
shiftToggle . current . currentShiftIds = [ ] ;
339
363
}
340
364
@@ -352,10 +376,10 @@ const useIdReducer = (
352
376
353
377
const newShiftIds = ids . slice ( originIndex , targetIndex + 1 ) ;
354
378
355
- onAddByIdRecursively ( newShiftIds , mergedOptions ) ;
379
+ onAddByIds ( newShiftIds , mergedOptions ) ;
356
380
shiftToggle . current . currentShiftIds = newShiftIds ;
357
381
} ,
358
- [ data . nodes , onAddByIdRecursively , onRemoveByIdRecursively ] ,
382
+ [ data . nodes , onAddByIds , onRemoveByIds ] ,
359
383
) ;
360
384
361
385
useSyncControlledState ( controlledState , state , ( ) =>
@@ -371,9 +395,12 @@ const useIdReducer = (
371
395
onRemoveById,
372
396
onToggleById,
373
397
398
+ onAddByIds,
399
+ onRemoveByIds,
400
+ onToggleByIdRecursively,
401
+
374
402
onAddByIdRecursively,
375
403
onRemoveByIdRecursively,
376
- onToggleByIdRecursively,
377
404
378
405
onAddByIdExclusively,
379
406
onRemoveByIdExclusively,
@@ -388,17 +415,19 @@ const useIdReducer = (
388
415
[
389
416
onAddAll ,
390
417
onAddById ,
391
- onAddByIdRecursively ,
418
+ onAddByIds ,
392
419
onRemoveAll ,
393
420
onRemoveById ,
394
- onRemoveByIdRecursively ,
421
+ onRemoveByIds ,
395
422
onAddByIdExclusively ,
396
423
onRemoveByIdExclusively ,
397
424
onToggleByIdExclusively ,
398
425
onToggleAll ,
399
426
onToggleById ,
400
427
onToggleByIdRecursively ,
401
428
onToggleByIdShift ,
429
+ onAddByIdRecursively ,
430
+ onRemoveByIdRecursively ,
402
431
] ,
403
432
) ;
404
433
0 commit comments