80

I am referring to the documentation of React Material-UI (https://material-ui.com/components/autocomplete/).

In the demo code,

    <Autocomplete
      options={top100Films}
      getOptionLabel={(option: FilmOptionType) => option.title}
      style={{ width: 300 }}
      renderInput={params => (
        <TextField {...params} label="Combo box" variant="outlined" fullWidth />
      )}
    />

I get how it works, but I am not sure how I am supposed to get the selected value.

For example, I want to use the onChange prop to this so that I can make some actions based on the selection.

I tried adding onChange={v => console.log(v)}

but the v does not show anything related to the selected value.

johannchopin
  • 13,720
  • 10
  • 55
  • 101
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • 1
    Thanks for asking. Can't believe that the project still hasn't clearly documented this! – Jay J Jan 07 '21 at 20:48

4 Answers4

178

Solved by using passing in the (event, value) to the onChange props.

<Autocomplete
    onChange={(event, value) => console.log(value)} // prints the selected value
    renderInput={params => (
        <TextField {...params} label="Label" variant="outlined" fullWidth />
    )}
/>
Dawn17
  • 7,825
  • 16
  • 57
  • 118
  • How add onsubmit? –  Jan 08 '20 at 16:38
  • 10
    I spent hours on end trying to get this value right, this solved beautifully. – Luis Febro Jan 12 '20 at 16:32
  • 15
    Nice! Minor addendum someone might fight useful: In case of the user typing in an arbitrary value (like `freeSolo`), there's no `onChange` event. However, there's `onInputChange` and I'm tracking the value in the component state, so I can use it when the user clicks on an "add" button. I'm using both events. – maaartinus Jun 20 '20 at 15:57
  • 5
    how do i get the index of the item selected? meaning if i select the first one, i want 0 and so on – Ak01 Oct 14 '20 at 06:47
  • 2
    How i get multiples autocomplete values? – Steve Angello Jun 15 '21 at 17:08
  • 1
    Hey @SteveAngello - have you found a way to get multiple autocomplete values? – ShwetaJ Jul 18 '21 at 21:40
  • Can you get the `option.title`/`getOptionLabel` in the `onChange` callback? – antpngl92 Aug 09 '22 at 09:24
8

The onChange prop works for multiple autocomplete values as well (@Steve Angello @ShwetaJ). The value returned is a list of all the selected options.

const [selectedOptions, setSelectedOptions] = useState([]);

const handleChange = (event, value) => setSelectedOptions(value);

const handleSubmit = () => console.log(selectedOptions);

.
.
.

<Autocomplete
  multiple
  autoHighlight
  id="tags-outlined"
  options={top100Films}
  getOptionLabel={(option) => option.title}
  onChange={handleChange}
  filterSelectedOptions
  renderInput={(params) => (
    <TextField
      {...params}
      variant="standard"
    />
  )}
/>

.
.
.


<button onClick={handleSubmit}>Submit!</button>
bcbl001
  • 81
  • 1
  • 2
3

You can use useState to store the recevied value and onChange to get the value:

const [selected, setSelected] = useState([]);


return (
    <Autocomplete
        onChange={(event, value) => setSelected(value)}
        renderInput={(params) => <TextField {...params} label="selected" />}
    />
);
Nicofisi
  • 1,083
  • 1
  • 15
  • 27
hem charan
  • 108
  • 1
  • 8
2

Here is TS working example

  const tags = ["perimeter", "Algebra", "equation", "square root"];

  const handleInput = (e: React.SyntheticEvent, value: string[]) => {
    console.log(value);
  };

<Autocomplete
    multiple
    options={tags}
    onChange={handleInput}
    renderTags={(value: readonly string[], getTagProps) =>
    value.map((option: string, index: number) => (
            <Chip
                variant="outlined"
                label={option}
                {...getTagProps({ index })}
             />
            ))
           }
           renderInput={(params) => (
            <TextField
               {...params}
               variant="filled"
               label="Tags"
               placeholder="select question tags"
           />
        )}
   />

============ Output ===============

enter image description here

Supun Sandaruwan
  • 1,814
  • 19
  • 19