Skip to content

Commit

Permalink
Wip support objectOf component type.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Aug 26, 2022
1 parent 181c2a1 commit 2478f1e
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 36 deletions.
154 changes: 131 additions & 23 deletions dash/dash-renderer/src/TreeContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
propOr,
path as rpath,
pathOr,
type
type,
toPairs
} from 'ramda';
import {notifyObservers, updateProps} from './actions';
import isSimpleComponent from './isSimpleComponent';
Expand Down Expand Up @@ -249,24 +250,54 @@ class BaseTreeContainer extends Component {

for (let i = 0; i < childrenProps.length; i++) {
const childrenProp = childrenProps[i];

const handleObject = (obj, opath) => {
return keys(obj).reduce((acc, k) => {
const node = acc[k];
return {
...acc,
[k]: this.wrapChildrenProp(
node,
concat(this.props._dashprivate_path, [...opath, k])
)
};
}, obj);
};

if (childrenProp.includes('.')) {
let path = childrenProp.split('.');
let node;
let nodeValue;
if (childrenProp.includes('[]')) {
let frontPath = [],
backPath = [],
found = false;
found = false,
hasObject = false;
path.forEach(p => {
if (!found) {
if (p.includes('[]')) {
found = true;
frontPath.push(p.replace('[]', ''));
if (p.includes('{}')) {
hasObject = true;
frontPath.push(
p.replace('{}', '').replace('[]', '')
);
} else {
frontPath.push(p.replace('[]', ''));
}
} else if (p.includes('{}')) {
hasObject = true;
frontPath.push(p.replace('{}', ''));
} else {
frontPath.push(p);
}
} else {
backPath.push(p);
if (p.includes('{}')) {
hasObject = true;
backPath.push(p.replace('{}', ''));
} else {
backPath.push(p);
}
}
});

Expand All @@ -278,38 +309,115 @@ class BaseTreeContainer extends Component {
if (!firstNode) {
continue;
}

nodeValue = node.map((element, i) => {
const elementPath = concat(
frontPath,
concat([i], backPath)
);
return assocPath(
backPath,
this.wrapChildrenProp(
let listValue;
if (hasObject) {
listValue = handleObject(element, elementPath);
} else {
listValue = this.wrapChildrenProp(
rpath(backPath, element),
elementPath
),
element
);
);
}
return assocPath(backPath, listValue, element);
});
path = frontPath;
} else {
node = rpath(path, props);
if (node === undefined) {
continue;
if (childrenProp.includes('{}')) {
// Only supports one level of nesting.
const front = [];
let dynamic = [];
let hasBack = false;
const backPath = [];

for (let j = 0; j < path.length; j++) {
const cur = path[j];
if (cur.includes('{}')) {
dynamic = concat(front, [
cur.replace('{}', '')
]);
if (j < path.length - 1) {
hasBack = true;
}
} else {
if (hasBack) {
backPath.push(cur);
} else {
front.push(cur);
}
}
}

const dynValue = rpath(dynamic, props);
if (dynValue !== undefined) {
nodeValue = toPairs(dynValue).reduce(
(acc, [k, d]) => ({
...acc,
[k]: this.wrapChildrenProp(
hasBack ? rpath(backPath, d) : d,
hasBack
? concat(
dynamic,
concat([k], backPath)
)
: concat(dynamic, [k])
)
}),
{}
);
path = dynamic;
}
} else {
node = rpath(path, props);
if (node === undefined) {
continue;
}
nodeValue = this.wrapChildrenProp(node, path);
}
nodeValue = this.wrapChildrenProp(node, path);
}
props = assocPath(path, nodeValue, props);
continue;
}
const node = props[childrenProp];
if (node !== undefined) {
props = assoc(
childrenProp,
this.wrapChildrenProp(node, [childrenProp]),
props
);
} else {
if (childrenProp.includes('{}')) {
let opath = childrenProp.replace('{}', '');
const isArray = childrenProp.includes('[]');
if (isArray) {
opath = opath.replace('[]', '');
}
const node = props[opath];

if (node !== undefined) {
if (isArray) {
for (let j = 0; j < node.length; j++) {
const aPath = concat([opath], [j]);
props = assocPath(
aPath,
handleObject(node[j], aPath),
props
);
}
} else {
props = assoc(
opath,
handleObject(node, [opath]),
props
);
}
}
} else {
const node = props[childrenProp];
if (node !== undefined) {
props = assoc(
childrenProp,
this.wrapChildrenProp(node, [childrenProp]),
props
);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion dash/dash-renderer/src/actions/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function fillVals(
inputList.map(({id, property, path: path_}: any) => ({
id,
property,
value: (path(path_, layout) as any).props[property]
value: path([...path_, 'props', property], layout) as any
})),
specs[i],
cb.anyVals,
Expand Down
69 changes: 57 additions & 12 deletions dash/dash-renderer/src/actions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,64 @@ export const crawlLayout = (
let [frontPath, backPath] = childrenProp
.split('[]')
.map(p => p.split('.').filter(e => e));
const front = concat(['props'], frontPath);
const basePath = concat(currentPath, front);
crawlLayout(path(front, object), func, basePath, backPath);
if (childrenProp.includes('{}')) {
// TODO
} else {
const front = concat(['props'], frontPath);
const basePath = concat(currentPath, front);
crawlLayout(path(front, object), func, basePath, backPath);
}
} else {
const newPath = concat(currentPath, [
'props',
...childrenProp.split('.')
]);
crawlLayout(
path(['props', ...childrenProp.split('.')], object),
func,
newPath
);
if (childrenProp.includes('{}')) {
const opath = childrenProp.split('.');
const frontPath = [];
const backPath = [];
let found = false;

for (let i = 0; i < opath.length; i++) {
const curPath = opath[i];
if (!found && curPath.includes('{}')) {
found = true;
frontPath.push(curPath.replace('{}', ''));
} else {
if (found) {
backPath.push(curPath);
} else {
frontPath.push(curPath);
}
}
}
const newPath = concat(currentPath, [
'props',
...frontPath
]);

const oValue = path(['props', ...frontPath], object);
if (oValue !== undefined) {
Object.keys(oValue).forEach(key => {
const value = oValue[key];
if (backPath.length) {
crawlLayout(
path(backPath, value),
func,
concat(newPath, [key, ...backPath])
);
} else {
crawlLayout(value, func, [...newPath, key]);
}
});
}
} else {
const newPath = concat(currentPath, [
'props',
...childrenProp.split('.')
]);
crawlLayout(
path(['props', ...childrenProp.split('.')], object),
func,
newPath
);
}
}
});
}
Expand Down

0 comments on commit 2478f1e

Please sign in to comment.