UNPKG

@ducor/react

Version:

admin template ui interface

109 lines (108 loc) 3.56 kB
import { useContext, useEffect, useMemo, useState } from "react"; import { FormContext } from "./components/form"; import { ScopeContext } from "./components/form/scope"; export function useDebounceEffect(value, delay) { var _a = useState(value), debouncedValue = _a[0], setDebouncedValue = _a[1]; useEffect(function () { var handler = setTimeout(function () { setDebouncedValue(value); }, delay); return function () { clearTimeout(handler); }; }, [value, delay]); return debouncedValue; } /** * * @returns */ export var useForm = function () { var context = useContext(FormContext); if (!context) { return { submiting: false, processing: false, value: "", error: [], }; } return context; }; /** * * @param string name * @returns */ export var useField = function (name, defaultValue) { if (defaultValue === void 0) { defaultValue = ""; } if (typeof name !== "string") { throw new Error("Invalid from field name: " + name); } // contexts var formCtx = useContext(FormContext); console.log("formCtx", formCtx); var data = { method: undefined, processing: false, isDirty: false, defaultValue: undefined, value: "", setValue: function () { }, error: [], setError: function () { }, id: "", name: 'ok', }; if (!formCtx) { return data; } var scopeCtx = useContext(ScopeContext); /* =============== start get name and id ======================*/ // Use memo to ensure fieldMap isn't recreated on each render var localFieldMap = useMemo(function () { return (scopeCtx === null || scopeCtx === void 0 ? void 0 : scopeCtx.fieldMap) || new Map(); }, [scopeCtx === null || scopeCtx === void 0 ? void 0 : scopeCtx.fieldMap]); /* =============== end get name and id ======================*/ // local filed data // scope name is address // email ~= address.email var absolateName = localFieldMap.get(name); var relativeName = name; console.log("formCtx.fieldsMap", formCtx); if (typeof absolateName === 'undefined') { absolateName = (scopeCtx !== undefined ? "".concat(scopeCtx.scope, ".").concat(name) : name); // set default value check or other // data = formCtx.fieldsMap.set(absolateName, { // ...data, // id: uuidv4(), // names: { // name: name, // absolateName, // relativeName, // } // // defaultValue: // }); } else { // data = formCtx.fieldsMap.get(absolateName); } var error = typeof formCtx.errors[relativeName] === "object" && Array.isArray(formCtx.errors[relativeName]) ? formCtx.errors[relativeName] : []; var hasError = error && error.length > 0; return { processing: formCtx.processing, isDirty: formCtx.isDirty, method: formCtx.method, defaultValue: formCtx.defaults[name] ? formCtx.defaults[name] : undefined, value: formCtx.values[name] || "", absolateName: absolateName, hasError: hasError, error: error, setValue: function (name, value) { return formCtx.setValue(name, value); }, //es version workning code ok pore fixed kore nibo setError: function (name, value) { return formCtx.setError(name, value); }, // ...data, }; };