-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSelect.tsx
More file actions
73 lines (69 loc) · 1.89 KB
/
Copy pathSelect.tsx
File metadata and controls
73 lines (69 loc) · 1.89 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
64
65
66
67
68
69
70
71
72
73
import React from 'react'
import FormControl from '@mui/material/FormControl'
import FormHelperText from '@mui/material/FormHelperText'
import InputLabel from '@mui/material/InputLabel'
import MenuItem from '@mui/material/MenuItem'
import MuiSelect, { SelectChangeEvent } from '@mui/material/Select'
export interface SelectOption {
label: string
value: string
}
export interface SelectProps {
'data-test'?: string
error?: boolean
fullWidth?: boolean
helperText?: string
label: string
name: string
onChange: (e: { target: { name: string; value: string } }) => void
options?: SelectOption[]
placeholder?: string
value: string
}
export const Select = ({
'data-test': dataTest,
error,
fullWidth = true,
helperText,
label,
name,
onChange,
options = [],
placeholder = 'Select a value',
value,
}: SelectProps) => {
const handleChange = (e: SelectChangeEvent) => {
onChange({ target: { name, value: e.target.value as string } })
}
return (
<FormControl error={error} fullWidth={fullWidth}>
<InputLabel id={`${name}-label`}>{label}</InputLabel>
<MuiSelect
displayEmpty={true}
inputProps={{ 'data-test': dataTest }}
label={label}
labelId={`${name}-label`}
name={name}
onChange={handleChange}
renderValue={(selected) => {
if (selected === '') {
return placeholder
}
const option = options.find((opt) => opt.value === selected)
return option ? option.label : selected
}}
value={value}
>
<MenuItem disabled={true} value="">
{placeholder}
</MenuItem>
{options.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</MuiSelect>
{helperText && <FormHelperText>{helperText}</FormHelperText>}
</FormControl>
)
}