RHF-MUI Components
NPM

RHFNativeSelect

RHFNativeSelect wraps the Material UI Native Select, a real <select> element that is ideal for mobile and lightweight dropdowns, and binds it to React Hook Form. Like RHFSelect, it accepts both arrays and arrays of objects as options.

Tip

If the number of options exceeds 20, consider using RHFAutocomplete or RHFMultiAutocomplete for better searchability, improved keyboard navigation, and overall performance.

Usage

import RHFNativeSelect, { RHFNativeSelectProps } from '@nish1896/rhf-mui-components/mui/native-select';
import { useForm } from 'react-hook-form';

type FormValues = {
  country: string;
};

const { control, handleSubmit } = useForm<FormValues>({
  defaultValues: { country: '' }
});

<form onSubmit={handleSubmit(values => console.log(values))}>
  <RHFNativeSelect
    fieldName="country"
    control={control}
    options={['India', 'United States', 'Germany']}
    registerOptions={{ required: 'Please select a country' }}
  />
</form>

Only a single selection is supported, and undefined or null mean no selection. onValueChange receives { newValue, event }: the normalized value and the native change event. The control always fills the width of its container.

With an array of objects

When options is an array of objects, both labelKey and valueKey are required.

  • labelKey: the property used as each option's visible label.
  • valueKey: the property used as each option's value.

The stored value has the type of the valueKey property: a string property gives a string, and a number property gives a number.

<RHFNativeSelect
  fieldName="country"
  control={control}
  options={[
    { code: 'AUS', country: 'Australia' },
    { code: 'IN', country: 'India' },
    { code: 'UAE', country: 'United Arab Emirates' }
  ]}
  labelKey="country"
  valueKey="code"
/>
Warning

When using an array of objects for options, both labelKey and valueKey are required. If either is missing, an error will be thrown.

Default option & placeholder

A native select always renders a first <option> with the value ''. Its text is defaultOptionText, falling back to placeholder, and it is empty when neither is set. The option is disabled when the field is required, so it can't be picked again once a real option is chosen. Picking it, when it is enabled, stores ''.

<RHFNativeSelect
  fieldName="currency"
  control={control}
  options={['USD', 'EUR', 'GBP']}
  defaultOptionText="Select currency"
  registerOptions={{ required: 'Currency is required' }}
/>

placeholder works as a shorthand for the same text. A native <select> has no placeholder of its own, so it only sets the default option's text.

Customizing individual options

renderOptionLabel customizes the text of each option. It also receives a { disabled, selected } state. An <option> can only display text, so return a string or number, not elements. getOptionDisabled receives an option and returns true to disable it.

<RHFNativeSelect
  fieldName="product"
  control={control}
  options={[
    { id: 1, name: 'Keyboard', stock: 12 },
    { id: 2, name: 'Monitor', stock: 0 },
    { id: 3, name: 'Mouse', stock: 4 }
  ]}
  labelKey="name"
  valueKey="id"
  getOptionDisabled={option => option.stock === 0}
  renderOptionLabel={(option, { disabled }) =>
    disabled
      ? `${option.name} (out of stock)`
      : `${option.name} (${option.stock} left)`
  }
/>

Label placement

A native select has no inline label, so showLabelAboveFormField defaults to true and the label is rendered above the control. Pass false to hide the visible label while keeping the accessible name. The prop takes precedence over allLabelsAboveFields from ConfigProvider, so false hides this label even when the config places every label above its field.

Handling value changes

onValueChange runs after the new value has been stored in the form. It receives { newValue, event }: the normalized value and the native change event. Use it for side effects, such as clearing a dependent field:

<RHFNativeSelect
  fieldName="country"
  control={control}
  options={['India', 'Germany', 'Canada']}
  onValueChange={({ newValue, event }) => setValue('city', '')}
/>

Use customOnChange when you need to change what gets stored. It replaces the default handling, so you must call rhfOnChange yourself with the value to store. This example stores null instead of an empty string for the default option: onValueChange is not called when customOnChange is used.

<RHFNativeSelect
  fieldName="currency"
  control={control}
  options={['USD', 'EUR', 'GBP']}
  customOnChange={({ rhfOnChange, newValue, event }) => {
    rhfOnChange(newValue === '' ? null : newValue);
  }}
/>

The label, helper text and error props work as described in RHFTextField.

Examples

API

The RHFNativeSelectProps interface extends NativeSelectProps and accepts the following additional props. Props marked with * are required.

NameTypeDescription
fieldName*string
Name of the field registered with React Hook Form. This prop is required for all components.
control*UseFormControl
The control option yielded on calling the useForm hook.
registerOptionsRegisterOptions
React Hook Form validation rules. Useful when not using a schema validation library such as Yup or Joi.
options*string[] / number[] / object[]
An array with string, numeric or object values. Make sure to pass labelKey and valueKey when options is an array of objects.
labelKeystring
Property name used as the visible label for each option.
Required when options is an array of objects.
valueKeystring
Property name used as the stored value for each option.
Required when options is an array of objects.
customOnChange({ rhfOnChange, newValue, event }) => void
Override the default onChange behavior of the native select component. You must pass the updated newValue to the rhfOnChange function to update the field value.
onValueChange({ newValue, event }) => void
An optional callback function when an option is selected. The latest value can be obtained from newValue argument.
renderOptionLabel(option) => ReactNode
Custom renderer for option labels. When not provided, the label is derived from the option value or the property specified by labelKey.
Option state param added in v4.2.0
getOptionDisabled(option) => boolean
Function used to determine whether an option should be disabled. Return true to disable the option and prevent it from being selected.
defaultOptionTextstring
Custom text to replace the default text when showDefaultOption is true for RHFSelect or RHFNativeSelect.
labelReactNode
The text to render in the FormLabel component. By default, the value of fieldName is transformed (e.g., "firstName" to "First Name") using the fieldNameToLabel function.
showLabelAboveFormFieldboolean
Renders the form label above the field.
formLabelPropsFormLabelProps
FormLabelProps to customise FormLabel component for a field. Multiple fields can be configured using the ConfigProvider component.
hideLabelboolean
Hides the FormLabel component if you don’t want to display the default form label component or prefer to render a fully custom label instead.
renderError(error: FieldError) => ReactNode
Custom renderer for the React Hook Form field error. Receives the current field error and returns the content to display, such as error.message or a custom React element in the HelperText component.
Added in v4.1.0.
hideErrorMessageboolean
A flag to prevent replacement of helper text of a field by the errorMessage when the validation is triggered.
helperTextReactNode
The content to display within the FormHelperText component below the field. If the field validation fails, this content will be overridden by the corresponding error message.
formHelperTextPropsFormHelperTextProps
FormHelperTextProps to customise FormHelperText component for a field. Multiple fields can be configured using the ConfigProvider component.
customIds{ field, label, helperText, error }
Overrides the default field, label, helper text, and error IDs used for accessibility.

Source Code

View the full implementation of this component on GitHub.