UNPKG

@mijadesign/mjui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

188 lines (187 loc) 5.81 kB
import { FormItem, Form } from "@nutui/nutui-react-taro"; import { a as __awaiter } from "./tslib.es6-iWu3F_1J.js"; import { useRef } from "react"; import Schema from "async-validator"; const SECRET = "NUT_FORM_INTERNAL"; class FormStore { constructor() { this.initialValues = {}; this.updateList = []; this.store = {}; this.fieldEntities = []; this.callbacks = {}; this.errors = {}; this.registerField = (field) => { this.fieldEntities.push(field); return () => { this.fieldEntities = this.fieldEntities.filter((item) => item !== field); if (this.store) { delete this.store[field.props.name]; } }; }; this.getFieldValue = (name) => { var _a; return (_a = this.store) === null || _a === void 0 ? void 0 : _a[name]; }; this.getFieldsValue = (nameList) => { if (typeof nameList === "boolean") { return JSON.parse(JSON.stringify(this.store)); } const fieldsValue = {}; nameList.forEach((field) => { fieldsValue[field] = this.getFieldValue(field); }); return fieldsValue; }; this.setInitialValues = (values, init) => { if (init) { this.initialValues = values; this.store = values; } }; this.setFieldsValue = (newStore) => { this.store = Object.assign(Object.assign({}, this.store), newStore); this.fieldEntities.forEach((entity) => { const { name } = entity.props; Object.keys(newStore).forEach((key) => { if (key === name) { entity.onStoreChange("update"); } }); }); this.updateList.forEach((item) => { let shouldUpdate = item.condition; if (typeof item.condition === "function") { shouldUpdate = item.condition(); } if (shouldUpdate) { item.entity.onStoreChange("update"); } }); }; this.setCallback = (callback) => { this.callbacks = Object.assign(Object.assign({}, this.callbacks), callback); }; this.validateEntities = (entity, errs) => __awaiter(this, void 0, void 0, function* () { var _a; const { name, rules = [] } = entity.props; const descriptor = {}; if (rules.length) { if (rules.length > 1) { descriptor[name] = []; rules.forEach((v) => { descriptor[name].push(v); }); } else { descriptor[name] = rules[0]; } } const validator = new Schema(descriptor); try { yield validator.validate({ [name]: (_a = this.store) === null || _a === void 0 ? void 0 : _a[name] }); } catch ({ errors }) { if (errors) { errs.push(...errors); this.errors[name] = errors; } } finally { if (!errs || errs.length === 0) { this.errors[name] = []; } } entity.onStoreChange("validate"); }); this.validateFields = (nameList) => __awaiter(this, void 0, void 0, function* () { let filterEntities = []; this.errors.length = 0; if (!nameList || nameList.length === 0) { filterEntities = this.fieldEntities; } else { filterEntities = this.fieldEntities.filter(({ props: { name } }) => nameList.includes(name)); } const errs = []; yield Promise.all(filterEntities.map((entity) => __awaiter(this, void 0, void 0, function* () { yield this.validateEntities(entity, errs); }))); return errs; }); this.submit = () => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const errors = yield this.validateFields(); if (errors.length === 0) { (_b = (_a = this.callbacks).onFinish) === null || _b === void 0 ? void 0 : _b.call(_a, this.store); } else if (errors.length > 0) { (_d = (_c = this.callbacks).onFinishFailed) === null || _d === void 0 ? void 0 : _d.call(_c, this.store, errors); } }); this.resetFields = () => { this.errors.length = 0; this.store = this.initialValues; this.fieldEntities.forEach((entity) => { entity.onStoreChange("reset"); }); }; this.registerUpdate = (field, shouldUpdate) => { this.updateList.push({ entity: field, condition: shouldUpdate }); return () => { this.updateList = this.updateList.filter((i) => i.entity !== field); }; }; this.dispatch = ({ name }) => { this.validateFields([name]); }; this.getInternal = (key) => { if (key === SECRET) { return { registerField: this.registerField, setCallback: this.setCallback, setInitialValues: this.setInitialValues, dispatch: this.dispatch, store: this.store, fieldEntities: this.fieldEntities, registerUpdate: this.registerUpdate }; } }; this.getForm = () => { return { getFieldValue: this.getFieldValue, getFieldsValue: this.getFieldsValue, setFieldsValue: this.setFieldsValue, resetFields: this.resetFields, validateFields: this.validateFields, submit: this.submit, errors: this.errors, getInternal: this.getInternal }; }; this.callbacks = { onFinish: () => { }, onFinishFailed: () => { } }; } } const useForm = (form) => { const formRef = useRef(); if (!formRef.current) { if (form) { formRef.current = form; } else { const formStore = new FormStore(); formRef.current = formStore.getForm(); } } return [formRef.current]; }; const InnerForm = Form; InnerForm.Item = FormItem; InnerForm.useForm = useForm; export { InnerForm as default };