Skip to content

Commit 2478f1e

Browse files
committed
Wip support objectOf component type.
1 parent 181c2a1 commit 2478f1e

File tree

3 files changed

+189
-36
lines changed

3 files changed

+189
-36
lines changed

dash/dash-renderer/src/TreeContainer.js

Lines changed: 131 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
propOr,
2121
path as rpath,
2222
pathOr,
23-
type
23+
type,
24+
toPairs
2425
} from 'ramda';
2526
import {notifyObservers, updateProps} from './actions';
2627
import isSimpleComponent from './isSimpleComponent';
@@ -249,24 +250,54 @@ class BaseTreeContainer extends Component {
249250

250251
for (let i = 0; i < childrenProps.length; i++) {
251252
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(
260+
node,
261+
concat(this.props._dashprivate_path, [...opath, k])
262+
)
263+
};
264+
}, obj);
265+
};
266+
252267
if (childrenProp.includes('.')) {
253268
let path = childrenProp.split('.');
254269
let node;
255270
let nodeValue;
256271
if (childrenProp.includes('[]')) {
257272
let frontPath = [],
258273
backPath = [],
259-
found = false;
274+
found = false,
275+
hasObject = false;
260276
path.forEach(p => {
261277
if (!found) {
262278
if (p.includes('[]')) {
263279
found = true;
264-
frontPath.push(p.replace('[]', ''));
280+
if (p.includes('{}')) {
281+
hasObject = true;
282+
frontPath.push(
283+
p.replace('{}', '').replace('[]', '')
284+
);
285+
} else {
286+
frontPath.push(p.replace('[]', ''));
287+
}
288+
} else if (p.includes('{}')) {
289+
hasObject = true;
290+
frontPath.push(p.replace('{}', ''));
265291
} else {
266292
frontPath.push(p);
267293
}
268294
} else {
269-
backPath.push(p);
295+
if (p.includes('{}')) {
296+
hasObject = true;
297+
backPath.push(p.replace('{}', ''));
298+
} else {
299+
backPath.push(p);
300+
}
270301
}
271302
});
272303

@@ -278,38 +309,115 @@ class BaseTreeContainer extends Component {
278309
if (!firstNode) {
279310
continue;
280311
}
312+
281313
nodeValue = node.map((element, i) => {
282314
const elementPath = concat(
283315
frontPath,
284316
concat([i], backPath)
285317
);
286-
return assocPath(
287-
backPath,
288-
this.wrapChildrenProp(
318+
let listValue;
319+
if (hasObject) {
320+
listValue = handleObject(element, elementPath);
321+
} else {
322+
listValue = this.wrapChildrenProp(
289323
rpath(backPath, element),
290324
elementPath
291-
),
292-
element
293-
);
325+
);
326+
}
327+
return assocPath(backPath, listValue, element);
294328
});
295329
path = frontPath;
296330
} else {
297-
node = rpath(path, props);
298-
if (node === undefined) {
299-
continue;
331+
if (childrenProp.includes('{}')) {
332+
// Only supports one level of nesting.
333+
const front = [];
334+
let dynamic = [];
335+
let hasBack = false;
336+
const backPath = [];
337+
338+
for (let j = 0; j < path.length; j++) {
339+
const cur = path[j];
340+
if (cur.includes('{}')) {
341+
dynamic = concat(front, [
342+
cur.replace('{}', '')
343+
]);
344+
if (j < path.length - 1) {
345+
hasBack = true;
346+
}
347+
} else {
348+
if (hasBack) {
349+
backPath.push(cur);
350+
} else {
351+
front.push(cur);
352+
}
353+
}
354+
}
355+
356+
const dynValue = rpath(dynamic, props);
357+
if (dynValue !== undefined) {
358+
nodeValue = toPairs(dynValue).reduce(
359+
(acc, [k, d]) => ({
360+
...acc,
361+
[k]: this.wrapChildrenProp(
362+
hasBack ? rpath(backPath, d) : d,
363+
hasBack
364+
? concat(
365+
dynamic,
366+
concat([k], backPath)
367+
)
368+
: concat(dynamic, [k])
369+
)
370+
}),
371+
{}
372+
);
373+
path = dynamic;
374+
}
375+
} else {
376+
node = rpath(path, props);
377+
if (node === undefined) {
378+
continue;
379+
}
380+
nodeValue = this.wrapChildrenProp(node, path);
300381
}
301-
nodeValue = this.wrapChildrenProp(node, path);
302382
}
303383
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-
);
384+
} else {
385+
if (childrenProp.includes('{}')) {
386+
let opath = childrenProp.replace('{}', '');
387+
const isArray = childrenProp.includes('[]');
388+
if (isArray) {
389+
opath = opath.replace('[]', '');
390+
}
391+
const node = props[opath];
392+
393+
if (node !== undefined) {
394+
if (isArray) {
395+
for (let j = 0; j < node.length; j++) {
396+
const aPath = concat([opath], [j]);
397+
props = assocPath(
398+
aPath,
399+
handleObject(node[j], aPath),
400+
props
401+
);
402+
}
403+
} else {
404+
props = assoc(
405+
opath,
406+
handleObject(node, [opath]),
407+
props
408+
);
409+
}
410+
}
411+
} else {
412+
const node = props[childrenProp];
413+
if (node !== undefined) {
414+
props = assoc(
415+
childrenProp,
416+
this.wrapChildrenProp(node, [childrenProp]),
417+
props
418+
);
419+
}
420+
}
313421
}
314422
}
315423

