@wcomp/elementum 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +282 -2
- package/custom-elements.json +3613 -0
- package/mcp/server.js +593 -0
- package/package.json +77 -4
- package/skills/elementum-break-ui/CATALOG.md +11 -0
- package/skills/elementum-break-ui/SKILL.md +16 -0
- package/skills/elementum-mobile/SKILL.md +18 -0
- package/skills/elementum-motion/SKILL.md +18 -0
- package/skills/elementum-motion/STANDARDS.md +15 -0
- package/skills/elementum-penpot/SKILL.md +45 -0
- package/skills/elementum-ui/CATALOG.md +14 -0
- package/skills/elementum-ui/SKILL.md +21 -0
- package/src/ag-ui.js +474 -0
- package/src/behaviors/index.js +760 -0
- package/src/components/el-accordion.js +201 -0
- package/src/components/el-alert-dialog.js +186 -0
- package/src/components/el-alert.js +254 -0
- package/src/components/el-aspect-ratio.js +73 -0
- package/src/components/el-autocomplete.js +775 -0
- package/src/components/el-avatar.js +271 -0
- package/src/components/el-badge.js +131 -0
- package/src/components/el-border-beam.js +135 -0
- package/src/components/el-breadcrumb.js +203 -0
- package/src/components/el-button.js +231 -0
- package/src/components/el-calendar.js +436 -0
- package/src/components/el-callout.js +158 -0
- package/src/components/el-card.js +298 -0
- package/src/components/el-carousel.js +308 -0
- package/src/components/el-chart.js +632 -0
- package/src/components/el-checkbox-card.js +221 -0
- package/src/components/el-checkbox.js +208 -0
- package/src/components/el-chip.js +276 -0
- package/src/components/el-code-block.js +230 -0
- package/src/components/el-collapsible.js +124 -0
- package/src/components/el-command.js +556 -0
- package/src/components/el-context-menu.js +328 -0
- package/src/components/el-data-list.js +210 -0
- package/src/components/el-datepicker.js +192 -0
- package/src/components/el-dialog.js +272 -0
- package/src/components/el-diff-viewer.js +441 -0
- package/src/components/el-drawer.js +782 -0
- package/src/components/el-dropdown.js +182 -0
- package/src/components/el-dropzone.js +242 -0
- package/src/components/el-empty-state.js +130 -0
- package/src/components/el-field.js +394 -0
- package/src/components/el-form.js +185 -0
- package/src/components/el-input.js +229 -0
- package/src/components/el-inset.js +90 -0
- package/src/components/el-insight-card.js +263 -0
- package/src/components/el-kbd.js +151 -0
- package/src/components/el-menubar.js +254 -0
- package/src/components/el-meter.js +183 -0
- package/src/components/el-navbar.js +294 -0
- package/src/components/el-navigation-menu.js +327 -0
- package/src/components/el-number-field.js +510 -0
- package/src/components/el-number-flow.js +164 -0
- package/src/components/el-otp-input.js +266 -0
- package/src/components/el-pagination.js +231 -0
- package/src/components/el-popover.js +288 -0
- package/src/components/el-preview-card.js +202 -0
- package/src/components/el-progress.js +179 -0
- package/src/components/el-prompt-input.js +383 -0
- package/src/components/el-radio-card.js +360 -0
- package/src/components/el-radio.js +257 -0
- package/src/components/el-rating.js +165 -0
- package/src/components/el-reasoning-card.js +293 -0
- package/src/components/el-resizable.js +324 -0
- package/src/components/el-router.js +305 -0
- package/src/components/el-scroll-area.js +150 -0
- package/src/components/el-segmented-control.js +282 -0
- package/src/components/el-select.js +220 -0
- package/src/components/el-separator.js +85 -0
- package/src/components/el-sidebar.js +386 -0
- package/src/components/el-skeleton.js +156 -0
- package/src/components/el-slider.js +351 -0
- package/src/components/el-sources.js +288 -0
- package/src/components/el-spinner.js +159 -0
- package/src/components/el-streaming-text.js +199 -0
- package/src/components/el-switch.js +221 -0
- package/src/components/el-table.js +397 -0
- package/src/components/el-tabs.js +235 -0
- package/src/components/el-task-list.js +230 -0
- package/src/components/el-textarea.js +175 -0
- package/src/components/el-theme-switch.js +120 -0
- package/src/components/el-timeline.js +254 -0
- package/src/components/el-toast.js +778 -0
- package/src/components/el-toggle.js +311 -0
- package/src/components/el-tool-call.js +386 -0
- package/src/components/el-toolbar.js +183 -0
- package/src/components/el-tooltip.js +172 -0
- package/src/components/el-tree-view.js +261 -0
- package/src/components/el-visually-hidden.js +32 -0
- package/src/elementum.js +504 -0
- package/src/index.d.ts +1093 -0
- package/src/index.js +112 -0
- package/src/signal.js +215 -0
- package/src/ssr.js +136 -0
- package/src/template.js +321 -0
- package/src/tokens.css +416 -0
- package/tokens/figma-tokens.json +69 -0
- package/tokens/penpot-tokens.json +98 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
import { Elementum } from '../elementum.js';
|
|
2
|
+
import { html, css } from '../template.js';
|
|
3
|
+
import { signal, effect } from '../signal.js';
|
|
4
|
+
import './el-chip.js';
|
|
5
|
+
import './el-spinner.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* <el-autocomplete>
|
|
9
|
+
* Form-associated combobox component supporting static data arrays,
|
|
10
|
+
* remote URL queries with debouncing, flexible key mapping, multi-select with inline chips,
|
|
11
|
+
* and ARIA 1.2 / WCAG 2.2 accessibility.
|
|
12
|
+
*
|
|
13
|
+
* @attr {string} name - Form submission field name
|
|
14
|
+
* @attr {string|Array} value - Form submission resolved value
|
|
15
|
+
* @attr {string} placeholder - Placeholder text
|
|
16
|
+
* @attr {boolean} disabled - Whether the component is disabled
|
|
17
|
+
* @attr {boolean} required - Whether selection is required
|
|
18
|
+
* @attr {boolean} multiple - Enable multi-selection with inline chips
|
|
19
|
+
* @attr {string} url - Remote endpoint URL
|
|
20
|
+
* @attr {string} query-param - Query param name for remote fetch (default: 'q')
|
|
21
|
+
* @attr {number} debounce - Milliseconds to debounce remote queries (default: 250)
|
|
22
|
+
* @attr {number} min-chars - Minimum characters required before querying (default: 1)
|
|
23
|
+
* @attr {string} label-key - Property name for display label (default: 'label')
|
|
24
|
+
* @attr {string} value-key - Property name for submission value (default: 'value')
|
|
25
|
+
* @attr {string} size - Size variant ('sm', 'md', 'lg')
|
|
26
|
+
*
|
|
27
|
+
* @fires change - Dispatched when value changes
|
|
28
|
+
* @fires select - Dispatched when an option is selected with { item, value, label }
|
|
29
|
+
* @fires remove - Dispatched in multi-mode when a chip is removed
|
|
30
|
+
* @fires clear - Dispatched when input is cleared
|
|
31
|
+
*/
|
|
32
|
+
export class ElAutocomplete extends Elementum {
|
|
33
|
+
static formAssociated = true;
|
|
34
|
+
|
|
35
|
+
static observedAttributes = [
|
|
36
|
+
'name', 'value', 'placeholder', 'disabled', 'required',
|
|
37
|
+
'multiple', 'url', 'query-param', 'debounce', 'min-chars',
|
|
38
|
+
'label-key', 'value-key', 'size', 'options', 'data'
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
name = signal('');
|
|
42
|
+
value = signal('');
|
|
43
|
+
displayLabel = signal('');
|
|
44
|
+
placeholder = signal('Search or type...');
|
|
45
|
+
disabled = signal(false);
|
|
46
|
+
required = signal(false);
|
|
47
|
+
multiple = signal(false);
|
|
48
|
+
url = signal('');
|
|
49
|
+
queryParam = signal('q');
|
|
50
|
+
debounce = signal(250);
|
|
51
|
+
minChars = signal(1);
|
|
52
|
+
labelKey = signal('label');
|
|
53
|
+
valueKey = signal('value');
|
|
54
|
+
size = signal('md');
|
|
55
|
+
|
|
56
|
+
// Internal reactivity
|
|
57
|
+
data = signal([]);
|
|
58
|
+
suggestions = signal([]);
|
|
59
|
+
isOpen = signal(false);
|
|
60
|
+
isLoading = signal(false);
|
|
61
|
+
activeIndex = signal(-1);
|
|
62
|
+
selectedItem = signal(null);
|
|
63
|
+
selectedItems = signal([]);
|
|
64
|
+
announcement = signal('');
|
|
65
|
+
|
|
66
|
+
#debounceTimer = null;
|
|
67
|
+
#abortController = null;
|
|
68
|
+
#listboxId = `el-ac-listbox-${Math.random().toString(36).slice(2, 9)}`;
|
|
69
|
+
|
|
70
|
+
static styles = css`
|
|
71
|
+
:host {
|
|
72
|
+
display: inline-block;
|
|
73
|
+
position: relative;
|
|
74
|
+
font-family: var(--el-font-sans, system-ui, -apple-system, sans-serif);
|
|
75
|
+
width: 100%;
|
|
76
|
+
box-sizing: border-box;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.combobox-wrapper {
|
|
80
|
+
position: relative;
|
|
81
|
+
display: flex;
|
|
82
|
+
flex-wrap: wrap;
|
|
83
|
+
align-items: center;
|
|
84
|
+
gap: 4px;
|
|
85
|
+
width: 100%;
|
|
86
|
+
border: 1px solid var(--el-surface-border, #cbd5e1);
|
|
87
|
+
border-radius: var(--el-radius-md, 8px);
|
|
88
|
+
background-color: var(--el-surface-card, #ffffff);
|
|
89
|
+
color: var(--el-text-primary, #0f172a);
|
|
90
|
+
transition: border-color 0.15s ease, box-shadow 0.15s ease;
|
|
91
|
+
box-sizing: border-box;
|
|
92
|
+
padding: 3px 8px;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.combobox-wrapper:focus-within {
|
|
96
|
+
border-color: var(--el-color-primary, #2563eb);
|
|
97
|
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
|
|
98
|
+
outline: none;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.combobox-wrapper.is-disabled {
|
|
102
|
+
background-color: var(--el-surface-muted, #f1f5f9);
|
|
103
|
+
border-color: var(--el-surface-border, #e2e8f0);
|
|
104
|
+
cursor: not-allowed;
|
|
105
|
+
opacity: 0.65;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* Size variants */
|
|
109
|
+
.size-sm {
|
|
110
|
+
min-height: 32px;
|
|
111
|
+
font-size: 0.8125rem;
|
|
112
|
+
}
|
|
113
|
+
.size-md {
|
|
114
|
+
min-height: 40px;
|
|
115
|
+
font-size: 0.875rem;
|
|
116
|
+
}
|
|
117
|
+
.size-lg {
|
|
118
|
+
min-height: 48px;
|
|
119
|
+
font-size: 1rem;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
.prefix-area {
|
|
123
|
+
display: inline-flex;
|
|
124
|
+
align-items: center;
|
|
125
|
+
margin-right: 4px;
|
|
126
|
+
color: var(--el-text-muted, #64748b);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.chips-group {
|
|
130
|
+
display: inline-flex;
|
|
131
|
+
align-items: center;
|
|
132
|
+
flex-wrap: wrap;
|
|
133
|
+
gap: 4px;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
.combobox-input {
|
|
137
|
+
flex: 1 1 80px;
|
|
138
|
+
min-width: 80px;
|
|
139
|
+
border: none;
|
|
140
|
+
background: transparent;
|
|
141
|
+
color: inherit;
|
|
142
|
+
font-family: inherit;
|
|
143
|
+
font-size: inherit;
|
|
144
|
+
padding: 5px 0;
|
|
145
|
+
outline: none;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.combobox-input:disabled {
|
|
149
|
+
cursor: not-allowed;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.actions-area {
|
|
153
|
+
display: inline-flex;
|
|
154
|
+
align-items: center;
|
|
155
|
+
gap: 6px;
|
|
156
|
+
margin-left: auto;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.btn-clear {
|
|
160
|
+
display: inline-flex;
|
|
161
|
+
align-items: center;
|
|
162
|
+
justify-content: center;
|
|
163
|
+
width: 24px;
|
|
164
|
+
height: 24px;
|
|
165
|
+
min-width: 24px;
|
|
166
|
+
min-height: 24px;
|
|
167
|
+
border: none;
|
|
168
|
+
background: transparent;
|
|
169
|
+
color: var(--el-text-muted, #64748b);
|
|
170
|
+
cursor: pointer;
|
|
171
|
+
border-radius: 50%;
|
|
172
|
+
font-size: 14px;
|
|
173
|
+
padding: 0;
|
|
174
|
+
transition: background-color 0.15s ease, color 0.15s ease;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.btn-clear:hover {
|
|
178
|
+
background-color: var(--el-surface-muted, #e2e8f0);
|
|
179
|
+
color: var(--el-text-primary, #0f172a);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.dropdown-listbox {
|
|
183
|
+
position: absolute;
|
|
184
|
+
top: calc(100% + 4px);
|
|
185
|
+
left: 0;
|
|
186
|
+
right: 0;
|
|
187
|
+
background-color: var(--el-surface-card, #ffffff);
|
|
188
|
+
border: 1px solid var(--el-surface-border, #e2e8f0);
|
|
189
|
+
border-radius: var(--el-radius-md, 8px);
|
|
190
|
+
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.05);
|
|
191
|
+
max-height: 240px;
|
|
192
|
+
overflow-y: auto;
|
|
193
|
+
z-index: 1000;
|
|
194
|
+
list-style: none;
|
|
195
|
+
padding: 4px;
|
|
196
|
+
margin: 0;
|
|
197
|
+
box-sizing: border-box;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
.option-item {
|
|
201
|
+
display: flex;
|
|
202
|
+
align-items: center;
|
|
203
|
+
justify-content: space-between;
|
|
204
|
+
padding: 8px 12px;
|
|
205
|
+
min-height: 36px;
|
|
206
|
+
border-radius: var(--el-radius-sm, 6px);
|
|
207
|
+
cursor: pointer;
|
|
208
|
+
font-size: 0.875rem;
|
|
209
|
+
color: var(--el-text-primary, #0f172a);
|
|
210
|
+
transition: background-color 0.1s ease;
|
|
211
|
+
box-sizing: border-box;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.option-item:hover,
|
|
215
|
+
.option-item.is-active {
|
|
216
|
+
background-color: var(--el-surface-muted, #f1f5f9);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
.option-item.is-selected {
|
|
220
|
+
background-color: rgba(37, 99, 235, 0.08);
|
|
221
|
+
color: var(--el-color-primary, #2563eb);
|
|
222
|
+
font-weight: 600;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.check-icon {
|
|
226
|
+
font-weight: bold;
|
|
227
|
+
color: var(--el-color-primary, #2563eb);
|
|
228
|
+
margin-right: 6px;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
.option-meta {
|
|
232
|
+
font-size: 0.75rem;
|
|
233
|
+
color: var(--el-text-muted, #64748b);
|
|
234
|
+
margin-left: 8px;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
.empty-message,
|
|
238
|
+
.loading-message {
|
|
239
|
+
padding: 12px 16px;
|
|
240
|
+
font-size: 0.875rem;
|
|
241
|
+
color: var(--el-text-muted, #64748b);
|
|
242
|
+
text-align: center;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.sr-only {
|
|
246
|
+
position: absolute;
|
|
247
|
+
width: 1px;
|
|
248
|
+
height: 1px;
|
|
249
|
+
padding: 0;
|
|
250
|
+
margin: -1px;
|
|
251
|
+
overflow: hidden;
|
|
252
|
+
clip: rect(0, 0, 0, 0);
|
|
253
|
+
white-space: nowrap;
|
|
254
|
+
border: 0;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
@media (pointer: coarse) {
|
|
258
|
+
input {
|
|
259
|
+
font-size: max(16px, 1em);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
`;
|
|
263
|
+
|
|
264
|
+
connected() {
|
|
265
|
+
this.required.value = this.hasAttribute('required');
|
|
266
|
+
this.disabled.value = this.hasAttribute('disabled');
|
|
267
|
+
this.multiple.value = this.hasAttribute('multiple');
|
|
268
|
+
|
|
269
|
+
this.#syncFormValue();
|
|
270
|
+
this.validate();
|
|
271
|
+
|
|
272
|
+
// Form value synchronization
|
|
273
|
+
effect(() => {
|
|
274
|
+
this.#syncFormValue();
|
|
275
|
+
this.validate();
|
|
276
|
+
}, { signal: this.signal });
|
|
277
|
+
|
|
278
|
+
// Sync input text with display label in single mode
|
|
279
|
+
effect(() => {
|
|
280
|
+
if (!this.multiple.value) {
|
|
281
|
+
const input = this.root?.querySelector?.('.combobox-input');
|
|
282
|
+
if (input && document.activeElement !== input) {
|
|
283
|
+
input.value = this.displayLabel.value;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}, { signal: this.signal });
|
|
287
|
+
|
|
288
|
+
// Light dismiss on outside clicks
|
|
289
|
+
const onDocPointerDown = (e) => {
|
|
290
|
+
if (!this.isOpen.value) return;
|
|
291
|
+
const path = e.composedPath ? e.composedPath() : [];
|
|
292
|
+
if (!path.includes(this)) {
|
|
293
|
+
this.close();
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
document.addEventListener('pointerdown', onDocPointerDown, { signal: this.signal });
|
|
298
|
+
|
|
299
|
+
if (this.hasAttribute('options')) {
|
|
300
|
+
this.#applyOptions(this.getAttribute('options'));
|
|
301
|
+
} else if (this.hasAttribute('data')) {
|
|
302
|
+
this.#applyOptions(this.getAttribute('data'));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
get options() {
|
|
307
|
+
return this.data.value;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
set options(val) {
|
|
311
|
+
this.#applyOptions(val);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
#applyOptions(val) {
|
|
315
|
+
if (Array.isArray(val)) {
|
|
316
|
+
this.data.value = val;
|
|
317
|
+
} else if (typeof val === 'string') {
|
|
318
|
+
try {
|
|
319
|
+
this.data.value = JSON.parse(val);
|
|
320
|
+
} catch {
|
|
321
|
+
this.data.value = [];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
attributeChanged(name, oldValue, newValue) {
|
|
327
|
+
if (name === 'required') {
|
|
328
|
+
this.required.value = newValue !== null && newValue !== 'false';
|
|
329
|
+
this.validate();
|
|
330
|
+
} else if (name === 'disabled') {
|
|
331
|
+
this.disabled.value = newValue !== null && newValue !== 'false';
|
|
332
|
+
} else if (name === 'multiple') {
|
|
333
|
+
this.multiple.value = newValue !== null && newValue !== 'false';
|
|
334
|
+
this.#syncFormValue();
|
|
335
|
+
this.validate();
|
|
336
|
+
} else if (name === 'value') {
|
|
337
|
+
this.value.value = newValue;
|
|
338
|
+
this.#syncFormValue();
|
|
339
|
+
this.validate();
|
|
340
|
+
} else if (name === 'options' || name === 'data') {
|
|
341
|
+
this.#applyOptions(newValue);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
#syncFormValue() {
|
|
346
|
+
if (this.multiple.value) {
|
|
347
|
+
const vals = this.selectedItems.value.map(it => this.getItemValue(it));
|
|
348
|
+
this.setFormValue(vals.join(','));
|
|
349
|
+
} else {
|
|
350
|
+
const val = this.value.value;
|
|
351
|
+
this.setFormValue(val ? String(val) : '');
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
validate() {
|
|
356
|
+
const isRequired = Boolean(this.required.value);
|
|
357
|
+
const inputEl = this.root?.querySelector?.('.combobox-input');
|
|
358
|
+
let hasValue = false;
|
|
359
|
+
|
|
360
|
+
if (this.multiple.value) {
|
|
361
|
+
hasValue = this.selectedItems.value.length > 0;
|
|
362
|
+
} else {
|
|
363
|
+
hasValue = String(this.value.value || '').trim().length > 0;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (isRequired && !hasValue) {
|
|
367
|
+
this.setValidity(
|
|
368
|
+
{ valueMissing: true },
|
|
369
|
+
'Please select an option from the list.',
|
|
370
|
+
inputEl
|
|
371
|
+
);
|
|
372
|
+
this.setAttribute('aria-invalid', 'true');
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
this.setValidity({});
|
|
377
|
+
this.removeAttribute('aria-invalid');
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
getItemLabel(item) {
|
|
382
|
+
if (item === null || item === undefined) return '';
|
|
383
|
+
if (typeof item === 'string' || typeof item === 'number') return String(item);
|
|
384
|
+
const key = this.labelKey.value || 'label';
|
|
385
|
+
return item[key] !== undefined ? String(item[key]) : (item.label || item.name || item.title || JSON.stringify(item));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
getItemValue(item) {
|
|
389
|
+
if (item === null || item === undefined) return '';
|
|
390
|
+
if (typeof item === 'string' || typeof item === 'number') return item;
|
|
391
|
+
const key = this.valueKey.value || 'value';
|
|
392
|
+
return item[key] !== undefined ? item[key] : (item.value || item.id || this.getItemLabel(item));
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
getItemMeta(item) {
|
|
396
|
+
if (!item || typeof item !== 'object') return '';
|
|
397
|
+
return item.meta || item.email || item.code || item.subtitle || '';
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
isItemSelected(item) {
|
|
401
|
+
const val = this.getItemValue(item);
|
|
402
|
+
if (this.multiple.value) {
|
|
403
|
+
return this.selectedItems.value.some(it => this.getItemValue(it) === val);
|
|
404
|
+
}
|
|
405
|
+
return this.value.value === val;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
setData(items) {
|
|
409
|
+
this.data.value = Array.isArray(items) ? items : [];
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async search(query) {
|
|
413
|
+
const q = String(query || '').trim();
|
|
414
|
+
const min = Number(this.minChars.value) || 1;
|
|
415
|
+
|
|
416
|
+
if (q.length < min) {
|
|
417
|
+
this.suggestions.value = [];
|
|
418
|
+
this.isOpen.value = false;
|
|
419
|
+
this.activeIndex.value = -1;
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const matchesQuery = (item) => {
|
|
424
|
+
const qLower = q.toLowerCase();
|
|
425
|
+
const label = this.getItemLabel(item).toLowerCase();
|
|
426
|
+
const meta = this.getItemMeta(item).toLowerCase();
|
|
427
|
+
const val = String(this.getItemValue(item)).toLowerCase();
|
|
428
|
+
return label.includes(qLower) || meta.includes(qLower) || val.includes(qLower);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const endpoint = this.url.value;
|
|
432
|
+
|
|
433
|
+
if (endpoint) {
|
|
434
|
+
this.isLoading.value = true;
|
|
435
|
+
this.isOpen.value = true;
|
|
436
|
+
this.activeIndex.value = -1;
|
|
437
|
+
|
|
438
|
+
if (this.#debounceTimer) clearTimeout(this.#debounceTimer);
|
|
439
|
+
if (this.#abortController) this.#abortController.abort();
|
|
440
|
+
|
|
441
|
+
const delay = Number(this.debounce.value) || 0;
|
|
442
|
+
|
|
443
|
+
this.#debounceTimer = setTimeout(async () => {
|
|
444
|
+
this.#abortController = new AbortController();
|
|
445
|
+
try {
|
|
446
|
+
let reqUrl = endpoint;
|
|
447
|
+
if (reqUrl.includes('{query}')) {
|
|
448
|
+
reqUrl = reqUrl.replace('{query}', encodeURIComponent(q));
|
|
449
|
+
} else {
|
|
450
|
+
const sep = reqUrl.includes('?') ? '&' : '?';
|
|
451
|
+
reqUrl = `${reqUrl}${sep}${this.queryParam.value || 'q'}=${encodeURIComponent(q)}`;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const res = await fetch(reqUrl, { credentials: 'omit', signal: this.#abortController.signal });
|
|
455
|
+
if (!res.ok) throw new Error(`HTTP error ${res.status}`);
|
|
456
|
+
const raw = await res.json();
|
|
457
|
+
const items = Array.isArray(raw) ? raw : (raw.results || raw.data || raw.items || []);
|
|
458
|
+
|
|
459
|
+
const filtered = items.filter(matchesQuery);
|
|
460
|
+
this.suggestions.value = filtered.length > 0 ? filtered : items;
|
|
461
|
+
this.isOpen.value = true;
|
|
462
|
+
} catch (err) {
|
|
463
|
+
if (err.name !== 'AbortError') {
|
|
464
|
+
const local = this.data.value.filter(matchesQuery);
|
|
465
|
+
this.suggestions.value = local;
|
|
466
|
+
}
|
|
467
|
+
} finally {
|
|
468
|
+
this.isLoading.value = false;
|
|
469
|
+
}
|
|
470
|
+
}, delay);
|
|
471
|
+
} else {
|
|
472
|
+
const local = this.data.value.filter(matchesQuery);
|
|
473
|
+
this.suggestions.value = local;
|
|
474
|
+
this.isOpen.value = true;
|
|
475
|
+
this.activeIndex.value = -1;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
select(item) {
|
|
480
|
+
if (!item) return;
|
|
481
|
+
const label = this.getItemLabel(item);
|
|
482
|
+
const val = this.getItemValue(item);
|
|
483
|
+
|
|
484
|
+
if (this.multiple.value) {
|
|
485
|
+
// Toggle or append item in multi-mode
|
|
486
|
+
const existsIndex = this.selectedItems.value.findIndex(it => this.getItemValue(it) === val);
|
|
487
|
+
if (existsIndex >= 0) {
|
|
488
|
+
this.removeItem(existsIndex);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
this.selectedItems.value = [...this.selectedItems.value, item];
|
|
493
|
+
const newVals = this.selectedItems.value.map(it => this.getItemValue(it));
|
|
494
|
+
this.value.value = newVals;
|
|
495
|
+
this.displayLabel.value = '';
|
|
496
|
+
|
|
497
|
+
const input = this.root?.querySelector?.('.combobox-input');
|
|
498
|
+
if (input) input.value = '';
|
|
499
|
+
|
|
500
|
+
this.announcement.value = `Selected ${label}. Total selected: ${this.selectedItems.value.length}.`;
|
|
501
|
+
this.suggestions.value = [];
|
|
502
|
+
this.isOpen.value = false;
|
|
503
|
+
this.activeIndex.value = -1;
|
|
504
|
+
this.validate();
|
|
505
|
+
|
|
506
|
+
this.emit('select', { item, items: this.selectedItems.value, value: newVals, label });
|
|
507
|
+
this.emit('change', { value: newVals, items: this.selectedItems.value });
|
|
508
|
+
} else {
|
|
509
|
+
// Single selection mode
|
|
510
|
+
this.selectedItem.value = item;
|
|
511
|
+
this.value.value = val;
|
|
512
|
+
this.displayLabel.value = label;
|
|
513
|
+
|
|
514
|
+
const input = this.root?.querySelector?.('.combobox-input');
|
|
515
|
+
if (input) input.value = label;
|
|
516
|
+
|
|
517
|
+
this.announcement.value = `Selected ${label}.`;
|
|
518
|
+
this.isOpen.value = false;
|
|
519
|
+
this.activeIndex.value = -1;
|
|
520
|
+
this.validate();
|
|
521
|
+
|
|
522
|
+
this.emit('select', { item, value: val, label });
|
|
523
|
+
this.emit('change', { value: val, label });
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
removeItem(index) {
|
|
528
|
+
if (!this.multiple.value) return;
|
|
529
|
+
const list = [...this.selectedItems.value];
|
|
530
|
+
if (index < 0 || index >= list.length) return;
|
|
531
|
+
|
|
532
|
+
const [removed] = list.splice(index, 1);
|
|
533
|
+
this.selectedItems.value = list;
|
|
534
|
+
const newVals = list.map(it => this.getItemValue(it));
|
|
535
|
+
this.value.value = newVals;
|
|
536
|
+
this.announcement.value = `Removed ${this.getItemLabel(removed)}. Total selected: ${list.length}.`;
|
|
537
|
+
this.validate();
|
|
538
|
+
|
|
539
|
+
this.emit('remove', { item: removed, items: list, value: newVals });
|
|
540
|
+
this.emit('change', { value: newVals, items: list });
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
clear() {
|
|
544
|
+
this.selectedItem.value = null;
|
|
545
|
+
this.selectedItems.value = [];
|
|
546
|
+
this.value.value = this.multiple.value ? [] : '';
|
|
547
|
+
this.displayLabel.value = '';
|
|
548
|
+
|
|
549
|
+
const input = this.root?.querySelector?.('.combobox-input');
|
|
550
|
+
if (input) input.value = '';
|
|
551
|
+
|
|
552
|
+
this.suggestions.value = [];
|
|
553
|
+
this.isOpen.value = false;
|
|
554
|
+
this.activeIndex.value = -1;
|
|
555
|
+
this.announcement.value = 'Cleared selection.';
|
|
556
|
+
this.validate();
|
|
557
|
+
|
|
558
|
+
this.emit('clear');
|
|
559
|
+
this.emit('change', { value: this.multiple.value ? [] : '', items: [] });
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
open() {
|
|
563
|
+
if (this.disabled.value) return;
|
|
564
|
+
if (this.suggestions.value.length > 0) {
|
|
565
|
+
this.isOpen.value = true;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
close() {
|
|
570
|
+
this.isOpen.value = false;
|
|
571
|
+
this.activeIndex.value = -1;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
#onInput = (e) => {
|
|
575
|
+
const query = e.target.value;
|
|
576
|
+
this.displayLabel.value = query;
|
|
577
|
+
|
|
578
|
+
if (!this.multiple.value && this.selectedItem.value && this.getItemLabel(this.selectedItem.value) !== query) {
|
|
579
|
+
this.selectedItem.value = null;
|
|
580
|
+
this.value.value = '';
|
|
581
|
+
this.validate();
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
this.search(query);
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
#onKeyDown = (e) => {
|
|
588
|
+
if (this.disabled.value) return;
|
|
589
|
+
const items = this.suggestions.value;
|
|
590
|
+
const input = e.target;
|
|
591
|
+
|
|
592
|
+
switch (e.key) {
|
|
593
|
+
case 'Backspace':
|
|
594
|
+
if (this.multiple.value && input.value === '' && this.selectedItems.value.length > 0) {
|
|
595
|
+
e.preventDefault();
|
|
596
|
+
this.removeItem(this.selectedItems.value.length - 1);
|
|
597
|
+
}
|
|
598
|
+
break;
|
|
599
|
+
|
|
600
|
+
case 'ArrowDown':
|
|
601
|
+
e.preventDefault();
|
|
602
|
+
if (!this.isOpen.value) {
|
|
603
|
+
this.isOpen.value = true;
|
|
604
|
+
} else if (items.length > 0) {
|
|
605
|
+
const next = this.activeIndex.value + 1;
|
|
606
|
+
this.activeIndex.value = next >= items.length ? 0 : next;
|
|
607
|
+
this.#scrollActiveIntoView();
|
|
608
|
+
}
|
|
609
|
+
break;
|
|
610
|
+
|
|
611
|
+
case 'ArrowUp':
|
|
612
|
+
e.preventDefault();
|
|
613
|
+
if (this.isOpen.value && items.length > 0) {
|
|
614
|
+
const prev = this.activeIndex.value - 1;
|
|
615
|
+
this.activeIndex.value = prev < 0 ? items.length - 1 : prev;
|
|
616
|
+
this.#scrollActiveIntoView();
|
|
617
|
+
}
|
|
618
|
+
break;
|
|
619
|
+
|
|
620
|
+
case 'Enter':
|
|
621
|
+
if (this.isOpen.value && this.activeIndex.value >= 0 && items[this.activeIndex.value]) {
|
|
622
|
+
e.preventDefault();
|
|
623
|
+
this.select(items[this.activeIndex.value]);
|
|
624
|
+
}
|
|
625
|
+
break;
|
|
626
|
+
|
|
627
|
+
case 'Escape':
|
|
628
|
+
e.preventDefault();
|
|
629
|
+
this.close();
|
|
630
|
+
break;
|
|
631
|
+
|
|
632
|
+
case 'Tab':
|
|
633
|
+
if (this.isOpen.value && this.activeIndex.value >= 0 && items[this.activeIndex.value]) {
|
|
634
|
+
this.select(items[this.activeIndex.value]);
|
|
635
|
+
} else {
|
|
636
|
+
this.close();
|
|
637
|
+
}
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
#scrollActiveIntoView() {
|
|
643
|
+
const listbox = this.root?.querySelector?.('.dropdown-listbox');
|
|
644
|
+
const active = listbox?.querySelector?.('.is-active');
|
|
645
|
+
if (active && typeof active.scrollIntoView === 'function') {
|
|
646
|
+
active.scrollIntoView({ block: 'nearest' });
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
formReset() {
|
|
651
|
+
this.clear();
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
formResetCallback() {
|
|
655
|
+
this.clear();
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
render() {
|
|
659
|
+
const listId = this.#listboxId;
|
|
660
|
+
|
|
661
|
+
return html`
|
|
662
|
+
<div
|
|
663
|
+
class=${() => `combobox-wrapper size-${this.size.value}${this.disabled.value ? ' is-disabled' : ''}`}
|
|
664
|
+
part="wrapper"
|
|
665
|
+
>
|
|
666
|
+
<span class="prefix-area" part="prefix">
|
|
667
|
+
<slot name="prefix"></slot>
|
|
668
|
+
</span>
|
|
669
|
+
|
|
670
|
+
${() => this.multiple.value ? html`
|
|
671
|
+
<div class="chips-group" part="chips" role="list" aria-label="Selected items">
|
|
672
|
+
${() => this.selectedItems.value.map((item, idx) => {
|
|
673
|
+
const label = this.getItemLabel(item);
|
|
674
|
+
return html`
|
|
675
|
+
<el-chip
|
|
676
|
+
role="listitem"
|
|
677
|
+
size="sm"
|
|
678
|
+
variant="primary"
|
|
679
|
+
removable
|
|
680
|
+
part="chip"
|
|
681
|
+
aria-label=${`Selected: ${label}`}
|
|
682
|
+
@remove=${() => this.removeItem(idx)}
|
|
683
|
+
>${label}</el-chip>
|
|
684
|
+
`;
|
|
685
|
+
})}
|
|
686
|
+
</div>
|
|
687
|
+
` : null}
|
|
688
|
+
|
|
689
|
+
<input
|
|
690
|
+
type="text"
|
|
691
|
+
class="combobox-input"
|
|
692
|
+
part="input"
|
|
693
|
+
placeholder=${() => (this.multiple.value && this.selectedItems.value.length > 0) ? '' : this.placeholder.value}
|
|
694
|
+
?disabled=${this.disabled}
|
|
695
|
+
?required=${() => (this.multiple.value && this.selectedItems.value.length > 0) ? null : this.required.value}
|
|
696
|
+
role="combobox"
|
|
697
|
+
aria-autocomplete="list"
|
|
698
|
+
aria-expanded=${() => this.isOpen.value ? 'true' : 'false'}
|
|
699
|
+
aria-controls=${listId}
|
|
700
|
+
aria-haspopup="listbox"
|
|
701
|
+
aria-activedescendant=${() => this.activeIndex.value >= 0 ? `${listId}-opt-${this.activeIndex.value}` : ''}
|
|
702
|
+
@input=${this.#onInput}
|
|
703
|
+
@keydown=${this.#onKeyDown}
|
|
704
|
+
@focus=${() => { if (this.suggestions.value.length > 0) this.open(); }}
|
|
705
|
+
/>
|
|
706
|
+
|
|
707
|
+
<div class="actions-area" part="actions">
|
|
708
|
+
${() => this.isLoading.value ? html`<el-spinner size="sm" part="spinner"></el-spinner>` : null}
|
|
709
|
+
${() => ((this.displayLabel.value || this.selectedItems.value.length > 0) && !this.disabled.value) ? html`
|
|
710
|
+
<button
|
|
711
|
+
type="button"
|
|
712
|
+
class="btn-clear"
|
|
713
|
+
part="clear-button"
|
|
714
|
+
aria-label="Clear all selections"
|
|
715
|
+
@click=${() => this.clear()}
|
|
716
|
+
>✕</button>
|
|
717
|
+
` : null}
|
|
718
|
+
<slot name="suffix"></slot>
|
|
719
|
+
</div>
|
|
720
|
+
</div>
|
|
721
|
+
|
|
722
|
+
<!-- Accessible Live Region for Screen Readers -->
|
|
723
|
+
<span class="sr-only" aria-live="polite" part="announcer">
|
|
724
|
+
${() => this.announcement.value}
|
|
725
|
+
</span>
|
|
726
|
+
|
|
727
|
+
${() => this.isOpen.value ? html`
|
|
728
|
+
<ul
|
|
729
|
+
id=${listId}
|
|
730
|
+
class="dropdown-listbox"
|
|
731
|
+
part="listbox"
|
|
732
|
+
role="listbox"
|
|
733
|
+
tabindex="-1"
|
|
734
|
+
>
|
|
735
|
+
${() => {
|
|
736
|
+
const list = this.suggestions.value;
|
|
737
|
+
if (this.isLoading.value && list.length === 0) {
|
|
738
|
+
return html`<li class="loading-message">Searching suggestions...</li>`;
|
|
739
|
+
}
|
|
740
|
+
if (list.length === 0) {
|
|
741
|
+
return html`<li class="empty-message">No matching results found</li>`;
|
|
742
|
+
}
|
|
743
|
+
return list.map((item, idx) => {
|
|
744
|
+
const label = this.getItemLabel(item);
|
|
745
|
+
const meta = this.getItemMeta(item);
|
|
746
|
+
const isSelected = this.isItemSelected(item);
|
|
747
|
+
const isActive = this.activeIndex.value === idx;
|
|
748
|
+
|
|
749
|
+
return html`
|
|
750
|
+
<li
|
|
751
|
+
id=${`${listId}-opt-${idx}`}
|
|
752
|
+
class=${`option-item${isSelected ? ' is-selected' : ''}${isActive ? ' is-active' : ''}`}
|
|
753
|
+
role="option"
|
|
754
|
+
aria-selected=${isSelected ? 'true' : 'false'}
|
|
755
|
+
part="option"
|
|
756
|
+
@click=${() => this.select(item)}
|
|
757
|
+
>
|
|
758
|
+
<span class="option-label">
|
|
759
|
+
${isSelected ? html`<span class="check-icon" aria-hidden="true">✓ </span>` : null}
|
|
760
|
+
${label}
|
|
761
|
+
</span>
|
|
762
|
+
${meta ? html`<span class="option-meta">${meta}</span>` : null}
|
|
763
|
+
</li>
|
|
764
|
+
`;
|
|
765
|
+
});
|
|
766
|
+
}}
|
|
767
|
+
</ul>
|
|
768
|
+
` : null}
|
|
769
|
+
`;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-autocomplete')) {
|
|
774
|
+
customElements.define('el-autocomplete', ElAutocomplete);
|
|
775
|
+
}
|