(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( |
| 20 | props.value ?? props.options[0]?.value, |
| 21 | ) |
| 22 | const id = createUniqueId() |
| 23 | const descriptionId = `${id}-description` |
| 24 | |
| 25 | createEffect(() => { |
| 26 | if (props.value !== undefined) { |
| 27 | setSelected(() => props.value) |
| 28 | } |
| 29 | }) |
| 30 | |
| 31 | const handleChange = (e: Event) => { |
| 32 | const value = (e.target as HTMLSelectElement).value |
| 33 | const option = props.options.find( |
| 34 | (candidate) => String(candidate.value) === value, |
| 35 | ) |
| 36 | if (!option) return |
| 37 | setSelected(() => option.value) |
| 38 | props.onChange?.(option.value) |
| 39 | } |
| 40 | |
| 41 | return ( |
| 42 | <div class={styles().selectContainer}> |
| 43 | <div class={styles().selectWrapper}> |
| 44 | {props.label && ( |
| 45 | <label for={id} class={styles().selectLabel}> |
| 46 | {props.label} |
| 47 | </label> |
| 48 | )} |
| 49 | {props.description && ( |
| 50 | <p id={descriptionId} class={styles().selectDescription}> |
| 51 | {props.description} |
| 52 | </p> |
| 53 | )} |
nothing calls this directly
no test coverage detected