@undermuz/react-json-form
Version:
Generate JSON-based forms with react
161 lines (159 loc) • 5.31 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/flat-form/useFlatRef.ts
var useFlatRef_exports = {};
__export(useFlatRef_exports, {
hasErrors: () => hasErrors,
useFlatRef: () => useFlatRef
});
module.exports = __toCommonJS(useFlatRef_exports);
var import_react = require("react");
var hasErrors = (errors) => errors !== null && errors !== void 0 && Object.keys(errors).length > 0;
var useFlatRef = (id, ref, form, childFormsRef) => {
(0, import_react.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);
};
}, []);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
hasErrors,
useFlatRef
});