import { Fragment, VNode, h } from "preact"; import { Choice } from "./InputChoiceStacked.js"; import { LabelWithTooltipMaybeRequired, UIFormProps } from "./InputLine.js"; import { useField } from "./useField.js"; import { useState } from "preact/hooks"; export function InputSelectOne( props: { choices: Choice[]; } & UIFormProps, ): VNode { const { name, label, choices, placeholder, tooltip, required } = props; const { value, onChange } = useField(name); const [filter, setFilter] = useState(undefined); const regex = new RegExp(`.*${filter}.*`, "i"); const choiceMap = choices.reduce((prev, curr) => { return { ...prev, [curr.value]: curr.label }; }, {} as Record); const filteredChoices = filter === undefined ? undefined : choices.filter((v) => { return regex.test(v.label); }); return (
{value ? ( {choiceMap[value as string]} ) : (
{ setFilter(e.currentTarget.value); }} placeholder={placeholder} class="w-full rounded-md border-0 bg-white py-1.5 pl-3 pr-12 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6" role="combobox" aria-controls="options" aria-expanded="false" /> {filteredChoices !== undefined && (
    {filteredChoices.map((v, idx) => { return (
  • { setFilter(undefined); onChange(v.value as T[K]); }} // tabindex="-1" > {/* */} {v.label} {/* */}
  • ); })} {/* */} {/* */}
)}
)}
); }