Spaces:
Running
Running
File size: 6,991 Bytes
94004a2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Search, ChevronDown, Check, Star, Clock } from 'lucide-react';
const MAX_DISPLAY = 200;
const readRecents = (key) => {
if (!key || typeof window === 'undefined') return [];
try {
const raw = window.localStorage.getItem(key);
return raw ? JSON.parse(raw) : [];
} catch { return []; }
};
const writeRecents = (key, list) => {
if (!key || typeof window === 'undefined') return;
try { window.localStorage.setItem(key, JSON.stringify(list.slice(0, 8))); } catch {}
};
const normalize = (s) => (s || '').toString().toLowerCase();
export default function SearchableSelect({
value,
onChange,
options,
placeholder = 'Select…',
popular = [],
recentsKey = '',
renderLabel,
renderOption,
disabled = false,
buttonStyle,
buttonClassName = 'input-base',
size = 'md',
}) {
const [open, setOpen] = useState(false);
const [query, setQuery] = useState('');
const [highlight, setHighlight] = useState(0);
const [recents, setRecents] = useState(() => readRecents(recentsKey));
const wrapRef = useRef(null);
const listRef = useRef(null);
const inputRef = useRef(null);
const getVal = useCallback((o) => (typeof o === 'string' ? o : o?.value), []);
const getLabel = useCallback((o) => {
if (renderLabel) return renderLabel(o);
if (typeof o === 'string') return o;
return o?.label ?? o?.value ?? '';
}, [renderLabel]);
const byVal = useMemo(() => {
const m = new Map();
for (const o of options) m.set(getVal(o), o);
return m;
}, [options, getVal]);
const currentLabel = useMemo(() => {
const o = byVal.get(value);
return o ? getLabel(o) : (value || placeholder);
}, [byVal, value, getLabel, placeholder]);
const filtered = useMemo(() => {
const q = normalize(query);
if (!q) return options;
return options.filter(o => normalize(getLabel(o)).includes(q) || normalize(getVal(o)).includes(q));
}, [options, query, getLabel, getVal]);
const pinned = useMemo(() => {
if (query) return [];
const out = [];
const seen = new Set();
for (const v of recents) {
const o = byVal.get(v);
if (o && !seen.has(v)) { out.push({ o, kind: 'recent' }); seen.add(v); }
if (out.length >= 5) break;
}
for (const v of popular) {
if (seen.has(v)) continue;
const o = byVal.get(v);
if (o) { out.push({ o, kind: 'popular' }); seen.add(v); }
if (out.length >= 12) break;
}
return out;
}, [query, recents, popular, byVal]);
const displayed = useMemo(() => filtered.slice(0, MAX_DISPLAY), [filtered]);
const flatItems = useMemo(() => {
const list = [];
for (const p of pinned) list.push({ o: p.o, kind: p.kind });
for (const o of displayed) list.push({ o, kind: 'main' });
return list;
}, [pinned, displayed]);
useEffect(() => {
if (!open) return;
const onDoc = (e) => {
if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false);
};
document.addEventListener('mousedown', onDoc);
return () => document.removeEventListener('mousedown', onDoc);
}, [open]);
useEffect(() => {
if (open) {
setHighlight(0);
setTimeout(() => inputRef.current?.focus(), 0);
} else {
setQuery('');
}
}, [open]);
useEffect(() => {
if (!open || !listRef.current) return;
const el = listRef.current.querySelector(`[data-idx="${highlight}"]`);
if (el) el.scrollIntoView({ block: 'nearest' });
}, [highlight, open]);
const commit = (o) => {
const v = getVal(o);
onChange?.(v);
if (recentsKey) {
const next = [v, ...recents.filter(r => r !== v)].slice(0, 8);
setRecents(next);
writeRecents(recentsKey, next);
}
setOpen(false);
};
const onKey = (e) => {
if (e.key === 'ArrowDown') { e.preventDefault(); setHighlight(h => Math.min(flatItems.length - 1, h + 1)); }
else if (e.key === 'ArrowUp') { e.preventDefault(); setHighlight(h => Math.max(0, h - 1)); }
else if (e.key === 'Enter') {
e.preventDefault();
const item = flatItems[highlight];
if (item) commit(item.o);
} else if (e.key === 'Escape') { e.preventDefault(); setOpen(false); }
};
const sizeCls = size === 'sm' ? 'ss-sm' : 'ss-md';
return (
<div ref={wrapRef} className={`ss-wrap ${sizeCls}`}>
<button
type="button"
className={`${buttonClassName} ss-trigger`}
style={buttonStyle}
onClick={() => !disabled && setOpen(o => !o)}
disabled={disabled}
aria-haspopup="listbox"
aria-expanded={open}
title={currentLabel}
>
<span className="ss-trigger-label">{currentLabel}</span>
<ChevronDown size={12} className="ss-chev" />
</button>
{open && (
<div className="ss-pop" role="listbox">
<div className="ss-search">
<Search size={12} className="ss-search-icon" />
<input
ref={inputRef}
className="ss-search-input"
placeholder="Search…"
value={query}
onChange={e => { setQuery(e.target.value); setHighlight(0); }}
onKeyDown={onKey}
/>
</div>
<div ref={listRef} className="ss-list">
{flatItems.length === 0 && (
<div className="ss-empty">No matches</div>
)}
{pinned.length > 0 && (
<div className="ss-group-label">
{recents.length ? <><Clock size={9}/> Recent & Popular</> : <><Star size={9}/> Popular</>}
</div>
)}
{flatItems.map((it, idx) => {
const v = getVal(it.o);
const selected = v === value;
const highlighted = idx === highlight;
return (
<div
key={`${it.kind}-${v}-${idx}`}
data-idx={idx}
className={`ss-option ${highlighted ? 'ss-hl' : ''} ${selected ? 'ss-sel' : ''}`}
onMouseEnter={() => setHighlight(idx)}
onMouseDown={(e) => { e.preventDefault(); commit(it.o); }}
role="option"
aria-selected={selected}
>
{it.kind === 'recent' && <Clock size={9} className="ss-kind-icon"/>}
{it.kind === 'popular' && <Star size={9} className="ss-kind-icon"/>}
<span className="ss-option-label">
{renderOption ? renderOption(it.o) : getLabel(it.o)}
</span>
{selected && <Check size={10} className="ss-check"/>}
</div>
);
})}
{!query && filtered.length > MAX_DISPLAY && (
<div className="ss-more">Showing {MAX_DISPLAY} of {filtered.length}. Type to search…</div>
)}
</div>
</div>
)}
</div>
);
}
|