Joe
10 months ago
9 changed files with 118 additions and 22 deletions
@ -0,0 +1,38 @@ |
|||
import { resolveName } from "./index"; |
|||
|
|||
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) { |
|||
if (item.hasOwnProperty("default")) return item.default; |
|||
|
|||
const getValue = presetDefaultValue[resolveName(item.type)]; |
|||
|
|||
return typeof getValue === "function" ? getValue(item) : null; |
|||
} |
|||
|
|||
export function reduceDefaultValue(formList) { |
|||
if (!Array.isArray(formList)) return null; |
|||
|
|||
return formList.reduce((prev, cur) => { |
|||
if (cur.key) prev[cur.key] = getDefaultValue(cur); |
|||
|
|||
return prev; |
|||
}, {}); |
|||
} |
@ -0,0 +1,3 @@ |
|||
export function resolveName(name = "input") { |
|||
return name.replace(/^[a-z]/, (word) => word.toUpperCase()); |
|||
} |
Loading…
Reference in new issue