@@ -14,6 +14,7 @@ import {
14
14
has ,
15
15
keys ,
16
16
map ,
17
+ mapObjIndexed ,
17
18
mergeRight ,
18
19
pick ,
19
20
pickBy ,
@@ -252,24 +253,48 @@ class BaseTreeContainer extends Component {
252
253
253
254
for ( let i = 0 ; i < childrenProps . length ; i ++ ) {
254
255
const childrenProp = childrenProps [ i ] ;
256
+
257
+ const handleObject = ( obj , opath ) => {
258
+ return mapObjIndexed (
259
+ ( node , k ) => this . wrapChildrenProp ( node , [ ...opath , k ] ) ,
260
+ obj
261
+ ) ;
262
+ } ;
263
+
255
264
if ( childrenProp . includes ( '.' ) ) {
256
265
let path = childrenProp . split ( '.' ) ;
257
266
let node ;
258
267
let nodeValue ;
259
268
if ( childrenProp . includes ( '[]' ) ) {
260
269
let frontPath = [ ] ,
261
270
backPath = [ ] ,
262
- found = false ;
271
+ found = false ,
272
+ hasObject = false ;
263
273
path . forEach ( p => {
264
274
if ( ! found ) {
265
275
if ( p . includes ( '[]' ) ) {
266
276
found = true ;
267
- frontPath . push ( p . replace ( '[]' , '' ) ) ;
277
+ if ( p . includes ( '{}' ) ) {
278
+ hasObject = true ;
279
+ frontPath . push (
280
+ p . replace ( '{}' , '' ) . replace ( '[]' , '' )
281
+ ) ;
282
+ } else {
283
+ frontPath . push ( p . replace ( '[]' , '' ) ) ;
284
+ }
285
+ } else if ( p . includes ( '{}' ) ) {
286
+ hasObject = true ;
287
+ frontPath . push ( p . replace ( '{}' , '' ) ) ;
268
288
} else {
269
289
frontPath . push ( p ) ;
270
290
}
271
291
} else {
272
- backPath . push ( p ) ;
292
+ if ( p . includes ( '{}' ) ) {
293
+ hasObject = true ;
294
+ backPath . push ( p . replace ( '{}' , '' ) ) ;
295
+ } else {
296
+ backPath . push ( p ) ;
297
+ }
273
298
}
274
299
} ) ;
275
300
@@ -281,38 +306,120 @@ class BaseTreeContainer extends Component {
281
306
if ( ! firstNode ) {
282
307
continue ;
283
308
}
309
+
284
310
nodeValue = node . map ( ( element , i ) => {
285
311
const elementPath = concat (
286
312
frontPath ,
287
313
concat ( [ i ] , backPath )
288
314
) ;
289
- return assocPath (
290
- backPath ,
291
- this . wrapChildrenProp (
315
+ let listValue ;
316
+ if ( hasObject ) {
317
+ if ( backPath . length ) {
318
+ listValue = handleObject (
319
+ rpath ( backPath , element ) ,
320
+ elementPath
321
+ ) ;
322
+ } else {
323
+ listValue = handleObject ( element , elementPath ) ;
324
+ }
325
+ } else {
326
+ listValue = this . wrapChildrenProp (
292
327
rpath ( backPath , element ) ,
293
328
elementPath
294
- ) ,
295
- element
296
- ) ;
329
+ ) ;
330
+ }
331
+ return assocPath ( backPath , listValue , element ) ;
297
332
} ) ;
298
333
path = frontPath ;
299
334
} else {
300
- node = rpath ( path , props ) ;
301
- if ( node === undefined ) {
302
- continue ;
335
+ if ( childrenProp . includes ( '{}' ) ) {
336
+ // Only supports one level of nesting.
337
+ const front = [ ] ;
338
+ let dynamic = [ ] ;
339
+ let hasBack = false ;
340
+ const backPath = [ ] ;
341
+
342
+ for ( let j = 0 ; j < path . length ; j ++ ) {
343
+ const cur = path [ j ] ;
344
+ if ( cur . includes ( '{}' ) ) {
345
+ dynamic = concat ( front , [
346
+ cur . replace ( '{}' , '' )
347
+ ] ) ;
348
+ if ( j < path . length - 1 ) {
349
+ hasBack = true ;
350
+ }
351
+ } else {
352
+ if ( hasBack ) {
353
+ backPath . push ( cur ) ;
354
+ } else {
355
+ front . push ( cur ) ;
356
+ }
357
+ }
358
+ }
359
+
360
+ const dynValue = rpath ( dynamic , props ) ;
361
+ if ( dynValue !== undefined ) {
362
+ nodeValue = mapObjIndexed (
363
+ ( d , k ) =>
364
+ this . wrapChildrenProp (
365
+ hasBack ? rpath ( backPath , d ) : d ,
366
+ hasBack
367
+ ? concat (
368
+ dynamic ,
369
+ concat ( [ k ] , backPath )
370
+ )
371
+ : concat ( dynamic , [ k ] )
372
+ ) ,
373
+ dynValue
374
+ ) ;
375
+ path = dynamic ;
376
+ }
377
+ } else {
378
+ node = rpath ( path , props ) ;
379
+ if ( node === undefined ) {
380
+ continue ;
381
+ }
382
+ nodeValue = this . wrapChildrenProp ( node , path ) ;
303
383
}
304
- nodeValue = this . wrapChildrenProp ( node , path ) ;
305
384
}
306
385
props = assocPath ( path , nodeValue , props ) ;
307
- continue ;
308
- }
309
- const node = props [ childrenProp ] ;
310
- if ( node !== undefined ) {
311
- props = assoc (
312
- childrenProp ,
313
- this . wrapChildrenProp ( node , [ childrenProp ] ) ,
314
- props
315
- ) ;
386
+ } else {
387
+ if ( childrenProp . includes ( '{}' ) ) {
388
+ let opath = childrenProp . replace ( '{}' , '' ) ;
389
+ const isArray = childrenProp . includes ( '[]' ) ;
390
+ if ( isArray ) {
391
+ opath = opath . replace ( '[]' , '' ) ;
392
+ }
393
+ const node = props [ opath ] ;
394
+
395
+ if ( node !== undefined ) {
396
+ if ( isArray ) {
397
+ for ( let j = 0 ; j < node . length ; j ++ ) {
398
+ const aPath = concat ( [ opath ] , [ j ] ) ;
399
+ props = assocPath (
400
+ aPath ,
401
+ handleObject ( node [ j ] , aPath ) ,
402
+ props
403
+ ) ;
404
+ }
405
+ } else {
406
+ props = assoc (
407
+ opath ,
408
+ handleObject ( node , [ opath ] ) ,
409
+ props
410
+ ) ;
411
+ }
412
+ }
413
+ } else {
414
+ const node = props [ childrenProp ] ;
415
+ if ( node !== undefined ) {
416
+ props = assoc (
417
+ childrenProp ,
418
+ this . wrapChildrenProp ( node , [ childrenProp ] ) ,
419
+ props
420
+ ) ;
421
+ }
422
+ }
316
423
}
317
424
}
318
425
0 commit comments