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