forked from mudler/LocalAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchableModelSelect.jsx
More file actions
176 lines (166 loc) · 5.3 KB
/
Copy pathSearchableModelSelect.jsx
File metadata and controls
176 lines (166 loc) · 5.3 KB
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
import { useState, useEffect, useRef, useCallback } from 'react'
import { useModels } from '../hooks/useModels'
export default function SearchableModelSelect({ value, onChange, capability, placeholder = 'Type or select a model...', style }) {
const { models, loading } = useModels(capability)
const [query, setQuery] = useState('')
const [open, setOpen] = useState(false)
const [focusIndex, setFocusIndex] = useState(-1)
const wrapperRef = useRef(null)
const listRef = useRef(null)
// Sync external value into the input
useEffect(() => {
setQuery(value || '')
}, [value])
// Close on outside click
useEffect(() => {
const handler = (e) => {
if (wrapperRef.current && !wrapperRef.current.contains(e.target)) setOpen(false)
}
document.addEventListener('mousedown', handler)
return () => document.removeEventListener('mousedown', handler)
}, [])
const filtered = models.filter(m =>
m.id.toLowerCase().includes(query.toLowerCase())
)
// Which item Enter will select — matches SearchableSelect behavior
const enterTargetIndex = focusIndex >= 0 ? focusIndex
: filtered.length > 0 ? 0
: -1
const commit = useCallback((val) => {
setQuery(val)
onChange(val)
setOpen(false)
setFocusIndex(-1)
}, [onChange])
const handleKeyDown = (e) => {
if (!open && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
setOpen(true)
return
}
if (!open && e.key === 'Enter') {
e.preventDefault()
commit(query)
return
}
if (!open) return
if (e.key === 'ArrowDown') {
e.preventDefault()
setFocusIndex(i => Math.min(i + 1, filtered.length - 1))
} else if (e.key === 'ArrowUp') {
e.preventDefault()
setFocusIndex(i => Math.max(i - 1, 0))
} else if (e.key === 'Enter') {
e.preventDefault()
if (enterTargetIndex >= 0) {
commit(filtered[enterTargetIndex].id)
} else {
commit(query)
}
} else if (e.key === 'Escape') {
setOpen(false)
setFocusIndex(-1)
}
}
// Scroll focused item into view
useEffect(() => {
if (focusIndex >= 0 && listRef.current) {
const item = listRef.current.children[focusIndex]
if (item) item.scrollIntoView({ block: 'nearest' })
}
}, [focusIndex])
return (
<div ref={wrapperRef} className="searchable-model-select" style={style}>
<style>{`
.searchable-model-select {
position: relative;
width: 280px;
}
.searchable-model-select input {
width: 100%;
}
.sms-dropdown {
position: absolute;
top: 100%;
left: 0;
right: 0;
z-index: 50;
max-height: 220px;
overflow-y: auto;
background: var(--color-bg-primary);
border: 1px solid var(--color-border);
border-radius: var(--radius-md);
box-shadow: var(--shadow-md);
animation: dropdownIn 120ms ease-out;
margin-top: 2px;
}
.sms-item {
padding: 6px 10px;
font-size: 0.8125rem;
cursor: pointer;
display: flex;
align-items: center;
gap: 6px;
}
.sms-item:hover, .sms-item.sms-focused {
background: var(--color-bg-tertiary);
}
.sms-item.sms-active {
color: var(--color-primary);
font-weight: 600;
}
.sms-empty {
padding: 8px 10px;
font-size: 0.8125rem;
color: var(--color-text-muted);
}
`}</style>
<input
className="input"
aria-haspopup="listbox"
aria-expanded={open}
value={query}
onChange={(e) => {
setQuery(e.target.value)
setOpen(true)
setFocusIndex(-1)
// Commit on every keystroke so the parent always has current value
onChange(e.target.value)
}}
onFocus={() => setOpen(true)}
onKeyDown={handleKeyDown}
placeholder={loading ? 'Loading models...' : placeholder}
/>
{open && !loading && (
<div className="sms-dropdown" ref={listRef} role="listbox">
{filtered.length === 0 ? (
<div className="sms-empty">
{query ? 'No matching models — value will be used as-is' : 'No models available'}
</div>
) : (
filtered.map((m, i) => {
const isEnterTarget = i === enterTargetIndex
return (
<div
key={m.id}
role="option"
aria-selected={m.id === value}
className={`sms-item${i === focusIndex || isEnterTarget ? ' sms-focused' : ''}${m.id === value ? ' sms-active' : ''}`}
onMouseEnter={() => setFocusIndex(i)}
onMouseDown={(e) => {
e.preventDefault()
commit(m.id)
}}
>
<span style={{ flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{m.id}</span>
{isEnterTarget && (
<span style={{ color: 'var(--color-text-muted)', fontSize: '0.75rem', flexShrink: 0 }}>↵</span>
)}
</div>
)
})
)}
</div>
)}
</div>
)
}