@zhsz/cool-design-crud
Version:
77 lines (76 loc) • 2.12 kB
JavaScript
import { pickBy } from "lodash-es";
import { isObject } from "./index.mjs";
function processItem(item, fieldNames = {
value: "value",
label: "label",
children: "children"
}) {
const outputItem = {};
if (isObject(item)) {
for (const key in fieldNames) {
outputItem[key] = item[fieldNames[key]];
}
if (item[fieldNames.children] && item[fieldNames.children].length > 0) {
outputItem.children = item[fieldNames.children].map(
(s) => processItem(s, fieldNames)
);
}
return pickBy(outputItem, (value) => value !== void 0);
} else {
for (const key in fieldNames) {
if (key !== "children") {
outputItem[key] = item;
}
}
return pickBy(outputItem, (value) => value !== void 0);
}
}
function extractDefaultsRecursively(source, result = {}) {
for (const key in source) {
const node = source[key];
if (node.type === "void" && node.properties) {
extractDefaultsRecursively(node.properties, result);
} else {
result[key] = node.default !== void 0 ? node.default : void 0;
}
}
return result;
}
function processProperties(properties, formHook, form, type) {
for (const prop in properties) {
const property = properties[prop];
if (property.hook) {
formHook[type]({
hook: property.hook,
prop,
value: form[prop],
form
});
}
if (property.type === "void" && property.properties) {
processProperties(property.properties, formHook, form, type);
} else if (property.properties) {
iterateProperties(property.properties, formHook, form, type);
}
}
}
function iterateProperties(properties, formHook, form, type) {
for (const prop in properties) {
const property = properties[prop];
if (property.type !== "void") {
formHook[type]({
hook: property.hook,
prop,
value: form[prop],
form
});
} else if (property.properties) {
processProperties(property.properties, formHook, form, type);
}
}
}
export {
extractDefaultsRecursively,
processItem,
processProperties
};