(props: SelectProps<T>)
| 15 | } |
| 16 | |
| 17 | export function Select<T extends string | number>(props: SelectProps<T>) { |
| 18 | const styles = createStyles() |
| 19 | const [selected, setSelected] = createSignal<T | undefined>( |
| 20 | () => props.value ?? props.options[0]?.value, |
| 21 | ) |
| 22 | const id = createUniqueId() |
| 23 | const descriptionId = `${id}-description` |
| 24 | |
| 25 | const handleChange = (e: Event) => { |
| 26 | const value = (e.target as HTMLSelectElement).value |
| 27 | const option = props.options.find( |
| 28 | (candidate) => String(candidate.value) === value, |
| 29 | ) |
| 30 | if (!option) return |
| 31 | setSelected(() => option.value) |
| 32 | props.onChange?.(option.value) |
| 33 | } |
| 34 | |
| 35 | return ( |
| 36 | <div class={styles().selectContainer}> |
| 37 | <div class={styles().selectWrapper}> |
| 38 | {props.label && ( |
| 39 | <label for={id} class={styles().selectLabel}> |
| 40 | {props.label} |
| 41 | </label> |
| 42 | )} |
| 43 | {props.description && ( |
| 44 | <p id={descriptionId} class={styles().selectDescription}> |
| 45 | {props.description} |
| 46 | </p> |
| 47 | )} |
| 48 | <select |
| 49 | id={id} |
| 50 | data-tsd-control |
| 51 | aria-describedby={props.description ? descriptionId : undefined} |
| 52 | class={styles().select} |
| 53 | value={selected()} |
nothing calls this directly
no test coverage detected