UNPKG

@hitachivantara/uikit-react-core

Version:
59 lines (58 loc) 2.17 kB
import { HvFormElementContext, HvFormElementDescriptorsContext, HvFormElementValueContext } from "./context.js"; import { useUniqueId } from "../hooks/useUniqueId.js"; import { useClasses } from "./FormElement.styles.js"; import { findDescriptors } from "./utils.js"; import { useDefaultProps } from "@hitachivantara/uikit-react-utils"; import { useMemo } from "react"; import { jsx } from "react/jsx-runtime"; //#region src/FormElement/FormElement.tsx /** * Provides form-related context (ie. required/disabled/readOnly) for building form components, * analogous to MUI's [`FormControl`](https://mui.com/material-ui/api/form-control/) component. * * It is used internally to build UI Kit's form components (eg. `HvInput`, `HvDatePicker`), and can be used to build custom form components. * * It exposes the common properties to be shared between all form components: `required`, `disabled`, `readOnly`, and `status`. * * Along with the properties above, form components also share the `value`/`defaultValue` and `onChange` props, * used to control the value of the form component, analogous to the native `input` component. */ var HvFormElement = (props) => { const { classes: classesProp, className, children, id: idProp, name, value, disabled, required, readOnly, status = "standBy", ...others } = useDefaultProps("HvFormElement", props); const { classes, cx } = useClasses(classesProp); const id = useUniqueId(idProp); const contextValue = useMemo(() => ({ id, name, status, disabled, required, readOnly }), [ id, name, status, disabled, required, readOnly ]); const descriptors = useMemo(() => findDescriptors(children), [children]); return /* @__PURE__ */ jsx("div", { id, className: cx(classes.root, className), ...others, children: /* @__PURE__ */ jsx(HvFormElementContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(HvFormElementValueContext.Provider, { value, children: /* @__PURE__ */ jsx(HvFormElementDescriptorsContext.Provider, { value: descriptors, children }) }) }) }); }; HvFormElement.formElementType = "formelement"; //#endregion export { HvFormElement };