react-formal
Version:
Classy HTML form management for React
43 lines (39 loc) • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _Contexts = require("./Contexts");
var _memoizeOne = _interopRequireDefault(require("memoize-one"));
var _Errors = require("./Errors");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Returns all errors for the form.
*
* @returns {Touched}
*/
/**
* Returns the current Field value at the provided path.
*
* @param path a path to retrieve errors for.
* @returns {Touched}
*/
/**
* Returns an array of values for the provided field paths.
*
* @param paths a set of paths to retrieve errors for.
* @returns {Touched}
*/
function useTouched(paths) {
const {
touched
} = (0, _Contexts.useFormContext)(_Contexts.BITS.touched);
const memoFilterAndMapErrors = (0, _react.useMemo)(() => (0, _memoizeOne.default)(_Errors.filterAndMapErrors, ([a], [b]) => a.errors === b.errors && a.names === b.names && a.mapErrors === b.mapErrors), []);
return paths ? memoFilterAndMapErrors({
errors: touched,
names: paths
}) : touched;
}
var _default = useTouched;
exports.default = _default;