"use client"; import { useMemo, useState } from "react"; import { Combobox, InputBase, Loader, useCombobox } from "@mantine/core"; import { clientApi } from "@homarr/api/client"; import { useOptionalBoard } from "@homarr/boards/context"; import type { CommonWidgetInputProps } from "./common"; import { useWidgetInputTranslation } from "./common"; import { useFormContext } from "./form"; export const WidgetCustomWidgetSelectInput = ({ property, kind, options, }: CommonWidgetInputProps<"customWidgetSelect">) => { const t = useWidgetInputTranslation(kind, property); const form = useFormContext(); const board = useOptionalBoard(); const { data, isLoading } = clientApi.customWidget.available.useQuery( { boardId: board?.id ?? "" }, { enabled: board !== null }, ); const [search, setSearch] = useState(""); const currentValue = form.values.options[property] as string; const definitions = useMemo(() => (data ?? []).map((def) => ({ value: def.id, label: def.name })), [data]); const filteredOptions = useMemo( () => definitions.filter( (def) => def.label.toLowerCase().includes(search.toLowerCase()) || def.value.toLowerCase().includes(search.toLowerCase()), ), [definitions, search], ); const selectedLabel = definitions.find((d) => d.value === currentValue)?.label; const combobox = useCombobox({ onDropdownClose: () => { combobox.resetSelectedOption(); setSearch(""); }, }); return ( { form.setFieldValue(`options.${property}`, val); combobox.closeDropdown(); }} > : } rightSectionPointerEvents="none" value={combobox.dropdownOpened ? search : (selectedLabel ?? currentValue)} onChange={(event) => { setSearch(event.currentTarget.value); form.setFieldValue(`options.${property}`, event.currentTarget.value); combobox.openDropdown(); combobox.updateSelectedOptionIndex(); }} onClick={() => combobox.openDropdown()} onFocus={() => combobox.openDropdown()} onBlur={() => combobox.closeDropdown()} placeholder={t("label")} /> {filteredOptions.map((def) => ( {def.label} ))} {filteredOptions.length === 0 && !isLoading && No definitions found} ); };