-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathselect.tsx
More file actions
50 lines (45 loc) · 1.31 KB
/
select.tsx
File metadata and controls
50 lines (45 loc) · 1.31 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
import { createSignal } from 'solid-js'
import { useStyles } from '../styles/use-styles'
interface SelectOption<T extends string | number> {
value: T
label: string
}
interface SelectProps<T extends string | number> {
label?: string
options: Array<SelectOption<T>>
value?: T
onChange?: (value: T) => void
description?: string
}
export function Select<T extends string | number>(props: SelectProps<T>) {
const styles = useStyles()
const [selected, setSelected] = createSignal(
props.value || props.options[0]?.value,
)
const handleChange = (e: Event) => {
const value = (e.target as HTMLSelectElement).value as T
setSelected((prev) => (prev !== value ? value : prev))
props.onChange?.(value)
}
return (
<div class={styles().selectContainer}>
<div class={styles().selectWrapper}>
{props.label && (
<label class={styles().selectLabel}>{props.label}</label>
)}
{props.description && (
<p class={styles().selectDescription}>{props.description}</p>
)}
<select
class={styles().select}
value={selected()}
onInput={handleChange}
>
{props.options.map((opt) => (
<option value={opt.value}>{opt.label}</option>
))}
</select>
</div>
</div>
)
}