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.
27 lines
605 B
TypeScript
27 lines
605 B
TypeScript
import { h } from "preact";
|
|
import { useRef } from "preact/hooks";
|
|
import { Callback } from "../../../utils.ts";
|
|
|
|
export type Options<Values extends string> = Record<Values, string>;
|
|
|
|
export const Select = <Values extends string>(p: {
|
|
value: Values;
|
|
options: Options<Values>;
|
|
onChange: Callback<[Values]>;
|
|
}) => {
|
|
const ref = useRef<HTMLSelectElement>(null);
|
|
const select = h(
|
|
"select",
|
|
{
|
|
ref,
|
|
value: p.value,
|
|
onChange: () => p.onChange(ref.current!.value as Values),
|
|
},
|
|
...Object.entries(p.options).map(([k, v]) =>
|
|
h("option", { value: `${v}` }, k),
|
|
),
|
|
);
|
|
|
|
return select;
|
|
};
|