I'm using the Material-UI Autocomplete in React for an address search/verification form, and I'd like there to always be a default option that shows up no matter the results.
In Angular I used [displayWith] with a function to tell it to always display an item from the array with a specific ID, but I'm not sure how to do it in React. I've been trying to figure out filterOptions, but I don't want it to filter based on anything related to input, I just want it to always be an option.
In this simplified example, if I wanted it to always show the film with the year: 1111, how would I do that?
import React, { useState, ChangeEvent } from 'react';
import {
TextField,
InputAdornment
} from "@material-ui/core";
import Autocomplete from '@material-ui/lab/Autocomplete';
import { Search } from "@material-ui/icons";
import axios from "axios";
const AddressSearch = (props) => {
const [addressesList, setAddressesList] = useState([]);
const [inputAddress, setInputAddress] = useState<string>("");
const handleAddressChange = (event: ChangeEvent<{ value: unknown }>) => {
setInputAddress(event.target.value as string);
};
const topFilms = [
{ title: 'I HATE MOVIES', year: 1111 },
{ title: 'The Shawshank Redemption', year: 1994 },
{ title: 'The Godfather'},
{ title: 'The Godfather: Part II', year: 1974 }
]
return (
<div>
<Autocomplete
id="address-autocomplete"
freeSolo
options={topFilms}
getOptionLabel={(option) => option.title}
popupIcon={<Search />}
renderInput={(params) => <TextField
id="address-input"
{...params}
onChange={handleAddressChange}
placeholder="Quickly find your address"
InputProps={{ ...params.InputProps,
startAdornment: (
<InputAdornment position="start"><Search /></InputAdornment>
)}}
/> }
/>
</div>
);
}
export default AddressSearch;
Note: the website I'm working with is using Material-UI v 4.12