import { Fragment, VNode, h } from "preact"; import { useState } from "preact/hooks"; import { UIFormProps } from "./FormProvider.js"; import { ChoiceS } from "./InputChoiceStacked.js"; import { LabelWithTooltipMaybeRequired } from "./InputLine.js"; import { useField } from "./useField.js"; import { useTranslationContext } from "../index.browser.js"; export function InputSelectMultiple( props: { choices: ChoiceS[]; unique?: boolean; max?: number; } & UIFormProps, ): VNode { const { name, label, choices, placeholder, tooltip, required, unique, max } = props; const { value, onChange, state } = useField(name); const [filter, setFilter] = useState(undefined); const regex = new RegExp(`.*${filter}.*`, "i"); const choiceMap = choices.reduce((prev, curr) => { return { ...prev, [curr.value as string]: curr.label }; }, {} as Record); const { i18n } = useTranslationContext(); const list = (value ?? []) as string[]; const filteredChoices = filter === undefined ? undefined : choices.filter((v) => { return regex.test(v.label); }); return (
{list.map((v, idx) => { return ( {choiceMap[v]} ); })} {!state.disabled &&
{ 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); if (unique && list.indexOf(v.value as string) !== -1) { return; } if (max !== undefined && list.length >= max) { return; } const newValue = [...list]; newValue.splice(0, 0, v.value as string); onChange(newValue as T[K]); }} // tabindex="-1" > {/* */} {v.label} {/* */}
  • ); })} {/* */} {/* */}
)}
}
); }