Getting the value in the React material-UI Autocomplete

JavascriptReactjsMaterial Ui

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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 />
    )}
/>

Solution 2 - Javascript

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>

Solution 3 - Javascript

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

Solution 4 - Javascript

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" />}
    />
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDawn17View Question on Stackoverflow
Solution 1 - JavascriptDawn17View Answer on Stackoverflow
Solution 2 - Javascriptbcbl001View Answer on Stackoverflow
Solution 3 - JavascriptSupun SandaruwanView Answer on Stackoverflow
Solution 4 - Javascripthem charanView Answer on Stackoverflow