@undermuz/use-form
Version:
React library for build forms
96 lines (94 loc) • 3.26 kB
JavaScript
;
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/useForm/middlewares/validate.ts
var validate_exports = {};
__export(validate_exports, {
createValidating: () => createValidating,
getFormErrors: () => getFormErrors
});
module.exports = __toCommonJS(validate_exports);
var import_reducer = require("../reducer.cjs");
var getFormErrors = (state) => {
const { tests, touched, values } = state;
const errors = {};
for (let index = 0; index < tests.length; index++) {
const [names, testList, errorText = "Invalid"] = tests[index];
for (const name of names) {
if (!touched.includes(name)) {
continue;
}
for (const valueTest of testList) {
if (valueTest(values[name], values)) {
continue;
}
if (!errors[name]) {
errors[name] = [];
}
const fieldErrors = errors[name];
fieldErrors.push(errorText);
break;
}
}
}
return errors;
};
var getValidateFunction = (action, store) => {
const { validate: customValidate = null } = action.payload;
const state = store.getState();
return customValidate != null ? customValidate : state.validate;
};
var validateEffect = (action, store, settings = {}) => {
const { silent = false, checkOnlyFilled = true } = action;
const { debug = false } = settings;
const triggers = [
import_reducer.FORM_ACTIONS.SET_VALUE,
import_reducer.FORM_ACTIONS.SET_VALUES,
import_reducer.FORM_ACTIONS.SET_TESTS,
import_reducer.FORM_ACTIONS.SET_TOUCHED_FIELD,
import_reducer.FORM_ACTIONS.SET_TOUCHED,
import_reducer.FORM_ACTIONS.SET_FIELDS,
import_reducer.FORM_ACTIONS.SET_VALIDATE,
import_reducer.FORM_ACTIONS.VALIDATE_FORM
];
if (!triggers.some((t) => action.type === t) || silent) {
return;
}
const state = store.getState();
const validate = getValidateFunction(action, store);
const validatedTouched = checkOnlyFilled ? state.touched : Object.keys(state.fields);
const validateState = {
...state,
touched: validatedTouched
};
const newErrors = validate(validateState, debug);
store.dispatch({
type: import_reducer.FORM_ACTIONS.SET_ERRORS,
payload: { errors: newErrors }
});
};
var createValidating = (settings = {}) => (store) => (next) => (action) => {
const result = next(action);
validateEffect(action, store, settings);
return result;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createValidating,
getFormErrors
});