I am fairly new to react, and would really appreciate some pointers here:
I have an autocomplete dropdown with certain set of options. I want one of them to be selected by default, based on a condition I have.
from what i understand this can be completed by getOptionSelected prop in Autocomplete.
I created a sample to test this, however this is not working as expected. Can you please help?
/* eslint-disable no-use-before-define */
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
export default function ComboBox() {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option: { title: string; year: number }) => option.title}
getOptionSelected={(option) => option.year === 1994}
style={{ width: 300 }}
renderInput={(params) => (
<TextField {...params} label="Combo box" 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 }
];