@undermuz/react-json-form
Version:
Generate JSON-based forms with react
74 lines (73 loc) • 1.33 kB
JavaScript
// src/Form.tsx
import { forwardRef, useCallback } from "react";
import FlatForm from "./flat-form/FlatForm.mjs";
import ArrayForm from "./array-form/ArrayForm.mjs";
import { jsx } from "react/jsx-runtime";
var Form = forwardRef((props, ref) => {
const {
id,
viewType,
level = 1,
multiple = false,
isLoading = false,
primary = true,
fillArrayDefault = true,
scheme = [],
children,
errors,
def,
value,
onChange,
onError
} = props;
const changeFlat = useCallback(
(newValue) => {
onChange({ ...value, ...newValue });
},
[value, onChange]
);
const setErrorsFlat = useCallback(
(newErrors) => {
onError(newErrors);
},
[errors, onError]
);
const rest = {
id,
level,
primary,
scheme,
children
};
if (!multiple)
return /* @__PURE__ */ jsx(
FlatForm,
{
...rest,
ref,
value,
isLoading,
onChange: changeFlat,
onError: setErrorsFlat
}
);
return /* @__PURE__ */ jsx(
ArrayForm,
{
...rest,
ref,
fillArrayDefault,
viewType,
errors,
defValue: def,
value,
onChange,
onError
}
);
});
Form.displayName = "Form";
var Form_default = Form;
export {
Form_default as default
};