@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.
96 lines (95 loc) • 3.64 kB
JavaScript
"use client";
import { RHFMuiConfigContext } from "../../config/ConfigProvider.js";
import { mergeRefs, resolveLabelAboveControl } from "../../utils/control.js";
import FormControl from "../../common/FormControl.js";
import FormHelperText from "../../common/FormHelperText.js";
import FormLabel from "../../common/FormLabel.js";
import { fieldNameToLabel } from "../../utils/text-transform.js";
import { useFieldIds } from "../../utils/useFieldIds.js";
import { forwardRef, useContext } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Controller } from "react-hook-form";
import MuiRating from "@mui/material/Rating";
//#region src/mui/rating/index.tsx
const RHFRating = forwardRef(function RHFRating({ fieldName, control, registerOptions, required, customOnChange, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, hideLabel, helperText, errorMessage, hideErrorMessage, formHelperTextProps, onBlur, customIds, ...otherRatingProps }, ref) {
const { allLabelsAboveFields } = useContext(RHFMuiConfigContext);
const { fieldId, labelId, helperTextId, errorId } = useFieldIds(fieldName, customIds);
const defaultFieldLabel = fieldNameToLabel(fieldName);
const fieldLabel = label ?? defaultFieldLabel;
const accessibleFieldLabel = typeof fieldLabel === "string" ? fieldLabel : defaultFieldLabel;
const isLabelAboveControl = resolveLabelAboveControl(showLabelAboveFormField, allLabelsAboveFields);
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, {
component: "fieldset",
error: isError,
disabled: isDisabled,
children: [
!hideLabel && /* @__PURE__ */ jsx(FormLabel, {
label: fieldLabel,
isVisible: isLabelAboveControl,
required,
error: isError,
disabled: isDisabled,
formLabelProps: {
...formLabelProps,
id: labelId,
component: "legend"
}
}),
/* @__PURE__ */ jsx(MuiRating, {
ref: mergeRefs(rhfRef, ref),
id: fieldId,
name: rhfFieldName,
value: rhfValue ?? null,
disabled: isDisabled,
onChange: (event, newValue) => {
if (customOnChange) {
customOnChange({
rhfOnChange,
newValue,
event
});
return;
}
rhfOnChange(newValue);
onValueChange?.({
newValue,
event
});
},
onBlur: (blurEvent) => {
rhfOnBlur();
onBlur?.(blurEvent);
},
"aria-labelledby": !hideLabel ? labelId : void 0,
"aria-label": hideLabel ? accessibleFieldLabel : void 0,
"aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0,
"aria-invalid": isError || void 0,
...otherRatingProps
}),
/* @__PURE__ */ jsx(FormHelperText, {
error: isError,
errorMessage: fieldErrorMessage,
hideErrorMessage,
helperText,
showHelperTextElement,
formHelperTextProps: {
...formHelperTextProps,
id: isError ? errorId : helperTextId
}
})
]
});
}
});
});
//#endregion
export { RHFRating as default };