@@ -90,8 +90,8 @@ const set = (state: State, action: Action) => ({
9090
9191const ADD_BY_ID = 'ADD_BY_ID' ;
9292const 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 ' ;
9595const ADD_BY_ID_EXCLUSIVELY = 'ADD_BY_ID_EXCLUSIVELY' ;
9696const REMOVE_BY_ID_EXCLUSIVELY = 'REMOVE_BY_ID_EXCLUSIVELY' ;
9797const ADD_ALL = 'ADD_ALL' ;
@@ -106,10 +106,10 @@ const reducer = (state: State, action: Action) => {
106106 case REMOVE_BY_ID : {
107107 return removeById ( state , action ) ;
108108 }
109- case ADD_BY_ID_RECURSIVELY : {
109+ case ADD_BY_IDS : {
110110 return addByIdRecursively ( state , action ) ;
111111 }
112- case REMOVE_BY_ID_RECURSIVELY : {
112+ case REMOVE_BY_IDS : {
113113 return removeByIdRecursively ( state , action ) ;
114114 }
115115 case ADD_BY_ID_EXCLUSIVELY : {
@@ -142,6 +142,12 @@ const getMergedOptions = (options: Record<string, any>) => ({
142142 ...options ,
143143} ) ;
144144
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+
145151const useIdReducer = (
146152 data : Data ,
147153 controlledState : State ,
@@ -203,12 +209,12 @@ const useIdReducer = (
203209 [ state , onAddById , onRemoveById ] ,
204210 ) ;
205211
206- const onAddByIdRecursively = React . useCallback (
212+ const onAddByIds = React . useCallback (
207213 ( ids , options ) => {
208214 const mergedOptions = getMergedOptions ( options ) ;
209215
210216 dispatchWithMiddleware ( {
211- type : ADD_BY_ID_RECURSIVELY ,
217+ type : ADD_BY_IDS ,
212218 payload : {
213219 ids,
214220 options : mergedOptions ,
@@ -218,10 +224,10 @@ const useIdReducer = (
218224 [ dispatchWithMiddleware ] ,
219225 ) ;
220226
221- const onRemoveByIdRecursively = React . useCallback (
227+ const onRemoveByIds = React . useCallback (
222228 ( ids ) => {
223229 dispatchWithMiddleware ( {
224- type : REMOVE_BY_ID_RECURSIVELY ,
230+ type : REMOVE_BY_IDS ,
225231 payload : { ids } ,
226232 } ) ;
227233 } ,
@@ -232,30 +238,48 @@ const useIdReducer = (
232238 ( id , options ) => {
233239 const mergedOptions = getMergedOptions ( options ) ;
234240
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 ) ;
238242
239243 if ( ! mergedOptions . isPartialToAll ) {
240244 if ( includesNone ( ids , state . ids ) ) {
241- onAddByIdRecursively ( ids , mergedOptions ) ;
245+ onAddByIds ( ids , mergedOptions ) ;
242246 } else {
243- onRemoveByIdRecursively ( ids ) ;
247+ onRemoveByIds ( ids ) ;
244248 }
245249 }
246250
247251 if ( mergedOptions . isPartialToAll ) {
248252 if ( includesAll ( ids , state . ids ) ) {
249- onRemoveByIdRecursively ( ids ) ;
253+ onRemoveByIds ( ids ) ;
250254 } else {
251- onAddByIdRecursively ( ids , mergedOptions ) ;
255+ onAddByIds ( ids , mergedOptions ) ;
252256 }
253257 }
254258
255259 shiftToggle . current . lastToggledId = id ;
256260 shiftToggle . current . currentShiftIds = [ ] ;
257261 } ,
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 ] ,
259283 ) ;
260284
261285 const onAddByIdExclusively = React . useCallback (
@@ -334,7 +358,7 @@ const useIdReducer = (
334358 const mergedOptions = getMergedOptions ( options ) ;
335359
336360 if ( shiftToggle . current . currentShiftIds . length ) {
337- onRemoveByIdRecursively ( shiftToggle . current . currentShiftIds ) ;
361+ onRemoveByIds ( shiftToggle . current . currentShiftIds ) ;
338362 shiftToggle . current . currentShiftIds = [ ] ;
339363 }
340364
@@ -352,10 +376,10 @@ const useIdReducer = (
352376
353377 const newShiftIds = ids . slice ( originIndex , targetIndex + 1 ) ;
354378
355- onAddByIdRecursively ( newShiftIds , mergedOptions ) ;
379+ onAddByIds ( newShiftIds , mergedOptions ) ;
356380 shiftToggle . current . currentShiftIds = newShiftIds ;
357381 } ,
358- [ data . nodes , onAddByIdRecursively , onRemoveByIdRecursively ] ,
382+ [ data . nodes , onAddByIds , onRemoveByIds ] ,
359383 ) ;
360384
361385 useSyncControlledState ( controlledState , state , ( ) =>
@@ -371,9 +395,12 @@ const useIdReducer = (
371395 onRemoveById,
372396 onToggleById,
373397
398+ onAddByIds,
399+ onRemoveByIds,
400+ onToggleByIdRecursively,
401+
374402 onAddByIdRecursively,
375403 onRemoveByIdRecursively,
376- onToggleByIdRecursively,
377404
378405 onAddByIdExclusively,
379406 onRemoveByIdExclusively,
@@ -388,17 +415,19 @@ const useIdReducer = (
388415 [
389416 onAddAll ,
390417 onAddById ,
391- onAddByIdRecursively ,
418+ onAddByIds ,
392419 onRemoveAll ,
393420 onRemoveById ,
394- onRemoveByIdRecursively ,
421+ onRemoveByIds ,
395422 onAddByIdExclusively ,
396423 onRemoveByIdExclusively ,
397424 onToggleByIdExclusively ,
398425 onToggleAll ,
399426 onToggleById ,
400427 onToggleByIdRecursively ,
401428 onToggleByIdShift ,
429+ onAddByIdRecursively ,
430+ onRemoveByIdRecursively ,
402431 ] ,
403432 ) ;
404433
0 commit comments