@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.
160 lines (159 loc) • 6.6 kB
TypeScript
import { CustomComponentIds, CustomOnChangeProps, OptionValue, StrNumObjOption } from "../../types/common.js";
import { FormHelperTextProps, FormLabelProps, SelectProps as SelectProps$1 } from "../../types/mui.js";
import { JSX, ReactNode, Ref } from "react";
import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form";
import { SelectChangeEvent } from "@mui/material/Select";
//#region src/mui/select/index.d.ts
type SelectValue<Value, Multiple extends boolean> = Multiple extends true ? Value[] : Value;
type OnValueChangeProps<Option extends StrNumObjOption = StrNumObjOption, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>, Multiple extends boolean = false> = {
newValue: SelectValue<OptionValue<Option, ValueKey>, Multiple>;
event: SelectChangeEvent<SelectValue<OptionValue<Option, ValueKey>, Multiple>>;
child: ReactNode;
};
type RHFSelectProps<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>, Multiple extends boolean = false> = {
/**
* 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 display in the dropdown.
* Note:
* - Works best for small to moderate datasets.
* - If options exceed ~20 items, `RHFAutocomplete` or `RHFMultiAutocomplete` is
* recommended for improved searchability, keyboard navigation, and performance.
*/
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;
/**
* Custom renderer for dropdown options.
*
* Use this prop to customize the label for each option in the `MenuItem` component.
* 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 selected.
*
* @param option - The option being evaluated.
*/
getOptionDisabled?: (option: Option) => boolean;
/**
* When true, allows selecting multiple values.
*/
multiple?: Multiple;
/**
* When `true`, displays a default placeholder option at the top of the
* dropdown menu.
*
* The option uses an empty string (`''`) as its value and is automatically
* disabled when the field is marked as required.
*
* @default false
*/
showDefaultOption?: boolean;
/**
* Custom text displayed for the default option when
* `showDefaultOption` is enabled.
*
* @default `Select ${fieldLabel}`
*/
defaultOptionText?: string;
/**
* Overrides the default MUI Select change handling.
* Receives the normalized selected value, original select event, and selected child element.
* Call `rhfOnChange` with the value that should be stored; else the form value will not be updated.
*
* @param rhfOnChange - React Hook Form field change handler for the selected value.
* @param newValue - Normalized selected value, using `valueKey` for object options and arrays for multiple selection.
* @param event - Original MUI Select change event.
* @param child - Selected option element provided by MUI Select.
*/
customOnChange?: ({
rhfOnChange,
newValue,
event,
child
}: CustomOnChangeProps<OnValueChangeProps<Option, ValueKey, Multiple>, SelectValue<OptionValue<Option, ValueKey>, Multiple>>) => void;
/**
* Called after the default MUI Select handler stores the normalized selected value in React Hook Form.
*
* ⚠️ Important:
* This callback is not called when `customOnChange` is used.
*
* @param newValue - Normalized selected value, using `valueKey` for object options and arrays for multiple selection.
* @param event - Original MUI Select change event.
* @param child - Selected option element provided by MUI Select.
*/
onValueChange?: ({
newValue,
event,
child
}: OnValueChangeProps<Option, ValueKey, Multiple>) => void;
/**
* 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;
/**
* Helper text shown below the field when there is no visible validation error.
*/
helperText?: ReactNode;
/**
* @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'>;
/**
* Placeholder text displayed when no option is selected.
*
* Unlike the default option, the placeholder is displayed in the select
* input itself and is not rendered as a selectable menu item.
*/
placeholder?: string;
/**
* Custom ids for generated field, label, helper text, and error elements.
*/
customIds?: CustomComponentIds;
} & SelectProps$1;
declare const RHFSelect: <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>, Multiple extends boolean = false>(props: RHFSelectProps<T, Option, LabelKey, ValueKey, Multiple> & {
ref?: Ref<HTMLInputElement>;
}) => JSX.Element;
//#endregion
export { RHFSelectProps, RHFSelect as default };