@undermuz/react-json-form
Version:
Generate JSON-based forms with react
151 lines (149 loc) • 6.08 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/components/JsonFormLayout.tsx
var JsonFormLayout_exports = {};
__export(JsonFormLayout_exports, {
JFL_ArrayFormItemName: () => JFL_ArrayFormItemName,
JFL_ArrayFormListName: () => JFL_ArrayFormListName,
JFL_FlatFormName: () => JFL_FlatFormName,
JFL_FormFieldName: () => JFL_FormFieldName,
JFL_FormFieldsName: () => JFL_FormFieldsName,
JFL_Nothing: () => JFL_Nothing,
default: () => JsonFormLayout_default
});
module.exports = __toCommonJS(JsonFormLayout_exports);
var import_react = require("react");
var import_ui = require("../contexts/ui.js");
var import_FlatForm = require("../flat-form/FlatForm.js");
var import_FieldsBlock = require("../flat-form/FieldsBlock.js");
var import_FormField = __toESM(require("../flat-form/FormField.js"));
var import_ArrayForm = require("../array-form/ArrayForm.js");
var import_ArrayFormItem = __toESM(require("../array-form/ArrayFormItem.js"));
var import_jsx_runtime = require("react/jsx-runtime");
var JFL_FlatFormName = "__JFL__FlatForm";
var JFL_Nothing = ({ children }) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
var JFL_Form = (props) => {
const Ui = (0, import_ui.useJsonFormUi)();
const FlatForm = Ui?.FlatForm ? Ui.FlatForm : JFL_Nothing;
const { as: Cmp = FlatForm, children: _children, ...rest } = props;
const value = (0, import_react.useContext)(import_FlatForm.FlatFormContext);
const cmpProps = (0, import_react.useMemo)(() => {
return {
isShow: value.isShow || true,
primary: value.isFormPrimary
};
}, [value]);
const count = import_react.Children.count(_children);
const children = count > 0 ? _children : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(JFL_FormFields, {});
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Cmp, { ...rest, ...cmpProps, children });
};
JFL_Form.displayName = JFL_FlatFormName;
var JFL_FormFieldsName = "__JFL__FlatFormFields";
var JFL_FormFields = (props) => {
const value = (0, import_react.useContext)(import_FlatForm.FlatFormContext);
const cmpProps = (0, import_react.useMemo)(() => {
return {
scheme: value.scheme,
isFormPrimary: value.isFormPrimary,
level: value.level,
onFormsRef: value.onFormsRef
};
}, [value]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_FieldsBlock.FieldsList, { ...props, ...cmpProps });
};
JFL_FormFields.displayName = JFL_FormFieldsName;
var JFL_FormFieldName = "__JFL__FlatFormField";
var JFL_FormField = ({ children, name, as, ...customProps }) => {
const value = (0, import_react.useContext)(import_FlatForm.FlatFormContext);
const schemeItem = (0, import_react.useMemo)(() => {
return value.scheme.find((s) => s.name === name);
}, [value.scheme, name]);
if (!schemeItem) {
throw new Error(`Cannot find scheme item by name: ${name}`);
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_FormField.default,
{
...schemeItem,
level: value.level,
isFormPrimary: value.isFormPrimary,
onFormsRef: value.onFormsRef,
...customProps,
as,
children
},
name
);
};
JFL_FormField.displayName = JFL_FormFieldName;
var JFL_ArrayFormItemName = "__JFL__ArrayFormItemName";
var JFL_ArrayFormItem = ({ itemId, itemIndex, ...other }) => {
const { value, changeTab, setTabErrors, ...rest } = (0, import_react.useContext)(import_ArrayForm.ArrayFormContext);
if (itemId === void 0 && itemIndex === void 0)
throw new Error(`itemId or itemIndex is required`);
const itemValue = value.find(
(item, index) => item.id === itemId || index === itemIndex
);
if (!itemValue)
throw new Error(`itemValue has not found by itemId or itemIndex`);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_ArrayFormItem.default,
{
...rest,
id: itemValue.id,
value: itemValue,
...other,
onChange: changeTab,
onError: setTabErrors
}
);
};
JFL_ArrayFormItem.displayName = JFL_ArrayFormItemName;
var JFL_ArrayFormListName = "__JFL__ArrayFormListName";
var JFL_ArrayFormList = ({ children }) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ArrayForm.ArrayFormContext.Consumer, { children });
};
JFL_ArrayFormList.displayName = JFL_ArrayFormListName;
var JsonFormLayout = {
Form: JFL_Form,
Fields: JFL_FormFields,
Field: JFL_FormField,
ArrayList: JFL_ArrayFormList,
ArrayItem: JFL_ArrayFormItem
};
var JsonFormLayout_default = JsonFormLayout;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
JFL_ArrayFormItemName,
JFL_ArrayFormListName,
JFL_FlatFormName,
JFL_FormFieldName,
JFL_FormFieldsName,
JFL_Nothing
});