Using with Material UI's Autocomplete? #847
-
I'm trying to use this hook along with Material UI's Autocomplete component, but am not having much success. Does anyone have an example of this scenario? I receive a TS error for the value prop, stating "Type 'string' is not assignable to type 'AutocompletePrediction | null | undefined'.". The main issue though is that when I type into the input, results aren't being displayed for some reason, and neither the Here's what I have so far:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Posting my fix here in case anyone else comes across this! import {
ClickAwayListener,
Grid,
TextField,
Typography,
} from '@material-ui/core';
import LocationOnIcon from '@material-ui/icons/LocationOn';
import parse from 'autosuggest-highlight/parse';
import Autocomplete from '@material-ui/lab/Autocomplete';
import usePlacesAutocomplete, {
getGeocode,
getLatLng,
} from 'use-places-autocomplete';
import { Coordinates } from './types';
interface Props {
onSelect: (coordinates: Coordinates) => void;
}
const EditLocation: React.FC<Props> = ({ onSelect }) => {
const {
value,
suggestions: { data },
setValue,
clearSuggestions,
} = usePlacesAutocomplete({
debounce: 300,
});
const handleInput = (
e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
) => {
setValue(e.target.value);
};
const handleSelect =
({ description }: { description: string }) =>
() => {
setValue(description, false);
clearSuggestions();
// Get latitude and longitude via utility functions
getGeocode({ address: description })
.then(results => getLatLng(results[0]))
.then(({ lat, lng }) => onSelect({ lat, lng }))
.catch(error => console.log('😱 Error: ', error));
};
return (
<ClickAwayListener onClickAway={() => clearSuggestions()}>
<Autocomplete
style={{ width: 300 }}
getOptionLabel={option =>
typeof option === 'string' ? option : option.description
}
filterOptions={x => x}
options={data}
autoComplete
includeInputInList
filterSelectedOptions
noOptionsText="No results"
value={data.find(x => x.description === value)} // <-- fixed TS error
renderInput={params => (
<TextField
{...params}
size="small"
label="Trip location"
variant="outlined"
fullWidth
onChange={e => handleInput(e)} // <-- moved from Autocomplete prop to here
/>
)}
renderOption={option => {
const matches =
option.structured_formatting.main_text_matched_substrings;
const parts = parse(
option.structured_formatting.main_text,
matches.map(match => [match.offset, match.offset + match.length])
);
return (
<Grid container alignItems="center" onClick={handleSelect(option)}>
<Grid item>
<LocationOnIcon />
</Grid>
<Grid item xs>
{parts.map((part, index) => (
<span
key={index}
style={{ fontWeight: part.highlight ? 700 : 400 }}>
{part.text}
</span>
))}
<Typography variant="body2" color="textSecondary">
{option.structured_formatting.secondary_text}
</Typography>
</Grid>
</Grid>
);
}}
/>
</ClickAwayListener>
);
};
export default EditLocation; |
Beta Was this translation helpful? Give feedback.
Posting my fix here in case anyone else comes across this!