// A dropdown that matches the dashboard: a clear button, a solid list with a check on the current // choice, keyboard support, and a search box. Typing while it is closed opens it and searches. // // import { useEffect, useId, useMemo, useRef, useState } from "react"; import { Icon } from "../lib/icons.jsx"; import { tr } from "../lib/i18n.js"; export function Select({ value, onChange, options, label, size = "md", searchable = true, placeholder = "Choose…", width }) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const [active, setActive] = useState(0); const [up, setUp] = useState(false); const wrap = useRef(null), button = useRef(null), list = useRef(null), search = useRef(null); const id = useId(); const canSearch = searchable; const shown = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return options; // Best first: a word of the name starts with the text, then the name contains it, then the hint or value. const rank = ([v, text, hint]) => { const name = String(text).toLowerCase(); if (name.split(/[\s/(),·-]+/).some((w) => w.startsWith(q))) return 0; if (name.includes(q)) return 1; return `${hint || ""} ${v}`.toLowerCase().includes(q) ? 2 : -1; }; return options.map((o, i) => [rank(o), i, o]).filter(([r]) => r >= 0) .sort((x, y) => x[0] - y[0] || x[1] - y[1]).map(([, , o]) => o); }, [options, query]); const current = options.find(([v]) => v === value); const show = (typed = "") => { const b = button.current.getBoundingClientRect(); setUp(window.innerHeight - b.bottom < 340 && b.top > window.innerHeight - b.bottom); setQuery(canSearch ? typed : ""); setActive(typed ? 0 : Math.max(0, options.findIndex(([v]) => v === value))); setOpen(true); }; const close = (focus = true) => { setOpen(false); if (focus) button.current?.focus(); }; const pick = (v) => { if (v !== value) onChange(v); close(); }; // Close on a click outside; focus the search box (or the list) when it opens. useEffect(() => { if (!open) return undefined; const away = (e) => { if (!wrap.current?.contains(e.target)) close(false); }; document.addEventListener("pointerdown", away); (canSearch ? search.current : list.current)?.focus(); return () => document.removeEventListener("pointerdown", away); }, [open]); // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { if (open) list.current?.querySelector(`[data-i="${active}"]`)?.scrollIntoView({ block: "nearest" }); }, [active, open, shown]); const typed = useRef({ text: "", at: 0 }); const onKey = (e) => { if (e.key === "ArrowDown") { e.preventDefault(); setActive((a) => Math.min(shown.length - 1, a + 1)); } else if (e.key === "ArrowUp") { e.preventDefault(); setActive((a) => Math.max(0, a - 1)); } else if (e.key === "Home") { e.preventDefault(); setActive(0); } else if (e.key === "End") { e.preventDefault(); setActive(shown.length - 1); } else if (e.key === "Enter") { e.preventDefault(); if (shown[active]) pick(shown[active][0]); } else if (e.key === "Escape") { e.preventDefault(); close(); } else if (e.key === "Tab") close(false); else if (!canSearch && e.key.length === 1) { // Type-ahead: jump to the first option starting with what was typed. const now = Date.now(); typed.current = { text: (now - typed.current.at < 700 ? typed.current.text : "") + e.key.toLowerCase(), at: now }; const i = shown.findIndex(([, text]) => String(text).toLowerCase().startsWith(typed.current.text)); if (i >= 0) setActive(i); } }; return (