informed
Version:
A lightweight framework and utility for building powerful forms in React applications
78 lines (75 loc) • 2.31 kB
JavaScript
import { useMemo } from 'react';
import { useFormApi } from './useFormApi.js';
import { useScope } from './useScope.js';
/* ----------------------- useFieldApi ----------------------- */
var buildFieldApi = function buildFieldApi(formApi, name) {
return {
getValue: function getValue() {
return formApi.getValue(name);
},
setValue: function setValue(value, e, key) {
return formApi.setValue(name, value, e, key);
},
getTouched: function getTouched() {
return formApi.getTouched(name);
},
setTouched: function setTouched(value, e) {
return formApi.setTouched(name, value, e);
},
getError: function getError() {
return formApi.getError(name);
},
setError: function setError(value) {
return formApi.setError(name, value);
},
getFocused: function getFocused() {
return formApi.getFocused(name);
},
setFocused: function setFocused(value, e) {
return formApi.setFocused(name, value, e);
},
getData: function getData() {
return formApi.getData(name);
},
getModified: function getModified() {
return formApi.getModified(name);
},
reset: function reset(options) {
return formApi.resetField(name, options);
},
validate: function validate() {
return formApi.validateField(name);
},
getDirty: function getDirty() {
return formApi.getDirty(name);
},
getPristine: function getPristine() {
return formApi.getPristine(name);
},
getMaskedValue: function getMaskedValue() {
return formApi.getMaskedValue(name);
},
clearValue: function clearValue() {
return formApi.clearValue(name);
},
setValueQuietly: function setValueQuietly(value) {
return formApi.setValueQuietly(name, value);
},
restore: function restore(value) {
return formApi.restore(name, value);
},
getMemory: function getMemory() {
return formApi.getMemory(name);
}
};
};
function useFieldApi(n) {
var scoped = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var formApi = useFormApi();
var name = scoped ? useScope(n) : n;
var fieldApi = useMemo(function () {
return buildFieldApi(formApi, name);
}, [name]);
return fieldApi;
}
export { useFieldApi };