UNPKG

@hookform/lenses

Version:

Type-safe lenses for React Hook Form that enable precise control over nested form state. Build reusable form components with composable operations, array handling, and full TypeScript support.

48 lines 1.77 kB
// src/rhf/useFieldArray.ts import { set, useFieldArray as useFieldArrayOriginal } from "react-hook-form"; function useFieldArray(props) { const result = useFieldArrayOriginal(props); const transformOnSet = (value) => { if (!props.lens || !props.lens.settings.lensesMap || !value) { return value; } const newValue = {}; Object.entries(value || {}).forEach(([key, value2]) => { var _a, _b, _c, _d; const restructuredLens = (_b = (_a = props.lens.settings.lensesMap) == null ? void 0 : _a[0]) == null ? void 0 : _b[key]; const newKey = (_d = restructuredLens == null ? void 0 : restructuredLens.settings.propPath) == null ? void 0 : _d.slice(`${(_c = props.lens) == null ? void 0 : _c.settings.restructureSourcePath}.`.length); set(newValue, newKey, value2); }); return newValue; }; return { ...result, prepend: (value, options) => { const newValue = Array.isArray(value) ? value.map(transformOnSet) : transformOnSet(value); result.prepend(newValue, options); }, append: (value, options) => { const newValue = Array.isArray(value) ? value.map(transformOnSet) : transformOnSet(value); result.append(newValue, options); }, insert: (index, value, options) => { const newValue = Array.isArray(value) ? value.map(transformOnSet) : transformOnSet(value); result.insert(index, newValue, options); }, update: (index, value) => { const newValue = transformOnSet(value); result.update(index, newValue); }, replace: (value) => { const newValue = Array.isArray(value) ? value.map(transformOnSet) : transformOnSet(value); result.replace(newValue); } }; } export { useFieldArray }; //# sourceMappingURL=index.js.map