trainvel/src/AutocompleteStation.jsx

38 lines
1.0 KiB
React
Raw Normal View History

2024-02-02 23:52:52 +00:00
import {Autocomplete, TextField} from "@mui/material";
2024-11-11 20:24:01 +00:00
import {useState} from "react";
2024-02-02 23:52:52 +00:00
2024-05-11 18:52:22 +00:00
function AutocompleteStation(params) {
2024-02-02 23:52:52 +00:00
const [options, setOptions] = useState([])
function onInputChange(event, value) {
if (!value) {
setOptions([])
return
}
2024-11-11 20:24:01 +00:00
fetch(`${process.env.REACT_APP_MOTIS_SERVER}/api/v1/geocode?language=fr&text=${value}`)
2024-02-02 23:52:52 +00:00
.then(response => response.json())
.then(setOptions)
}
return <>
<Autocomplete
id="stop"
options={options}
onInputChange={onInputChange}
2024-11-11 20:24:01 +00:00
filterOptions={(x) => x.filter(stop => stop.type === "STOP").filter(stop => !stop.id.startsWith("node/"))}
2024-02-02 23:52:52 +00:00
getOptionKey={option => option.id}
getOptionLabel={option => option.name}
groupBy={option => getOptionGroup(option)}
2024-02-02 23:52:52 +00:00
isOptionEqualToValue={(option, value) => option.id === value.id}
renderInput={(params) => <TextField {...params} label="Arrêt" />}
{...params} />
</>
}
function getOptionGroup(option) {
2024-11-11 20:24:01 +00:00
return option.id.split('_')[0]
}
2024-05-11 18:52:22 +00:00
export default AutocompleteStation;