apollo-form
Version:
Form state manager
111 lines (110 loc) • 4.43 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const merge_1 = __importDefault(require("lodash/merge"));
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
const get_1 = __importDefault(require("lodash/get"));
const set_1 = __importDefault(require("lodash/set"));
const isEqual_1 = __importDefault(require("lodash/isEqual"));
const utils_1 = require("../utils");
class StateManipulator {
constructor(props) {
this.validateHandler = props.validate;
this.validationSchema = props.validationSchema;
this.customValidators = props.customValidators;
this.initialState = props.initialState;
this.initialErrors = props.initialErrors;
this.initialTouches = props.initialTouches;
this.validateOnMount = props.validateOnMount;
this.defaultState = props.defaultState;
}
setValue(state, key, newValue) {
const value = get_1.default(state.values, key);
if (!isEqual_1.default(value, newValue)) {
set_1.default(state.values, key, newValue);
}
return state;
}
setError(state, key, value) {
const error = utils_1.getDeepStatus(state.errors, key);
if (error !== value) {
utils_1.setDeepStatus(state.errors, key, value);
}
return state;
}
setTouched(state, key, value) {
const touched = utils_1.getDeepStatus(state.touches, key);
if (touched !== value) {
utils_1.setDeepStatus(state.touches, key, value);
}
return state;
}
getValue(state, key) {
return get_1.default(state.values, key);
}
getError(state, key) {
return utils_1.getDeepStatus(cloneDeep_1.default(state.errors), key);
}
getTouched(state, key) {
const touched = utils_1.getDeepStatus(cloneDeep_1.default(state.touches), key);
return touched;
}
validate(state, allTouched = false) {
state.errors = {};
// merge errors from validate func
if (this.validateHandler) {
const customErrors = this.validateHandler(state);
merge_1.default(state.errors, customErrors);
}
// custom validators
for (const key in this.customValidators) {
if (!(key in state.errors)) {
const value = get_1.default(state.values, key);
const newError = this.customValidators[key](value);
if (newError) {
this.setError(state, key, newError);
}
}
}
// yup validate
if (this.validationSchema) {
try {
this.validationSchema.validateSync(state.values, {
recursive: true,
abortEarly: false,
});
}
catch (e) {
for (const err of e.inner) {
const path = err.path.replace('[', '.').replace(']', '');
if (!this.getError(state, path)) {
this.setError(state, path, err.message);
}
}
}
}
if (allTouched) {
state.touches = utils_1.replaceErrors(state.touches, state.errors, true);
}
const nextIsValid = !utils_1.firstError(state.errors);
state.isValid = nextIsValid;
return state;
}
reset(state, getState) {
if (getState) {
if (typeof getState === 'function') {
Object.assign(state, Object.assign(Object.assign({}, this.defaultState), { values: getState(state.values), errors: this.initialErrors, touches: this.initialTouches, submitCount: state.submitCount }));
}
else {
Object.assign(state, Object.assign(Object.assign({}, this.defaultState), { values: getState, errors: this.initialErrors, touches: this.initialTouches, submitCount: state.submitCount }));
}
}
else {
Object.assign(state, Object.assign(Object.assign({}, this.defaultState), { values: this.initialState, errors: this.initialErrors, touches: this.initialTouches, submitCount: state.submitCount }));
}
return state;
}
}
exports.default = StateManipulator;