EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
LiveSearchSelect.jsx
Go to the documentation of this file.
1// Simple live search select component for large lists
2
3import { useState, useEffect } from 'react';
4import { apiFetch } from '../lib/api';
5
6export default function LiveSearchSelect({ options, value, onChange, placeholder, labelKey = 'name', valueKey = 'id', disabled, resetSearch }) {
7 const [search, setSearch] = useState('');
8
9 useEffect(() => {
10 if (resetSearch) setSearch('');
11 }, [resetSearch]);
12
13 const filtered = Array.isArray(options)
14 ? options.filter(opt =>
15 (opt[labelKey] || '').toString().toLowerCase().includes(search.toLowerCase())
16 )
17 : [];
18
19 return (
20 <div style={{ position: 'relative' }}>
21 <input
22 type="text"
23 value={search}
24 onChange={e => setSearch(e.target.value)}
25 placeholder={placeholder || 'Search...'}
26 style={{ width: '100%', marginBottom: 4, padding: 6, borderRadius: 4, border: '1px solid #ccc', fontSize: 14 }}
27 disabled={disabled}
28 />
29 <select
30 value={value}
31 onChange={e => onChange(e.target.value)}
32 style={{ width: '100%', padding: 6, borderRadius: 4, border: '1px solid #ccc', fontSize: 14 }}
33 disabled={disabled}
34 >
35 <option value="">Select...</option>
36 {filtered.map(opt => (
37 <option key={opt[valueKey]} value={opt[valueKey]}>{opt[labelKey]}</option>
38 ))}
39 </select>
40 </div>
41 );
42}