You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.0 KiB
TypeScript

import { StateUpdater } from 'preact/hooks'
import { JSX } from 'preact/jsx-runtime'
type Props<K extends string, T extends K> = {
options: Record<K, string | JSX.Element>
value: T
setValue?: StateUpdater<K>
}
export const Select = <K extends string, T extends K>({ options, value, setValue }: Props<K, T>) => {
const optionValues = new Set(Object.keys(options))
return (
<div class="input-select">
<select
onInput={e => {
if (e.target instanceof HTMLSelectElement && optionValues.has(e.target.value)) {
setValue?.(e.target.value as K)
}
}}
>
{Object.entries<string | JSX.Element>(options).map(([k, v]) => (
<option value={k} selected={value === k}>
{v}
</option>
))}
</select>
<span class="material-symbols-outlined">expand_more</span>
</div>
)
}