@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.
138 lines (137 loc) • 5.74 kB
TypeScript
import { CustomComponentIds, CustomOnChangeProps, OptionValue, StrNumObjOption } from "../../types/common.js";
import { FormHelperTextProps, FormLabelProps } from "../../types/mui.js";
import { ChangeEvent, JSX, ReactNode, Ref } from "react";
import { Control, FieldValues, Path, RegisterOptions } from "react-hook-form";
import { NativeSelectProps } from "@mui/material/NativeSelect";
//#region src/mui/native-select/index.d.ts
type InputNativeSelectProps = Omit<NativeSelectProps, 'name' | 'id' | 'labelId' | 'error' | 'onChange' | 'value' | 'ref'>;
type OnValueChangeProps<Option extends StrNumObjOption = StrNumObjOption, ValueKey extends Extract<keyof Option, string> = Extract<keyof Option, string>> = {
newValue: OptionValue<Option, ValueKey>;
event: ChangeEvent<HTMLSelectElement>;
};
type RHFNativeSelectProps<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 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 how each option is displayed in the `<option>` element.
* 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;
/**
* Overrides the default native select change handling.
* Receives the normalized selected value and the original native select change event.
* 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 when provided.
* @param event - Original native select change event.
*/
customOnChange?: ({
rhfOnChange,
newValue,
event
}: CustomOnChangeProps<OnValueChangeProps<Option, ValueKey>, OptionValue<Option, ValueKey>>) => void;
/**
* Called after the default native 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 when provided.
* @param event - Original native select change event.
*/
onValueChange?: ({
newValue,
event
}: OnValueChangeProps<Option, ValueKey>) => void;
/**
* Custom text displayed for the default option when
* `showDefaultOption` is enabled.
*
* @default `Select ${fieldLabel}`
*/
defaultOptionText?: string;
/**
* 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;
/**
* 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'>;
/**
* Custom ids for generated field, label, helper text, and error elements.
*/
customIds?: CustomComponentIds;
} & InputNativeSelectProps;
declare const RHFNativeSelect: <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>>(props: RHFNativeSelectProps<T, Option, LabelKey, ValueKey> & {
ref?: Ref<HTMLInputElement>;
}) => JSX.Element;
//#endregion
export { RHFNativeSelectProps, RHFNativeSelect as default };