-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathArma3ScenariosAutocomplete.tsx
More file actions
63 lines (56 loc) · 1.96 KB
/
Arma3ScenariosAutocomplete.tsx
File metadata and controls
63 lines (56 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Autocomplete, AutocompleteValue, Box, TextField } from "@mui/material";
import React, { useEffect, useState } from "react";
import { Arma3ScenarioDto } from "../../dtos/Arma3ScenarioDto.ts";
import { getScenarios } from "../../services/scenarioService.ts";
type Arma3ScenariosAutocompleteProps = {
onChange: (_: any, value: AutocompleteValue<Arma3ScenarioDto | string, false, false, true> | null) => void,
};
export function Arma3ScenariosAutocomplete({onChange}: Arma3ScenariosAutocompleteProps) {
const [scenarios, setScenarios] = useState<Array<Arma3ScenarioDto>>([]);
useEffect(() => {
const fetchScenarios = async () => {
const { data: scenariosDto } = await getScenarios();
setScenarios(scenariosDto.scenarios);
};
fetchScenarios();
onChange(null, "");
}, []);
const getScenarioDisplayName = (scenario: Arma3ScenarioDto) => {
return scenario.name; // TOOD: Can make pretty later
};
const handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {
onChange(null, e.target.value);
};
return <Autocomplete
id="scenario-select"
fullWidth
freeSolo
selectOnFocus
onBlur={handleBlur}
handleHomeEndKeys
options={scenarios}
autoHighlight
getOptionLabel={(option) => {
if (typeof option === "string")
return option;
return option.name;
}}
onChange={onChange}
renderOption={(props, option) => (
<Box component="li" {...props}>
{getScenarioDisplayName(option)}
</Box>
)}
renderInput={(params) => (
<TextField
{...params}
name="scenarioName"
label="Scenario name"
inputProps={{
...params.inputProps,
autoComplete: 'new-password'
}}
/>
)}
/>;
}