@undermuz/react-json-form
Version:
Generate JSON-based forms with react
122 lines (120 loc) • 4.17 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/flat-form/FlatForm.tsx
var FlatForm_exports = {};
__export(FlatForm_exports, {
FlatFormContext: () => FlatFormContext,
default: () => FlatForm_default
});
module.exports = __toCommonJS(FlatForm_exports);
var import_react = require("react");
var import_use_form = __toESM(require("@undermuz/use-form"));
var import_types = require("../types.js");
var import_utils = require("../utils.js");
var import_useFlatRef = require("./useFlatRef.js");
var import_FieldsBlock = require("./FieldsBlock.js");
var import_jsx_runtime = require("react/jsx-runtime");
var FlatFormContext = (0, import_react.createContext)({
scheme: [],
isShow: true,
isFormPrimary: true,
isLoading: false,
level: 1
});
var FlatForm = (0, import_react.forwardRef)(
(props, ref) => {
const {
id,
scheme,
value,
level,
children,
isShow = true,
isLoading = false,
primary = false,
tests,
onChange,
onError
} = props;
const fieldsScheme = (0, import_utils.useFieldsScheme)(scheme);
const formConfig = (0, import_utils.useSchemeToForm)({
scheme: fieldsScheme,
value,
tests,
onChange,
onError
});
const form = (0, import_use_form.default)(formConfig);
const childFormsRef = (0, import_react.useRef)({});
const onChildRef = (0, import_react.useCallback)(
(params) => {
if (!params.ref) {
delete childFormsRef.current[params.id];
return;
}
childFormsRef.current[params.id] = params.ref;
},
[]
);
(0, import_react.useEffect)(() => {
let shouldUpdate = false;
const new_value = {};
for (const schemeItem of fieldsScheme) {
const { name, type = import_types.EnumSchemeItemType.Text } = schemeItem;
const def_value = (0, import_utils.getDefValueForItem)(schemeItem);
if (value[name] !== void 0 || type === import_types.EnumSchemeItemType.Checkbox) {
continue;
}
new_value[name] = def_value;
shouldUpdate = true;
}
if (shouldUpdate)
onChange({ ...value, ...new_value });
}, []);
(0, import_useFlatRef.useFlatRef)(id, ref, form, childFormsRef);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_use_form.FormContext.Provider, { value: form, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_FieldsBlock.FieldsBlock,
{
level,
isShow,
isLoading,
scheme,
isFormPrimary: primary,
onFormsRef: onChildRef,
children
}
) });
}
);
FlatForm.displayName = "FlatForm";
var FlatForm_default = FlatForm;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FlatFormContext
});