UNPKG

@wakflo/informed

Version:

A lightweight framework and utility for building powerful forms in React applications

78 lines (72 loc) 2.81 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); var useFormApi = require('./useFormApi.js'); // eslint-disable-next-line no-unused-vars var buildScopedFormApi = function buildScopedFormApi(scope, formApi) { return { getValue: function getValue(field) { return formApi.getValue("".concat(scope, ".").concat(field)); }, setValue: function setValue(field, value, e, key) { return formApi.setValue("".concat(scope, ".").concat(field), value, e, key); }, getTouched: function getTouched(field) { return formApi.getTouched("".concat(scope, ".").concat(field)); }, setTouched: function setTouched(field, value) { return formApi.setTouched("".concat(scope, ".").concat(field), value); }, getFocused: function getFocused(field) { return formApi.getFocused("".concat(scope, ".").concat(field)); }, setFocused: function setFocused(field, value) { return formApi.setFocused("".concat(scope, ".").concat(field), value); }, getError: function getError(field) { return formApi.getError("".concat(scope, ".").concat(field)); }, setError: function setError(field, value) { return formApi.setError("".concat(scope, ".").concat(field), value); }, getData: function getData(field) { return formApi.getData("".concat(scope, ".").concat(field)); }, getModified: function getModified(field) { return formApi.getModified("".concat(scope, ".").concat(field)); }, resetField: function resetField(field) { return formApi.resetField("".concat(scope, ".").concat(field)); }, validate: function validate(field) { return formApi.validate("".concat(scope, ".").concat(field)); }, getDirty: function getDirty(field) { return formApi.getDirty("".concat(scope, ".").concat(field)); }, getPristine: function getPristine(field) { return formApi.getPristine("".concat(scope, ".").concat(field)); }, getMaskedValue: function getMaskedValue(field) { return formApi.getMaskedValue("".concat(scope, ".").concat(field)); }, clearValue: function clearValue(field) { return formApi.clearValue("".concat(scope, ".").concat(field)); }, clearError: function clearError(field) { return formApi.clearError("".concat(scope, ".").concat(field)); }, restore: function restore(field) { return formApi.restore("".concat(scope, ".").concat(field)); } }; }; function useScopedApi(scope) { var formApi = useFormApi.useFormApi(); // VERY important to memoize the builder! var scopedFormApi = React.useMemo(function () { return buildScopedFormApi(scope, formApi); }, [scope]); return scopedFormApi; } exports.useScopedApi = useScopedApi;