@undermuz/react-json-form
Version:
Generate JSON-based forms with react
143 lines (141 loc) • 5 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/FieldsBlock.tsx
var FieldsBlock_exports = {};
__export(FieldsBlock_exports, {
FieldsBlock: () => FieldsBlock,
FieldsList: () => FieldsList
});
module.exports = __toCommonJS(FieldsBlock_exports);
var import_react = require("react");
var import_utils = require("../utils.js");
var import_JsonFormLayout = __toESM(require("../components/JsonFormLayout.js"));
var import_FormField = __toESM(require("./FormField.js"));
var import_FormItem = __toESM(require("./FormItem.js"));
var import_FlatForm = require("./FlatForm.js");
var import_react_error_boundary = require("react-error-boundary");
var import_ErrorFallback = __toESM(require("../components/ErrorFallback.js"));
var import_jsx_runtime = require("react/jsx-runtime");
var DEF_EXCEPT = [];
var FieldsList = (props) => {
const {
scheme,
isFormPrimary,
isLoading = false,
level,
except = DEF_EXCEPT,
include = DEF_EXCEPT,
onFormsRef
} = props;
const fields = (0, import_react.useMemo)(() => {
const fields2 = scheme;
if (include?.length > 0)
return fields2.filter((s) => include.includes(s.name));
if (except?.length > 0)
return fields2.filter((s) => !except.includes(s.name));
return fields2;
}, [except, scheme]);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fields.map((schemeItem, index) => {
const isField = schemeItem.type && !import_utils.nonFieldTypes.includes(schemeItem.type);
if (!isField)
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_react_error_boundary.ErrorBoundary,
{
FallbackComponent: import_ErrorFallback.default,
onReset: () => {
},
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_FormItem.default,
{
...schemeItem,
level,
isLoading,
isFormPrimary,
isLast: index === scheme.length - 1
},
index
)
},
index
);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_react_error_boundary.ErrorBoundary,
{
FallbackComponent: import_ErrorFallback.default,
onReset: () => {
},
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_FormField.default,
{
...schemeItem,
onFormsRef,
level,
isFormPrimary,
isLast: index === scheme.length - 1
}
)
},
index
);
}) });
};
var FieldsBlock = (0, import_react.memo)(
(props) => {
const {
scheme,
isFormPrimary,
isLoading = false,
level,
children: _children,
onFormsRef
} = props;
const value = (0, import_react.useMemo)(() => {
const { children: children2, ...rest } = props;
return rest;
}, Object.values(props));
const count = import_react.Children.count(_children);
const children = count > 0 ? _children : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_JsonFormLayout.default.Form, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
FieldsList,
{
scheme,
level,
isLoading,
isFormPrimary,
onFormsRef
}
) });
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_FlatForm.FlatFormContext.Provider, { value, children });
}
);
FieldsBlock.displayName = "FieldsBlock";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FieldsBlock,
FieldsList
});