UNPKG

@undermuz/react-json-form

Version:
109 lines (108 loc) 2.83 kB
// src/flat-form/FieldsBlock.tsx import { memo, Children, useMemo } from "react"; import { nonFieldTypes } from "../utils.mjs"; import JsonFormLayout from "../components/JsonFormLayout.mjs"; import FormField from "./FormField.mjs"; import FormItem from "./FormItem.mjs"; import { FlatFormContext } from "./FlatForm.mjs"; import { ErrorBoundary } from "react-error-boundary"; import ErrorFallback from "../components/ErrorFallback.mjs"; import { Fragment, jsx } from "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 = 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__ */ jsx(Fragment, { children: fields.map((schemeItem, index) => { const isField = schemeItem.type && !nonFieldTypes.includes(schemeItem.type); if (!isField) return /* @__PURE__ */ jsx( ErrorBoundary, { FallbackComponent: ErrorFallback, onReset: () => { }, children: /* @__PURE__ */ jsx( FormItem, { ...schemeItem, level, isLoading, isFormPrimary, isLast: index === scheme.length - 1 }, index ) }, index ); return /* @__PURE__ */ jsx( ErrorBoundary, { FallbackComponent: ErrorFallback, onReset: () => { }, children: /* @__PURE__ */ jsx( FormField, { ...schemeItem, onFormsRef, level, isFormPrimary, isLast: index === scheme.length - 1 } ) }, index ); }) }); }; var FieldsBlock = memo( (props) => { const { scheme, isFormPrimary, isLoading = false, level, children: _children, onFormsRef } = props; const value = useMemo(() => { const { children: children2, ...rest } = props; return rest; }, Object.values(props)); const count = Children.count(_children); const children = count > 0 ? _children : /* @__PURE__ */ jsx(JsonFormLayout.Form, { children: /* @__PURE__ */ jsx( FieldsList, { scheme, level, isLoading, isFormPrimary, onFormsRef } ) }); return /* @__PURE__ */ jsx(FlatFormContext.Provider, { value, children }); } ); FieldsBlock.displayName = "FieldsBlock"; export { FieldsBlock, FieldsList };