import { createSignal, createEffect, on, Show, For } from "solid-js" import { hope, useSelectContext, SelectTrigger, SelectPlaceholder, SelectValue, SelectIcon, Input, SelectContent, SelectListbox, SelectOption, SelectOptionText, SelectOptionIndicator, } from "@hope-ui/solid" import { BsSearch } from "solid-icons/bs" import { useT } from "~/hooks" interface SelectOptionsProps { options: { key: string; label: string }[] searchable?: boolean readonly?: boolean } const SearchIcon = hope(BsSearch, { baseStyle: { color: "$neutral11" } }) export const SelectOptions = (props: SelectOptionsProps) => { const t = useT() const selectContext = useSelectContext() const [searching, setSearching] = createSignal(false) const [displayValue, setDisplayValue] = createSignal("") let tid: ReturnType const displayOptions = () => { if (!props.searchable) return props.options return props.options.filter((o) => new RegExp(displayValue(), "i").test(o.label), ) } const selectedLabel = () => selectContext.state.selectedOptions.map((o) => o.textValue).join(",") createEffect(on(selectedLabel, setDisplayValue)) return ( <> {t("global.choose")} } when={!props.readonly && props.searchable} > e.stopPropagation()} onClick={(e) => { if (!selectContext.state.opened) return e.stopPropagation() }} onInput={(e) => { clearTimeout(tid) setDisplayValue(e.currentTarget.value) }} onBlur={() => { setSearching(false) tid = setTimeout( () => setDisplayValue(""), 300 /* transition duration of */, ) }} onFocus={() => { clearTimeout(tid) setDisplayValue("") setSearching(true) }} /> }> {(item) => ( {item.label} )} ) }