UNPKG

@undermuz/use-form

Version:
39 lines (38 loc) 1.02 kB
// src/useForm/useFormConfigBySettings.ts import { useMemo } from "react"; var useFormConfigBySettings = (props) => { return useMemo(() => { const _config = { initialValues: props.value ? props.value : {}, valueTests: [], fields: {}, ...props.options || {} }; Object.keys(props.fields).forEach((fieldName) => { var _a; const _field = props.fields[fieldName]; let field; if (typeof _field === "string") { field = { label: _field, initialValue: void 0, rules: [] }; } else { field = _field; } _config.fields[fieldName] = field.label || fieldName; if (!props.value) _config.initialValues[fieldName] = field.initialValue; if ((_a = field.rules) == null ? void 0 : _a.length) { field.rules.forEach((rule) => { _config.valueTests.push([[fieldName], ...rule]); }); } }); return _config; }, []); }; export { useFormConfigBySettings };