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.

167 lines (166 loc) 6.24 kB
import { CustomComponentIds, CustomOnChangeProps, OptionValue, StrNumObjOption } from "../../types/common.js"; import { FormControlLabelProps, FormHelperTextProps, FormLabelProps, RadioProps } from "../../types/mui.js"; import { ChangeEvent, ReactNode } from "react"; import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form"; import { RadioGroupProps } from "@mui/material/RadioGroup"; //#region src/mui/radio-group/index.d.ts type OnValueChangeProps<Option extends StrNumObjOption = StrNumObjOption, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>> = { newValue: OptionValue<Option, ValueKey>; event: ChangeEvent<HTMLInputElement>; }; type RadioGroupInputProps = Omit<RadioGroupProps, 'name' | 'value' | 'onChange'>; type RHFRadioGroupProps<T extends FieldValues, Option extends StrNumObjOption = StrNumObjOption, LabelKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>> = { /** * Name/path of the React Hook Form field this component controls. */ fieldName: Path<T>; /** * React Hook Form control object returned by `useForm`. */ control: Control<T>; /** * Validation rules passed to React Hook Form for this field. */ registerOptions?: RegisterOptions<T, Path<T>>; /** * List of options to render as radio buttons. Best suited for smaller datasets, with * upto 10 options. For larger datasets, consider using `RHFAutocomplete`. */ options: Option[]; /** * Object key used to read the display label from each option. */ labelKey?: LabelKey; /** * Object key used to derive the stored field value when options are an array of objects. */ valueKey?: ValueKey; /** * Function to customize the label for each radio button. * When not provided, the option label derived from `labelKey` (or the * option value itself for primitive options) is rendered. * * @param option - The option being rendered. * @returns Custom React content to display for the option. */ renderOptionLabel?: (option: Option) => ReactNode; /** * Function to dynamically disable specific option(s). * * Return `true` to disable the option and prevent it from being checked. * * @param option - The option being evaluated. */ getOptionDisabled?: (option: Option) => boolean; /** * Overrides the default radio group change handling. * Receives the normalized selected option value and the original radio change event. * Call `rhfOnChange` with the selected value that should be stored; else the form value will not be updated. * * @param rhfOnChange - React Hook Form field change handler for the selected radio value. * @param newValue - Normalized selected option value, using `valueKey` for object options when provided. * @param event - Original radio change event. */ customOnChange?: ({ rhfOnChange, newValue, event }: CustomOnChangeProps<OnValueChangeProps<Option, ValueKey>, OptionValue<Option, ValueKey>>) => void; /** * Called after the default radio group handler stores the normalized selected value in React Hook Form. * * ⚠️ Important: * This callback is not called when `customOnChange` is used. * * @param newValue - Normalized selected option value, using `valueKey` for object options when provided. * @param event - Original radio change event. */ onValueChange?: ({ newValue, event }: OnValueChangeProps<Option, ValueKey>) => void; /** * When true, disables the field and associated controls. */ disabled?: boolean; /** * Label content shown for the field. Defaults to a label generated from `fieldName`. */ label?: ReactNode; /** * When true, renders the field label above the form field instead of inside or beside it. */ showLabelAboveFormField?: boolean; /** * Props forwarded to the internal `FormLabel`. The `id` is managed by the component. */ formLabelProps?: Omit<FormLabelProps, 'id'>; /** * When true, hides the rendered field label while preserving accessible labeling where possible. */ hideLabel?: boolean; /** * Props to pass down to each Radio component. Can be used to set * a custom color, size, etc. for all radios in the group. */ radioProps?: RadioProps; /** * Props forwarded to each internal MUI `FormControlLabel`. */ formControlLabelProps?: FormControlLabelProps; /** * Helper text shown below the field when there is no visible validation error. */ helperText?: ReactNode; /** * When true, marks the field as required in the UI and accessibility attributes. */ required?: boolean; /** * @deprecated * Field error message is now automatically derived from form state. * Passing this prop is no longer necessary and it will be removed in the next major version. */ errorMessage?: ReactNode; /** * If true, hides the error message text while keeping the field in an error state. */ hideErrorMessage?: boolean; /** * Props forwarded to the internal `FormHelperText`. The `id` is managed by the component. */ formHelperTextProps?: Omit<FormHelperTextProps, 'id'>; /** * Custom ids for generated field, label, helper text, and error elements. */ customIds?: CustomComponentIds; } & RadioGroupInputProps; declare const RHFRadioGroup: <T extends FieldValues, Option extends StrNumObjOption = StrNumObjOption, LabelKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>>({ fieldName, control, registerOptions, options, renderOptionLabel, getOptionDisabled, labelKey, valueKey, customOnChange, onValueChange, disabled: muiDisabled, label, showLabelAboveFormField, formLabelProps, hideLabel, radioProps, formControlLabelProps, required, helperText, errorMessage, hideErrorMessage, formHelperTextProps, onBlur, customIds, ...otherRadioGroupProps }: RHFRadioGroupProps<T, Option, LabelKey, ValueKey>) => import("react").JSX.Element; //#endregion export { RHFRadioGroupProps, RHFRadioGroup as default };