-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSelector.tsx
37 lines (32 loc) · 1.11 KB
/
Selector.tsx
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
import classNames from 'classnames'
import './Selector.scss'
export interface ISelectorOption {
name: string;
value: string;
}
interface ISelectorProps {
onSelect: (option: string) => void;
options: ISelectorOption[];
className?: string;
value?: string;
label?: string;
testId?: string;
hideLabel?: boolean;
}
export const Selector = ({ onSelect, options, className, value, label, testId, hideLabel }: ISelectorProps) => {
const id = label ? label.toLowerCase().replace(/ /, '_') : null
return (
<div className={classNames('selector', className, { 'hide-label': hideLabel })}>
{label && <label className="selector__title" htmlFor={id}>{label}</label>}
<select
data-testid={testId}
id={id}
value={value}
className="selector__select"
onChange={(e: any) => onSelect(options[e.currentTarget.selectedIndex].value)}
>
{options.map((option, i) => <option key={i} value={option.value}>{option.name}</option>)}
</select>
</div>
)
}