@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.
130 lines (129 loc) • 5.51 kB
JavaScript
"use client";
import { RHFMuiConfigContext } from "../../config/ConfigProvider.js";
import { validateArray } from "../../utils/array.js";
import { keepLabelAboveFormField } from "../../utils/control.js";
import { getDisplayLabelForSelectValue, getOptionValue, isKeyValueOption, normalizeSelectValue } from "../../utils/object.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 { Fragment, useContext } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Controller } from "react-hook-form";
import MenuItem from "@mui/material/MenuItem";
import InputLabel from "@mui/material/InputLabel";
import Select from "@mui/material/Select";
//#region src/mui/select/index.tsx
const RHFSelect = ({ fieldName, control, registerOptions, options, labelKey, valueKey, multiple, showDefaultOption, defaultOptionText, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, required, helperText, errorMessage, hideErrorMessage, formHelperTextProps, onBlur, autoComplete = "off", renderValue, placeholder, ...otherSelectProps }) => {
validateArray("RHFSelect", options, labelKey, valueKey);
const { fieldId, labelId, helperTextId, errorId } = useFieldIds(fieldName);
const { allLabelsAboveFields } = useContext(RHFMuiConfigContext);
const isLabelAboveFormField = keepLabelAboveFormField(showLabelAboveFormField, allLabelsAboveFields);
const fieldLabelText = fieldNameToLabel(fieldName);
const fieldLabel = label ?? fieldLabelText;
const isError = !!errorMessage;
const showHelperTextElement = !!helperText || isError && !hideErrorMessage;
const SelectFormLabel = /* @__PURE__ */ jsx(FormLabelText, {
label: fieldLabel,
required
});
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 } }) => {
const isValueEmpty = !rhfValue || rhfValue === "" || multiple && Array.isArray(rhfValue) && !rhfValue.length;
const showPlaceholder = isValueEmpty && !!placeholder;
const selectLabelProp = isLabelAboveFormField || isValueEmpty ? void 0 : SelectFormLabel;
return /* @__PURE__ */ jsxs(Fragment, { children: [!isLabelAboveFormField && !showPlaceholder && /* @__PURE__ */ jsx(InputLabel, {
id: labelId,
shrink: !isValueEmpty,
children: SelectFormLabel
}), /* @__PURE__ */ jsxs(Select, {
id: fieldId,
name: rhfFieldName,
autoComplete,
labelId: isLabelAboveFormField ? void 0 : labelId,
"aria-required": required,
"aria-invalid": isError,
"aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0,
label: selectLabelProp,
value: rhfValue ?? (multiple ? [] : ""),
error: isError,
multiple,
displayEmpty: isValueEmpty,
inputRef: rhfRef,
disabled: muiDisabled,
onChange: (event, child) => {
const selectedValue = event.target.value;
const normalizedValue = normalizeSelectValue(selectedValue, options, labelKey, valueKey);
rhfOnChange(normalizedValue);
onValueChange?.(normalizedValue, event, child);
},
...otherSelectProps,
onBlur: (blurEvent) => {
rhfOnBlur();
onBlur?.(blurEvent);
},
renderValue: (value) => {
if (showPlaceholder) return /* @__PURE__ */ jsx("span", {
style: {
opacity: .6,
color: "inherit"
},
children: placeholder
});
if (Array.isArray(value)) {
const labels = value.map((val) => getDisplayLabelForSelectValue(val, options, labelKey, valueKey)).filter((node) => node !== "" && node !== null && node !== void 0);
return /* @__PURE__ */ jsx(Fragment, { children: renderValue?.(value) ?? labels.join(", ") });
}
const optionLabel = getDisplayLabelForSelectValue(value, options, labelKey, valueKey);
return /* @__PURE__ */ jsx(Fragment, { children: renderValue?.(value) ?? optionLabel });
},
children: [showDefaultOption && /* @__PURE__ */ jsx(MenuItem, {
value: "",
disabled: required,
children: defaultOptionText ?? `Select ${fieldLabelText}`
}), options.map((option) => {
const isObject = isKeyValueOption(option, labelKey, valueKey);
const opnValue = getOptionValue(option, valueKey);
return /* @__PURE__ */ jsx(MenuItem, {
value: opnValue,
children: isObject ? String(option[labelKey]) : String(option)
}, opnValue);
})]
})] });
}
}),
/* @__PURE__ */ jsx(FormHelperText, {
error: isError,
errorMessage,
hideErrorMessage,
helperText,
showHelperTextElement,
formHelperTextProps: {
id: isError ? errorId : helperTextId,
...formHelperTextProps
}
})
]
});
};
//#endregion
export { RHFSelect as default };