You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
50 lines
1.2 KiB
import { resolveName } from "./index";
|
|
import { set as pathSet, get as pathGet } from "lodash";
|
|
|
|
export const presetDefaultValue = {
|
|
Select(item) {
|
|
if (item.options?.multiple) return [];
|
|
return null;
|
|
},
|
|
MultipleLabelItem(item) {
|
|
return reduceDefaultValue(item.options?.options);
|
|
},
|
|
// RadioGroup() {
|
|
// return [];
|
|
// },
|
|
CheckboxGroup() {
|
|
return [];
|
|
},
|
|
InputNumber() {
|
|
return 0;
|
|
},
|
|
};
|
|
|
|
export function getDefaultValue(item, data) {
|
|
if (data && typeof data !== "object") return data;
|
|
|
|
if (item.hasOwnProperty("default")) return item.default;
|
|
|
|
const getValue = presetDefaultValue[resolveName(item.type)];
|
|
|
|
return typeof getValue === "function" ? getValue(item) : null;
|
|
}
|
|
|
|
export function reduceDefaultValue(formList, data, total = {}) {
|
|
if (!Array.isArray(formList)) return null;
|
|
|
|
return formList.reduce((prev, cur) => {
|
|
if (cur?.type === "MultipleLabelItem")
|
|
Array.isArray(cur.options?.options) &&
|
|
reduceDefaultValue(cur.options.options, data, prev);
|
|
else if (cur?.key)
|
|
pathSet(
|
|
prev,
|
|
cur.key,
|
|
getDefaultValue(cur, pathGet(data || {}, cur.key))
|
|
);
|
|
// prev[cur.key] = getDefaultValue(cur, pathGet(data || {}, cur.key));
|
|
|
|
return prev;
|
|
}, total);
|
|
}
|
|
|