dash/dash-renderer/src/actions/callbacks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function fillVals(
160160
inputList.map(({id, property, path: path_}: any) => ({
161161
id,
162162
property,
163-
value: (path(path_, layout) as any).props[property]
163+
value: path([...path_, 'props', property], layout) as any
164164
})),
165165
specs[i],
166166
cb.anyVals,

dash/dash-renderer/src/actions/utils.js

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,64 @@ export const crawlLayout = (
6565
let [frontPath, backPath] = childrenProp
6666
.split('[]')
6767
.map(p => p.split('.').filter(e => e));
68-
const front = concat(['props'], frontPath);
69-
const basePath = concat(currentPath, front);
70-
crawlLayout(path(front, object), func, basePath, backPath);
68+
if (childrenProp.includes('{}')) {
69+
// TODO
70+
} else {
71+
const front = concat(['props'], frontPath);
72+
const basePath = concat(currentPath, front);
73+
crawlLayout(path(front, object), func, basePath, backPath);
74+
}
7175
} else {
72-
const newPath = concat(currentPath, [
73-
'props',
74-
...childrenProp.split('.')
75-
]);
76-
crawlLayout(
77-
path(['props', ...childrenProp.split('.')], object),
78-
func,
79-
newPath
80-
);
76+
if (childrenProp.includes('{}')) {
77+
const opath = childrenProp.split('.');
78+
const frontPath = [];
79+
const backPath = [];
80+
let found = false;
81+
82+
for (let i = 0; i < opath.length; i++) {
83+
const curPath = opath[i];
84+
if (!found && curPath.includes('{}')) {
85+
found = true;
86+
frontPath.push(curPath.replace('{}', ''));
87+
} else {
88+
if (found) {
89+
backPath.push(curPath);
90+
} else {
91+
frontPath.push(curPath);
92+
}
93+
}
94+
}
95+
const newPath = concat(currentPath, [
96+
'props',
97+
...frontPath
98+
]);
99+
100+
const oValue = path(['props', ...frontPath], object);
101+
if (oValue !== undefined) {
102+
Object.keys(oValue).forEach(key => {
103+
const value = oValue[key];
104+
if (backPath.length) {
105+
crawlLayout(
106+
path(backPath, value),
107+
func,
108+
concat(newPath, [key, ...backPath])
109+
);
110+
} else {
111+
crawlLayout(value, func, [...newPath, key]);
112+
}
113+
});
114+
}
115+
} else {
116+
const newPath = concat(currentPath, [
117+
'props',
118+
...childrenProp.split('.')
119+
]);
120+
crawlLayout(
121+
path(['props', ...childrenProp.split('.')], object),
122+
func,
123+
newPath
124+
);
125+
}
81126
}
82127
});
83128
}

0 commit comments

Comments
 (0)