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.

72 lines (70 loc) 2.83 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/rhf/index.ts var rhf_exports = {}; __export(rhf_exports, { useFieldArray: () => useFieldArray }); module.exports = __toCommonJS(rhf_exports); // src/rhf/useFieldArray.ts var import_react_hook_form = require("react-hook-form"); function useFieldArray(props) { const result = (0, import_react_hook_form.useFieldArray)(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); (0, import_react_hook_form.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); } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useFieldArray }); //# sourceMappingURL=index.cjs.map