UNPKG

@nish1896/rhf-mui-components

Version:

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

157 lines (156 loc) 5.95 kB
"use client"; import { RHFMuiConfigContext } from "../../config/ConfigProvider.js"; import { validateArray } from "../../utils/array.js"; import { keepLabelAboveFormField } from "../../utils/control.js"; import { isAboveMuiV5 } from "../../utils/mui.js"; import { fieldNameToLabel } from "../../utils/text-transform.js"; import { useFieldIds } from "../../utils/useFieldIds.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 { 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 MuiTextField from "@mui/material/TextField"; import Chip from "@mui/material/Chip"; import CircularProgress from "@mui/material/CircularProgress"; //#region src/mui/autocomplete-object/index.tsx const RHFAutocompleteObject = ({ fieldName, control, registerOptions, options, multiple, labelKey, valueKey, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, required, helperText, errorMessage, hideErrorMessage, formHelperTextProps, textFieldProps, slotProps, ChipProps, onBlur, loading, ...otherAutoCompleteProps }) => { validateArray("RHFAutocompleteObject", options, labelKey, valueKey); const { fieldId, labelId, helperTextId, errorId } = useFieldIds(fieldName); const { allLabelsAboveFields } = useContext(RHFMuiConfigContext); const isLabelAboveFormField = keepLabelAboveFormField(showLabelAboveFormField, allLabelsAboveFields); const fieldLabel = label ?? fieldNameToLabel(fieldName); const isError = !!errorMessage; const showHelperTextElement = !!helperText || isError && !hideErrorMessage; const renderOptionLabel = useCallback((option) => { return option[labelKey]; }, [labelKey]); return /* @__PURE__ */ jsxs(FormControl, { error: isError, children: [ /* @__PURE__ */ jsx(FormLabel, { label: fieldLabel, isVisible: isLabelAboveFormField, required, error: isError, formLabelProps: { id: labelId, htmlFor: fieldId, ...formLabelProps } }), /* @__PURE__ */ jsx(Controller, { name: fieldName, control, rules: registerOptions, render: ({ field: { name: rhfFieldName, value: rhfValue, onChange: rhfOnChange, onBlur: rhfOnBlur, ref: rhfRef } }) => { return /* @__PURE__ */ jsx(Autocomplete, { id: fieldId, options, multiple, value: rhfValue, loading, autoHighlight: true, blurOnSelect: !multiple, disableCloseOnSelect: multiple, fullWidth: true, renderTags: (value, getTagProps) => value.map((option, index) => { const { key, ...otherChipProps } = getTagProps({ index }); return /* @__PURE__ */ jsx(Chip, { ...otherChipProps, label: renderOptionLabel(option), ...ChipProps }, key); }), disabled: muiDisabled, onChange: (event, newValue, reason, details) => { const fieldValue = newValue === null ? null : Array.isArray(newValue) ? newValue : newValue; rhfOnChange(newValue); onValueChange?.(fieldValue, event, reason, details); }, onBlur: (blurEvent) => { rhfOnBlur(); onBlur?.(blurEvent); }, limitTags: 2, getLimitTagsText: (value) => `+${value} More`, getOptionLabel: (option) => renderOptionLabel(option), isOptionEqualToValue: (option, value) => { return 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": isLabelAboveFormField ? labelId : void 0, "aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0, autoComplete }; return /* @__PURE__ */ jsx(MuiTextField, { name: rhfFieldName, inputRef: rhfRef, disabled: paramsDisabled || muiDisabled, ...otherTextFieldProps, ...otherInputParams, label: !isLabelAboveFormField ? /* @__PURE__ */ jsx(FormLabelText, { label: fieldLabel, required }) : void 0, error: isError, ...isAboveMuiV5 ? { 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 } } : { InputProps: { ...InputProps, ...textFieldProps?.InputProps, endAdornment: /* @__PURE__ */ jsxs(Fragment$1, { children: [loading && /* @__PURE__ */ jsx(CircularProgress, { color: "inherit", size: 20 }), InputProps?.endAdornment] }) }, inputProps: textFieldInputProps } }); }, ...isAboveMuiV5 ? { slotProps: { ...slotProps, chip: ChipProps } } : { ChipProps, slotProps }, ...otherAutoCompleteProps }); } }), /* @__PURE__ */ jsx(FormHelperText, { error: isError, errorMessage, hideErrorMessage, helperText, showHelperTextElement, formHelperTextProps: { id: isError ? errorId : helperTextId, ...formHelperTextProps } }) ] }); }; //#endregion export { RHFAutocompleteObject as default };