UNPKG

@undermuz/react-json-form

Version:
135 lines (134 loc) 4.23 kB
// src/flat-form/useFlatRef.ts import { useEffect } from "react"; var hasErrors = (errors) => errors !== null && errors !== void 0 && Object.keys(errors).length > 0; var useFlatRef = (id, ref, form, childFormsRef) => { useEffect(() => { const setRef = (value) => { if (typeof ref === "function") { ref(value); } else if (ref !== null) { ref.current = value; } }; setRef({ setTouched(newTouched, silent, checkOnlyFilled) { const state = form.store.getState(); const fields = Object.keys(state.fields); form.setTouched(newTouched || fields, silent, checkOnlyFilled); const childRefs = Object.values(childFormsRef.current); const _setTouchedChild = (child) => { if (child === null) { console.warn( `[FlatForm: ${id}][ref][SetTouched][Child is null]`, childRefs ); return; } try { child.setTouched(newTouched, silent, checkOnlyFilled); } catch (e) { console.error( `[ERROR] [FlatForm: setTouched][Child]: ${e?.message}`, { item: child, refs: childRefs } ); console.error(e); } }; for (const childRef of childRefs) { if (!Array.isArray(childRef)) { _setTouchedChild(childRef); continue; } for (const child of childRef) { _setTouchedChild(child); } } }, validate(checkOnlyFilled = true, level = 0) { const prefixLogArr = []; for (let i = 0; i < level; i++) { prefixLogArr.push(" "); } const p = `${prefixLogArr.join("")}[Ref: ${id}][Validate]`; let [hasFormErrors, formErrors] = form.hasFormErrors(checkOnlyFilled); console.log(`${p}`, { checkOnlyFilled, hasErrors: hasFormErrors, errors: formErrors }); const childForms = childFormsRef.current; const childRefs = Object.entries(childForms); console.log(`${p}[Sub: ${childRefs.length}]`, childRefs); let childIndex = -1; for (const [childId, child] of childRefs) { childIndex++; if (!Array.isArray(child)) { const subFormErrors = child.validate( checkOnlyFilled, level + 1 ); const hasSubFormsErrors = hasErrors(subFormErrors); if (hasSubFormsErrors) { hasFormErrors = true; formErrors[childId] = subFormErrors; } console.log(`${p}[Child #${childIndex}]`, { hasErrors: hasSubFormsErrors, errors: subFormErrors }); continue; } let subChildIndex = -1; const subErrors = {}; for (const subChild of child) { subChildIndex++; if (subChild === null) { console.warn( `${p}[Sub #${childIndex}][Item: ${subChildIndex}][Error: Child is null]`, childRefs ); continue; } const subFormErrors = subChild.validate( checkOnlyFilled, level + 1 ); const hasSubFormsErrors = hasErrors(subFormErrors); if (hasSubFormsErrors) { hasFormErrors = true; subErrors[subChildIndex] = subFormErrors; } console.log( `${p}[Sub #${childIndex}][Item: #${subChildIndex}]`, { hasErrors: hasSubFormsErrors, errors: subFormErrors } ); } if (Object.keys(subErrors).length > 0) { formErrors[childId] = subErrors; } } if (hasFormErrors) { form.validate(checkOnlyFilled); return formErrors; } return null; }, values() { return form.getValues(); }, errors() { return form.getErrors(); } }); return () => { setRef(null); }; }, []); }; export { hasErrors, useFlatRef };