I would like to display default value to my Autocomplete TextField component from Material UI in React.js. A pre-populated value that is automatically loaded from the users' profile and that can be changed with another one from the list.
Here is my code:
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
export const ComboBox =() => {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
);
}
// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather', year: 1972 },
{ title: 'The Godfather: Part II', year: 1974 },
{ title: 'The Dark Knight', year: 2008 }
]
I only see the input field with the label on it. defaultValue is listed as an API of both TextField and Autocomplete and I also tried to move it directly under Autocomplete. Still not working.
If you need to set the initial value for your Autocomplete component and then never programmatically update it again, the best option is the defaultValue prop. This prop accepts a value that only gets rendered until an option is selected from the Autocomplete's dropdown list.
To get the value in the React Material-UI autocomplete, we can get the selected values from the onChange callback. We add the Autocomplete from the @material-ui/lab module. And we set the options prop to the top5films array to add the options for the autocomplete.
The React AutoComplete supports the autofill behavior with the help of autofill property. Whenever you change the input value and press the down key, the AutoComplete will autocomplete your data by matching the typed character. Suppose, if no matches found then, AutoComplete doesn't suggest any item.
your defaultValue
of <AutoComplete />
should have the same format as your options
, the getOptionLable()
is used to form the label even from your defaultValue
.
in your code title
property of the string is undefined
, so nothing is shown.
this code should work fine:
<Autocomplete
id="combo-box-demo"
options={top100Films}
defaultValue={{ title: "The Godfather", year: 1972 }}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
You can use the defaultValue
option like this:
<Autocomplete
id="combo-box-demo"
options={top100Films}
defaultValue={ top100Films[0] }
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>
If you use multiple so you should set defaultValue in list like blow
<Autocomplete multiple id="tags-standard" options={categoryList}
defaultValue={[{ title: "cat1", id: 1 }, { title: "cat2", id: 2 }]}
getOptionLabel={(option) => option.title}
renderInput={(params) => (
<TextField {...params} variant="standard" label="Category" />
)}
onChange={handleCategory}
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With