@olapat/react-useform
Version:
react hook useform controller
346 lines (345 loc) • 13.4 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var actions_1 = require("./actions");
var useCheckValidate_1 = __importStar(require("./useCheckValidate"));
function reducerValues(state, action) {
var _a;
switch (action.type) {
case actions_1.actionTypes.MAIN.CHANGE:
return __assign(__assign({}, state), (_a = {}, _a[action.key || ''] = action.value, _a));
case actions_1.actionTypes.MAIN.SET:
return __assign(__assign({}, state), action.payload);
case actions_1.actionTypes.MAIN.RESET:
return __assign({}, action.payload);
default:
return state;
}
}
function reducerRules(state, action) {
var _a;
switch (action.type) {
case actions_1.actionTypes.MAIN.CHANGE:
return __assign(__assign({}, state), (_a = {}, _a[action.key || ''] = action.value, _a));
case actions_1.actionTypes.MAIN.SET:
return __assign(__assign({}, state), action.payload);
case actions_1.actionTypes.MAIN.RESET:
return __assign({}, action.payload);
default:
return state;
}
}
function reducerError(state, action) {
var _a;
switch (action.type) {
case actions_1.actionTypes.MAIN.CHANGE:
return __assign(__assign({}, state), (_a = {}, _a[action.key || ''] = action.value, _a));
case actions_1.actionTypes.MAIN.SET:
return __assign(__assign({}, state), action.payload);
case actions_1.actionTypes.MAIN.RESET:
return __assign({}, action.payload);
default:
return state;
}
}
var initialErrors = {};
var useForm = function (props) {
var initialValues = props.initialValues, initialRules = props.rules, blackList = props.blackList, whiteList = props.whiteList, onValuesUpdate = props.onValuesUpdate;
var _a = React.useReducer(reducerValues, initialValues), values = _a[0], dispatchValues = _a[1];
var _b = React.useState(false), submitting = _b[0], setSubmitting = _b[1];
var _c = React.useState(false), submitted = _c[0], setSubmitted = _c[1];
var _d = React.useReducer(reducerRules, initialRules), rules = _d[0], dispatchRules = _d[1];
var _e = React.useReducer(reducerError, initialErrors), errors = _e[0], dispatchErrors = _e[1];
var checkValidate = (0, useCheckValidate_1.default)();
var setValues = React.useCallback(function (a1) {
if (typeof a1 === 'function') {
var newValues = a1(values);
dispatchValues({
type: actions_1.actionTypes.MAIN.SET,
payload: newValues
});
}
else {
dispatchValues({
type: actions_1.actionTypes.MAIN.SET,
payload: a1
});
}
}, [values]);
var setRules = React.useCallback(function (a1) {
var newRules = {};
if (typeof a1 === 'function') {
newRules = a1(rules);
dispatchRules({
type: actions_1.actionTypes.MAIN.SET,
payload: newRules
});
var newErrors_1 = {};
for (var key in newRules) {
if (Object.prototype.hasOwnProperty.call(newRules, key)) {
newErrors_1[key] = undefined;
}
}
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: newErrors_1
});
}
else {
newRules = a1;
dispatchRules({
type: actions_1.actionTypes.MAIN.SET,
payload: a1
});
}
var newErrors = {};
for (var key in newRules) {
if (Object.prototype.hasOwnProperty.call(newRules, key)) {
newErrors[key] = undefined;
}
}
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: newErrors
});
}, [rules]);
var setErrors = React.useCallback(function (a1) {
if (typeof a1 === 'function') {
var newErrors = a1(errors);
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: newErrors
});
}
else {
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: a1
});
}
}, [errors]);
var validate = React.useCallback(function (next, end, getErrorArray, onSubmitError) {
setSubmitted(true);
var errors = {};
if (rules) {
errors = checkValidate(values, rules);
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: errors
});
}
if (rules && Object.keys(errors).length) {
setSubmitting(false);
var firstId = Object.keys(errors)[0];
var ele = document.getElementById(firstId);
var returner = false;
if (ele) {
var typeEle = ele.getAttribute('type');
if (!!ele.focus && (typeEle !== 'checkbox' && typeEle !== 'radio') && typeEle !== null) {
ele.focus();
}
else {
ele.scrollIntoView({ block: 'center' });
}
}
if (typeof end === 'function')
end();
if (getErrorArray === true) {
returner = [false, errors, values];
}
else {
returner = false;
}
if (typeof onSubmitError === 'function')
onSubmitError(errors);
return returner;
}
if (typeof next === 'function')
next(values);
if (getErrorArray === true) {
return [true, errors, values];
}
else {
return true;
}
}, [rules, values, checkValidate]);
var handlerChange = React.useCallback(function (a1, value, type) {
var _a, _b, _c;
var _d, _e, _f, _g;
if (type === void 0) { type = 'string'; }
if (typeof a1 === 'function') {
var newValues = a1(values);
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: errors
});
dispatchValues({
type: actions_1.actionTypes.MAIN.SET,
payload: newValues
});
if (typeof onValuesUpdate === 'function') {
onValuesUpdate(__assign(__assign({}, values), newValues));
}
}
else if (typeof a1 === 'object') {
var event_1 = a1;
if (((_d = event_1 === null || event_1 === void 0 ? void 0 : event_1.target) === null || _d === void 0 ? void 0 : _d.name) && ((_e = event_1 === null || event_1 === void 0 ? void 0 : event_1.target) === null || _e === void 0 ? void 0 : _e.value)) {
var inputName = (_f = event_1 === null || event_1 === void 0 ? void 0 : event_1.target) === null || _f === void 0 ? void 0 : _f.name;
var inputValue = (_g = event_1 === null || event_1 === void 0 ? void 0 : event_1.target) === null || _g === void 0 ? void 0 : _g.value;
dispatchErrors({
type: actions_1.actionTypes.MAIN.CHANGE,
key: inputName,
value: (0, useCheckValidate_1.checkErr)(rules[inputName], inputValue, inputName, values)
});
dispatchValues({
type: actions_1.actionTypes.MAIN.CHANGE,
key: inputName,
value: inputValue
});
if (typeof onValuesUpdate === 'function') {
onValuesUpdate(__assign(__assign({}, values), (_a = {}, _a[inputName] = inputValue, _a)));
}
}
else {
var newValues = a1;
if (submitted && rules) {
var errors_1 = {};
for (var name_1 in newValues) {
var value_1 = newValues[name_1];
errors_1[name_1] = (0, useCheckValidate_1.checkErr)(rules[name_1], value_1, name_1, values);
}
dispatchErrors({
type: actions_1.actionTypes.MAIN.SET,
payload: errors_1
});
}
dispatchValues({
type: actions_1.actionTypes.MAIN.SET,
payload: newValues
});
if (typeof onValuesUpdate === 'function') {
onValuesUpdate(__assign(__assign({}, values), newValues));
}
}
}
else {
var name_2 = a1;
if (rules && Object.keys(errors).length && rules[name_2] && submitted) {
dispatchErrors({
type: actions_1.actionTypes.MAIN.CHANGE,
key: name_2,
value: (0, useCheckValidate_1.checkErr)(rules[name_2], value, name_2, values)
});
}
else if (errors[name_2]) {
dispatchErrors({
type: actions_1.actionTypes.MAIN.CHANGE,
key: name_2,
value: undefined
});
}
switch (type) {
case 'string': {
dispatchValues({
type: actions_1.actionTypes.MAIN.CHANGE,
key: name_2,
value: value
});
if (typeof onValuesUpdate === 'function') {
onValuesUpdate(__assign(__assign({}, values), (_b = {}, _b[name_2] = value, _b)));
}
break;
}
case 'number': {
var notNumValue = /\D/.exec(value);
if (!(notNumValue && notNumValue[0]) || value === '') {
dispatchValues({
type: actions_1.actionTypes.MAIN.CHANGE,
key: name_2,
value: value
});
}
if (typeof onValuesUpdate === 'function') {
onValuesUpdate(__assign(__assign({}, values), (_c = {}, _c[name_2] = value, _c)));
}
break;
}
default:
break;
}
}
}, [errors, rules, submitted, values, onValuesUpdate]);
var handlerReset = React.useCallback(function (valuesReset) {
if (valuesReset === void 0) { valuesReset = {}; }
dispatchValues({
type: actions_1.actionTypes.MAIN.RESET,
payload: valuesReset
});
dispatchErrors({
type: actions_1.actionTypes.MAIN.RESET,
payload: {}
});
}, []);
return {
initialValues: initialValues,
values: values,
submitting: submitting,
setSubmitting: setSubmitting,
rules: rules,
errors: errors,
setValues: setValues,
validate: validate,
setRules: setRules,
setErrors: setErrors,
handlerReset: handlerReset,
handlerChange: handlerChange,
blackList: blackList,
whiteList: whiteList,
submitted: submitted
};
};
exports.default = useForm;