Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Inputs and Forms

Select and Autocomplete

Choose from lists with Material UI: Select with MenuItem, multiple selection, native selects, and Autocomplete with filtering, free solo, async options and grouping.

When a user must pick from a list, you have a few choices. A short list of fixed options suits Select. A long or searchable list suits Autocomplete. On phones, a native select often gives the best experience because the operating system shows its own picker. This lesson covers all three and how to decide.

Select

A MUI Select is a styled dropdown built from MenuItem children. It is controlled: you hold the value in state. Wrap it in FormControl with an InputLabel so the label floats and is connected for accessibility.

JSX
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';

export default function CountrySelect() {
  const [country, setCountry] = useState('');

  return (
    <FormControl fullWidth>
      <InputLabel id="country-label">Country</InputLabel>
      <Select
        labelId="country-label"
        id="country"
        value={country}
        label="Country"
        onChange={(event) => setCountry(event.target.value)}
      >
        <MenuItem value="de">Germany</MenuItem>
        <MenuItem value="fr">France</MenuItem>
        <MenuItem value="jp">Japan</MenuItem>
      </Select>
    </FormControl>
  );
}

Details that matter:

  • labelId must match the InputLabel's id, and the label prop on Select leaves a gap in the outlined border for the floating label. Forgetting label makes the border run through the text.
  • value must be one of the MenuItem values or '' (nothing selected). Another value triggers a console warning.
  • A simpler alternative: <TextField select label="Country" value={country} onChange={...}> with MenuItem children. It wires label, helper text and error state for you, so it is the usual choice in forms.
JSX
<TextField select label="Country" value={country} onChange={(e) => setCountry(e.target.value)} helperText="Where you live" fullWidth>
  {countries.map((c) => <MenuItem key={c.code} value={c.code}>{c.name}</MenuItem>)}
</TextField>

Multiple selection

Set multiple and make the value an array. renderValue controls what the closed select shows; Chips are a popular choice:

JSX
<Select
  multiple
  value={tags}                       // array of strings
  onChange={(e) => setTags(typeof e.target.value === 'string' ? e.target.value.split(',') : e.target.value)}
  renderValue={(selected) => (
    <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
      {selected.map((t) => <Chip key={t} label={t} size="small" />)}
    </Box>
  )}
>
  {allTags.map((t) => (
    <MenuItem key={t} value={t}>
      <Checkbox checked={tags.includes(t)} />
      <ListItemText primary={t} />
    </MenuItem>
  ))}
</Select>

The typeof ... === 'string' guard handles autofill, which can deliver a comma-separated string.

Native select

NativeSelect (or TextField select with slotProps={{ select: { native: true } }}) renders a real <select> with <option> children. It is less pretty but faster, always accessible, and uses the platform picker on mobile:

JSX
<TextField select label="Size" value={size} onChange={handleChange} slotProps={{ select: { native: true } }}>
  <option value="s">Small</option>
  <option value="m">Medium</option>
</TextField>

Use native for long, simple lists on mobile-first sites and the styled Select when you need rich items (icons, checkboxes, groups).

Stand-in: a Select with a floating label

The first runnable example is a stand-in that behaves like TextField select for the common case: a label, a value and an error when nothing is chosen. It uses a native <select> underneath.

Stand-in: select with label and validation

Autocomplete

Autocomplete is a text input with a popup list of options that filters as you type. It supports keyboard navigation, screen readers, clearing, multiple values and more. The minimum is options plus a renderInput:

JSX
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';

const movies = [
  { title: 'The Matrix', year: 1999 },
  { title: 'Spirited Away', year: 2001 },
  { title: 'Parasite', year: 2019 },
];

<Autocomplete
  options={movies}
  getOptionLabel={(option) => option.title}
  renderInput={(params) => <TextField {...params} label="Movie" />}
  value={selected}
  onChange={(event, newValue) => setSelected(newValue)}
  isOptionEqualToValue={(option, value) => option.title === value.title}
/>
  • options is an array of strings or objects. For objects, set getOptionLabel.
  • renderInput receives params (props the input needs) that you spread into a TextField. Do not skip {...params}.
  • value and onChange control the selection; inputValue and onInputChange control the typed text separately.
  • isOptionEqualToValue tells MUI how to compare objects (by id, not by reference).
  • multiple allows several values (chips are shown); freeSolo allows text that is not in the list; disableClearable removes the clear button.
  • groupBy={(o) => o.firstLetter} groups options; renderOption customizes each row; loading, loadingText and noOptionsText handle states.

Async options

For large lists, fetch options from the server as the user types. Keep a loading flag and debounce the request (wait ~300ms after the last keystroke) so you do not fire a request for every character:

JSX
<Autocomplete
  options={options}
  loading={loading}
  filterOptions={(x) => x}              // server already filtered
  getOptionLabel={(o) => o.name}
  onInputChange={(e, text) => setQuery(text)}   // effect debounces and fetches
  renderInput={(params) => (
    <TextField
      {...params}
      label="City"
      slotProps={{ input: { ...params.InputProps, endAdornment: (<>{loading ? <CircularProgress size={20} /> : null}{params.InputProps.endAdornment}</>) } }}
    />
  )}
/>

Stand-in: autocomplete with keyboard navigation

The second stand-in is a small combobox following the same ideas: filter as you type, arrow keys move the highlight, Enter selects, Escape closes. The real component also handles many more details (ARIA live regions, virtualization, grouping).

Stand-in: Autocomplete with arrow keys

Choosing the right control

Situation Control
2 to 7 fixed options, desktop Select or radio buttons (see the next lesson)
Long list, search needed Autocomplete
Mobile, plain list NativeSelect
Pick several Select multiple or Autocomplete multiple
Free text with suggestions Autocomplete freeSolo

Common mistakes

  • Select without InputLabel/labelId: the control has no accessible name.
  • Using object options without isOptionEqualToValue, producing "value not in options" warnings after the options reload.
  • Passing value={undefined} to Autocomplete; use null for "nothing selected".
  • Forgetting {...params} in renderInput.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must the Select value be?

    A value that matches no MenuItem triggers a warning and shows nothing selected.
  2. 2. What does renderInput's params argument contain?

    Autocomplete passes props for wiring (id, ARIA, refs, adornments) that TextField needs.
  3. 3. How do you let the user pick several values in Autocomplete?

    multiple turns the value into an array and displays selected items as chips.
  4. 4. Why debounce an async Autocomplete request?

    Waiting for a short pause in typing reduces server load and out-of-order responses.

Summary

  • Select with MenuItem is controlled; connect InputLabel/labelId and the label prop, or use TextField select.
  • multiple needs an array value; renderValue decides what the closed control shows.
  • NativeSelect uses the platform picker and is the simplest accessible option.
  • Autocomplete filters as you type: options, renderInput, getOptionLabel, isOptionEqualToValue, freeSolo, multiple, groupBy.
  • Use null for an empty Autocomplete value and debounce server queries.

Next: Checkbox, Radio and Switch