2024-02-02 23:52:52 +00:00
|
|
|
import {Autocomplete, TextField} from "@mui/material";
|
|
|
|
import {useRef, useState} from "react";
|
|
|
|
|
2024-05-11 18:52:22 +00:00
|
|
|
function AutocompleteStation(params) {
|
2024-02-02 23:52:52 +00:00
|
|
|
const [options, setOptions] = useState([])
|
|
|
|
const previousController = useRef()
|
|
|
|
|
|
|
|
function onInputChange(event, value) {
|
|
|
|
if (!value) {
|
|
|
|
setOptions([])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousController.current)
|
|
|
|
previousController.current.abort()
|
|
|
|
|
|
|
|
const controller = new AbortController()
|
|
|
|
const signal = controller.signal
|
|
|
|
previousController.current = controller
|
2024-05-11 18:52:22 +00:00
|
|
|
fetch("/api/core/station/?search=" + value, {signal})
|
2024-02-02 23:52:52 +00:00
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => data.results)
|
|
|
|
.then(setOptions)
|
|
|
|
.catch()
|
|
|
|
}
|
|
|
|
|
|
|
|
return <>
|
|
|
|
<Autocomplete
|
|
|
|
id="stop"
|
|
|
|
options={options}
|
|
|
|
onInputChange={onInputChange}
|
|
|
|
filterOptions={(x) => x}
|
|
|
|
getOptionKey={option => option.id}
|
|
|
|
getOptionLabel={option => option.name}
|
2024-02-10 16:33:36 +00:00
|
|
|
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} />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
|
2024-02-10 16:33:36 +00:00
|
|
|
function getOptionGroup(option) {
|
2024-05-11 18:52:22 +00:00
|
|
|
return option.country
|
2024-02-10 16:33:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-11 18:52:22 +00:00
|
|
|
export default AutocompleteStation;
|