UNPKG

@nish1896/rhf-mui-components

Version:

A suite of 25+ production-ready react-hook-form components built with material-ui. Fully typed, tree-shakable, and optimized for enterprise-grade forms.

162 lines (161 loc) 6.36 kB
"use client"; import { RHFMuiConfigContext } from "../../config/ConfigProvider.js"; import { keepLabelAboveFormField, mergeRefs } from "../../utils/control.js"; import FormControl from "../../common/FormControl.js"; import FormHelperText from "../../common/FormHelperText.js"; import FormLabel from "../../common/FormLabel.js"; import FormLabelText from "../../common/FormLabelText.js"; import { fieldNameToLabel } from "../../utils/text-transform.js"; import { useFieldIds } from "../../utils/useFieldIds.js"; import { forwardRef, useCallback, useContext } from "react"; import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime"; import { Controller } from "react-hook-form"; import Autocomplete from "@mui/material/Autocomplete"; import TextField from "@mui/material/TextField"; import CircularProgress from "@mui/material/CircularProgress"; //#region src/mui/autocomplete-object/index.tsx /** * The component is designed to store complete option object(s) in form state. * * `freeSolo` is not supported in `RHFAutocompleteObject` as it would introduce * string values alongside objects (`Option | string`), making the field value * less predictable and type-safe. * * Use `RHFAutocomplete` instead when `freeSolo` behavior is required. */ const RHFAutocompleteObject = forwardRef(function RHFAutocompleteObject({ fieldName, control, registerOptions, options, multiple, labelKey, valueKey, disableClearable, autoHighlight = true, customOnChange, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, hideLabel, required, helperText, errorMessage, hideErrorMessage, formHelperTextProps, textFieldProps, slotProps, ChipProps, onBlur: muiOnBlur, onFocus, loading, limitTags = 2, getLimitTagsText, customIds, ...otherAutocompleteObjectProps }, ref) { const { allLabelsAboveFields } = useContext(RHFMuiConfigContext); const { fieldId, labelId, helperTextId, errorId } = useFieldIds(fieldName, customIds); const isLabelAboveFormField = keepLabelAboveFormField(showLabelAboveFormField, allLabelsAboveFields); const defaultFieldLabel = fieldNameToLabel(fieldName); const fieldLabel = label ?? defaultFieldLabel; const accessibleFieldLabel = typeof fieldLabel === "string" ? fieldLabel : defaultFieldLabel; const renderOptionLabel = useCallback((option) => String(option[labelKey]), [labelKey]); return /* @__PURE__ */ jsx(Controller, { name: fieldName, control, rules: registerOptions, render: ({ field: { name: rhfFieldName, value: rhfValue, onChange: rhfOnChange, onBlur: rhfOnBlur, ref: rhfRef, disabled: rhfDisabled }, fieldState: { error: fieldStateError } }) => { const isDisabled = muiDisabled || rhfDisabled; const fieldErrorMessage = fieldStateError?.message?.toString() ?? errorMessage; const isError = !!fieldErrorMessage; const showHelperTextElement = !!(helperText || isError && !hideErrorMessage); return /* @__PURE__ */ jsxs(FormControl, { error: isError, disabled: isDisabled, children: [ !hideLabel && /* @__PURE__ */ jsx(FormLabel, { label: fieldLabel, isVisible: isLabelAboveFormField, required, error: isError, disabled: isDisabled, formLabelProps: { ...formLabelProps, id: labelId, htmlFor: fieldId } }), /* @__PURE__ */ jsx(Autocomplete, { ...otherAutocompleteObjectProps, id: fieldId, options, multiple, value: rhfValue ?? (multiple ? [] : null), disabled: isDisabled, onChange: (event, newValue, reason, details) => { const fieldValue = newValue; if (customOnChange) { customOnChange({ rhfOnChange, newValue: fieldValue, event, reason, details }); return; } rhfOnChange(newValue); onValueChange?.({ newValue: fieldValue, event, reason, details }); }, onFocus, onBlur: (blurEvent) => { rhfOnBlur(); muiOnBlur?.(blurEvent); }, getOptionLabel: (option) => renderOptionLabel(option), isOptionEqualToValue: (option, value) => option[valueKey] === value?.[valueKey], renderInput: (params) => { const { InputProps, inputProps, disabled: paramsDisabled, ...otherInputParams } = params ?? {}; const { autoComplete = "off", ...otherTextFieldProps } = textFieldProps ?? {}; const textFieldInputProps = { ...inputProps, "aria-required": required, "aria-invalid": isError, "aria-labelledby": !hideLabel && isLabelAboveFormField ? labelId : void 0, "aria-label": hideLabel ? accessibleFieldLabel : void 0, "aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0, autoComplete }; return /* @__PURE__ */ jsx(TextField, { name: rhfFieldName, inputRef: mergeRefs(rhfRef, ref), disabled: paramsDisabled, ...otherTextFieldProps, ...otherInputParams, label: !isLabelAboveFormField ? /* @__PURE__ */ jsx(FormLabelText, { label: fieldLabel, required }) : void 0, error: isError, slotProps: { ...textFieldProps?.slotProps, input: { ...InputProps, ...textFieldProps?.slotProps?.input, endAdornment: /* @__PURE__ */ jsxs(Fragment$1, { children: [loading && /* @__PURE__ */ jsx(CircularProgress, { color: "inherit", size: 20 }), InputProps?.endAdornment] }) }, htmlInput: textFieldInputProps } }); }, autoHighlight, blurOnSelect: !multiple, disableCloseOnSelect: multiple, disableClearable, fullWidth: true, loading, limitTags, freeSolo: false, getLimitTagsText: (more) => getLimitTagsText?.(more) ?? `+${more} More`, slotProps: { ...slotProps, chip: ChipProps } }), /* @__PURE__ */ jsx(FormHelperText, { error: isError, errorMessage: fieldErrorMessage, hideErrorMessage, helperText, showHelperTextElement, formHelperTextProps: { ...formHelperTextProps, id: isError ? errorId : helperTextId } }) ] }); } }); }); //#endregion export { RHFAutocompleteObject as default };