@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.
91 lines (90 loc) • 3.54 kB
JavaScript
"use client";
import { RHFMuiConfigContext } from "../../config/ConfigProvider.js";
import { mergeRefs } from "../../utils/control.js";
import FormHelperText from "../../common/FormHelperText.js";
import { fieldNameToLabel } from "../../utils/text-transform.js";
import { useFieldIds } from "../../utils/useFieldIds.js";
import { Fragment, forwardRef, useContext } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { Controller } from "react-hook-form";
import FormControlLabel from "@mui/material/FormControlLabel";
import MuiCheckbox from "@mui/material/Checkbox";
//#region src/mui/checkbox/index.tsx
const RHFCheckbox = forwardRef(function RHFCheckbox({ fieldName, control, registerOptions, customOnChange, onValueChange, disabled: muiDisabled, label, formControlLabelProps, hideLabel, helperText, errorMessage, hideErrorMessage, formHelperTextProps, onBlur, slotProps: muiSlotProps, customIds, ...otherCheckboxProps }, ref) {
const { fieldId, helperTextId, errorId } = useFieldIds(fieldName, customIds);
const { defaultFormControlLabelSx } = useContext(RHFMuiConfigContext);
const defaultFieldLabel = fieldNameToLabel(fieldName);
const fieldLabel = label ?? defaultFieldLabel;
const accessibleFieldLabel = typeof fieldLabel === "string" ? fieldLabel : defaultFieldLabel;
const { sx, ...otherFormControlLabelProps } = formControlLabelProps ?? {};
const appliedFormControlLabelSx = {
...defaultFormControlLabelSx,
...sx
};
const { input: slotPropsInput, ...otherSlotProps } = muiSlotProps ?? {};
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(Fragment, { children: [/* @__PURE__ */ jsx(FormControlLabel, {
...otherFormControlLabelProps,
control: /* @__PURE__ */ jsx(MuiCheckbox, {
...otherCheckboxProps,
id: fieldId,
name: rhfFieldName,
checked: Boolean(rhfValue),
disabled: isDisabled,
onChange: (event, checked) => {
if (customOnChange) {
customOnChange({
rhfOnChange,
newValue: checked,
event
});
return;
}
rhfOnChange(checked);
onValueChange?.({
newValue: checked,
event
});
},
onBlur: (blurEvent) => {
rhfOnBlur();
onBlur?.(blurEvent);
},
"aria-label": hideLabel ? accessibleFieldLabel : void 0,
"aria-describedby": showHelperTextElement ? isError ? errorId : helperTextId : void 0,
"aria-invalid": isError || void 0,
slotProps: {
...otherSlotProps,
input: {
...slotPropsInput,
ref: mergeRefs(rhfRef, ref)
}
}
}),
label: hideLabel ? void 0 : fieldLabel,
sx: appliedFormControlLabelSx,
disabled: isDisabled
}), /* @__PURE__ */ jsx(FormHelperText, {
error: isError,
errorMessage: fieldErrorMessage,
hideErrorMessage,
helperText,
showHelperTextElement,
formHelperTextProps: {
...formHelperTextProps,
id: isError ? errorId : helperTextId
}
})] });
}
});
});
//#endregion
export { RHFCheckbox as default };