@syware/jlib 0.17.0 → 0.18.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.
Files changed (39) hide show
  1. package/dist/all.js +5 -3
  2. package/dist/base/jl-combobox-base.css.js +3 -0
  3. package/dist/base/jl-combobox-base.d.ts +202 -0
  4. package/dist/base/jl-combobox-base.js +632 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +4 -2
  7. package/dist/lib/combobox.d.ts +12 -0
  8. package/dist/lib/combobox.js +22 -0
  9. package/dist/lib/floating.d.ts +5 -1
  10. package/dist/lib/floating.js +9 -1
  11. package/dist/lib/input-list.d.ts +2 -0
  12. package/dist/lib/input-list.js +3 -3
  13. package/dist/lib/internals.js +1 -0
  14. package/dist/lib/table.d.ts +24 -3
  15. package/dist/lib/table.js +58 -2
  16. package/dist/lib/texts-de.d.ts +3 -0
  17. package/dist/lib/texts-de.js +3 -0
  18. package/dist/lib/texts-fr.d.ts +3 -0
  19. package/dist/lib/texts-fr.js +3 -0
  20. package/dist/lib/texts.d.ts +3 -0
  21. package/dist/lib/texts.js +3 -0
  22. package/dist/molecules/jl-combobox.d.ts +15 -123
  23. package/dist/molecules/jl-combobox.js +24 -461
  24. package/dist/molecules/jl-input-list.d.ts +8 -1
  25. package/dist/molecules/jl-input-list.js +16 -5
  26. package/dist/molecules/jl-multi-combobox.css.js +3 -0
  27. package/dist/molecules/jl-multi-combobox.d.ts +50 -0
  28. package/dist/molecules/jl-multi-combobox.js +176 -0
  29. package/dist/molecules/jl-popover.d.ts +2 -0
  30. package/dist/molecules/jl-popover.js +4 -0
  31. package/dist/organisms/jl-side-nav.js +1 -1
  32. package/dist/organisms/jl-table.css.js +1 -1
  33. package/dist/organisms/jl-table.d.ts +77 -5
  34. package/dist/organisms/jl-table.js +223 -23
  35. package/dist/templates/jl-app-shell.js +1 -1
  36. package/dist/types/jl-combobox.types.d.ts +5 -0
  37. package/dist/types/jl-table.types.d.ts +22 -0
  38. package/package.json +2 -1
  39. package/dist/molecules/jl-combobox.css.js +0 -3
@@ -0,0 +1,632 @@
1
+ import { jlText } from "../lib/texts.js";
2
+ import { BASE_STYLES, CONTROL_STYLES, FLOATING_STYLES } from "../styles/shared.js";
3
+ import { JlFormControl } from "./jl-form-control.js";
4
+ import { jsonOf, oneOf } from "../lib/attributes.js";
5
+ import { followAnchor } from "../lib/floating.js";
6
+ import { registerIcons } from "../lib/icons.js";
7
+ import { CONTROL_SIZES } from "../lib/options.js";
8
+ import { JlIcon } from "../atoms/jl-icon.js";
9
+ import { setAriaFlag, setOrRemove, uniqueId } from "../lib/dom.js";
10
+ import { JlSpinner } from "../atoms/jl-spinner.js";
11
+ import { heldValues, keptLabels, sameValues } from "../lib/combobox.js";
12
+ import { keepInView } from "../lib/keep-in-view.js";
13
+ import { edgeEnabledIndex, filterOptions, foldText, matchParts, nextEnabledIndex } from "../lib/option-filter.js";
14
+ import jl_combobox_base_default from "./jl-combobox-base.css.js";
15
+ import { css } from "@jadis/core";
16
+ import { Check, ChevronDown, X } from "lucide";
17
+ import { Fragment, jsx, jsxs } from "@jadis/core/jsx-runtime";
18
+ // src/base/jl-combobox-base.tsx
19
+ registerIcons({
20
+ check: Check,
21
+ "chevron-down": ChevronDown,
22
+ x: X
23
+ });
24
+ var COMBOBOX_FILTERS = ["local", "none"];
25
+ var ICON_SIZE = 16;
26
+ var ACTION_ICON_SIZE = 14;
27
+ var CHECK_SIZE = 14;
28
+ var SPINNER_SIZE = 14;
29
+ var NONE = -1;
30
+ var PLACEMENT = "bottom-start";
31
+ var LOCAL_FILTER = "local";
32
+ var NO_DEBOUNCE = 0;
33
+ /** What the list shows of the options for a typed text: those that match it, or all of them as the page gave them. */
34
+ var FILTERS = {
35
+ local: filterOptions,
36
+ none: (options) => [...options]
37
+ };
38
+ /** A JSON attribute that should hold a list of options; anything else gives none. */
39
+ var parseOptions = (value) => {
40
+ const parsed = jsonOf([])(value);
41
+ return Array.isArray(parsed) ? parsed : [];
42
+ };
43
+ /** A label with the searched text in a `<mark>`. */
44
+ var highlighted = (label, query) => matchParts(label, query).map((part) => part.matched ? /* @__PURE__ */ jsx("mark", { children: part.text }) : part.text);
45
+ /**
46
+ * What `jl-combobox` (one value, a `string`) and `jl-multi-combobox` (several, a `string[]`) share: the text field,
47
+ * the list that narrows as people type or that the page fills (`filter="none"`, `loading`, `debounce`), the keys,
48
+ * and the labels kept with the values. Each says what its value is, what a choice and typed text do to it, and what
49
+ * the field shows.
50
+ */
51
+ var JlComboboxBase = class extends JlFormControl {
52
+ events = this.useEvents();
53
+ options = this.useChange([], () => this.renderOptionsChange(), {
54
+ attribute: "options",
55
+ parse: parseOptions
56
+ });
57
+ value = this.useChange(this.shaped([]), () => this.renderValue(), {
58
+ attribute: "value",
59
+ parse: (text) => this.parseValue(text)
60
+ });
61
+ placeholder = this.useChange("", () => this.renderPlaceholder(), { attribute: "placeholder" });
62
+ /** The field's name for screen readers, when no visible label names it. */
63
+ label = this.useChange("", (value) => {
64
+ setOrRemove(this._refs.native, "aria-label", value);
65
+ setOrRemove(this._refs.listbox, "aria-label", value);
66
+ }, { attribute: "label" });
67
+ /**
68
+ * Who narrows the options to what was typed: the combobox (`local`), or the page (`none`), which hears `input`
69
+ * and gives the options it found, shown as they are.
70
+ */
71
+ filter = this.useChange(LOCAL_FILTER, () => this.renderOptionsChange(), {
72
+ attribute: "filter",
73
+ parse: oneOf(COMBOBOX_FILTERS, LOCAL_FILTER)
74
+ });
75
+ /** The page is finding options: the open list says so, above the options it holds, until the page sets new ones. */
76
+ loading = this.useChange(false, (value) => this.renderLoading(value), { attribute: "loading" });
77
+ /** What the list says while loading; jlib's own when empty. */
78
+ loadingText = this.useChange("", () => this.renderLoadingText(), { attribute: "loading-text" });
79
+ /** How long people stop typing, in milliseconds, before `input` fires; 0 fires it at every keystroke. */
80
+ debounce = this.useChange(NO_DEBOUNCE, () => void 0, { attribute: "debounce" });
81
+ /** Takes the typed text as a value when no option has it as its label. */
82
+ allowCustom = this.useChange(false, () => this.renderValue(), { attribute: "allow-custom" });
83
+ /** Shows a button that empties the field while it holds something. */
84
+ clearable = this.useChange(false, () => this.renderClear(), { attribute: "clearable" });
85
+ /** What the list says when no option matches; jlib's own when empty. */
86
+ emptyText = this.useChange("", () => this.renderEmptyText(), { attribute: "empty-text" });
87
+ required = this.useChange(false, (value) => {
88
+ setAriaFlag(this._refs.native, "aria-required", value);
89
+ this.reflect("required", value);
90
+ this.renderValidity();
91
+ }, { attribute: "required" });
92
+ disabled = this.useChange(false, (value) => this.renderDisabled(value), { attribute: "disabled" });
93
+ /** Draws the field in the danger colour, for an error the page found itself. */
94
+ invalid = this.useChange(false, (value) => {
95
+ this.reflect("invalid", value);
96
+ setAriaFlag(this._refs.native, "aria-invalid", value);
97
+ }, { attribute: "invalid" });
98
+ size = this.useChange("md", (value) => this.reflect("size", value), {
99
+ attribute: "size",
100
+ parse: oneOf(CONTROL_SIZES, "md")
101
+ });
102
+ _listId = uniqueId("jl-combobox-list");
103
+ _state = {
104
+ active: NONE,
105
+ /** The option element the arrows reached: it alone is marked, wherever the filter put it. */
106
+ activeItem: null,
107
+ disabledByForm: false,
108
+ /** The label of each value held, kept when the page gives other options. */
109
+ labels: /* @__PURE__ */ new Map(),
110
+ matches: [],
111
+ opening: new AbortController(),
112
+ /** What people typed since the last choice; empty shows every option. */
113
+ query: "",
114
+ /** The `input` waiting for the typing to pause. */
115
+ typing: 0
116
+ };
117
+ _items = /* @__PURE__ */ new WeakMap();
118
+ _refs = this.useRefs((ref) => ({
119
+ clear: ref(".clear"),
120
+ control: ref(".control"),
121
+ empty: ref(".empty"),
122
+ entry: ref(".entry"),
123
+ listbox: ref(".listbox"),
124
+ loading: ref(".loading"),
125
+ loadingText: ref(".loading-text"),
126
+ native: ref("input"),
127
+ popup: ref(".popup"),
128
+ status: ref(".status"),
129
+ toggle: ref(".toggle")
130
+ }));
131
+ focus(options) {
132
+ this._refs.native.focus(options);
133
+ }
134
+ templateHtml() {
135
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
136
+ /* @__PURE__ */ jsxs("div", {
137
+ class: "control",
138
+ attrs: { part: "base" },
139
+ children: [
140
+ /* @__PURE__ */ jsxs("div", {
141
+ class: "entry",
142
+ children: [this.entryStart(), /* @__PURE__ */ jsx("input", {
143
+ class: "native",
144
+ id: "field",
145
+ role: "combobox",
146
+ "aria-autocomplete": "list",
147
+ "aria-expanded": "false",
148
+ "aria-controls": this._listId,
149
+ autocomplete: "off",
150
+ spellcheck: false
151
+ })]
152
+ }),
153
+ /* @__PURE__ */ jsx("button", {
154
+ class: "action clear",
155
+ type: "button",
156
+ tabIndex: -1,
157
+ hidden: true,
158
+ children: /* @__PURE__ */ jsx(JlIcon, {
159
+ name: "x",
160
+ size: ACTION_ICON_SIZE
161
+ })
162
+ }),
163
+ /* @__PURE__ */ jsx("button", {
164
+ class: "action toggle",
165
+ type: "button",
166
+ tabIndex: -1,
167
+ children: /* @__PURE__ */ jsx(JlIcon, {
168
+ name: "chevron-down",
169
+ size: ICON_SIZE
170
+ })
171
+ })
172
+ ]
173
+ }),
174
+ /* @__PURE__ */ jsxs("div", {
175
+ class: "floating panel popup",
176
+ popover: "manual",
177
+ attrs: { part: "list" },
178
+ children: [
179
+ /* @__PURE__ */ jsxs("p", {
180
+ class: "loading",
181
+ hidden: true,
182
+ children: [/* @__PURE__ */ jsx("span", {
183
+ class: "spinner",
184
+ "aria-hidden": "true",
185
+ children: /* @__PURE__ */ jsx(JlSpinner, { size: SPINNER_SIZE })
186
+ }), /* @__PURE__ */ jsx("span", { class: "loading-text" })]
187
+ }),
188
+ /* @__PURE__ */ jsx("div", {
189
+ class: "listbox",
190
+ role: "listbox",
191
+ id: this._listId
192
+ }),
193
+ /* @__PURE__ */ jsx("p", {
194
+ class: "empty",
195
+ hidden: true
196
+ })
197
+ ]
198
+ }),
199
+ /* @__PURE__ */ jsx("span", {
200
+ class: "status sr-only",
201
+ role: "status"
202
+ })
203
+ ] });
204
+ }
205
+ templateCss() {
206
+ return css`${BASE_STYLES}${CONTROL_STYLES}${FLOATING_STYLES}${jl_combobox_base_default}`;
207
+ }
208
+ onConnect() {
209
+ const native = this._refs.native;
210
+ this.on(native, "input", (event) => {
211
+ event.stopPropagation();
212
+ this.onType();
213
+ });
214
+ this.on(native, "keydown", (event) => this.onKey(event));
215
+ this.on(native, "blur", () => this.commitText());
216
+ this.on(native, "click", () => this.openList(false));
217
+ this.on(this._refs.toggle, "click", () => this.toggleList());
218
+ this.on(this._refs.clear, "click", () => this.clearValue());
219
+ this.on(this._refs.entry, "click", (event) => {
220
+ if (event.target === this._refs.entry) this.focus();
221
+ });
222
+ for (const element of [
223
+ this._refs.popup,
224
+ this._refs.toggle,
225
+ this._refs.clear
226
+ ]) this.on(element, "mousedown", (event) => event.preventDefault());
227
+ this.on(this._refs.listbox, "click", (event) => this.choose(this.optionAt(event)));
228
+ this.on(this._refs.listbox, "pointermove", (event) => this.hover(event));
229
+ }
230
+ onDisconnect() {
231
+ this._state.opening.abort();
232
+ this.cancelInput();
233
+ }
234
+ resetValue() {
235
+ this._state.query = "";
236
+ this.value.set(this.parseValue(this.getAttribute("value")));
237
+ }
238
+ setDisabledByForm(disabled) {
239
+ this._state.disabledByForm = disabled;
240
+ this.whenRendered(() => this.drawDisabled());
241
+ }
242
+ /** Disabled by its own input, or by a `<fieldset disabled>` around it. */
243
+ isOff() {
244
+ return this.disabled.get() || this._state.disabledByForm;
245
+ }
246
+ /** What stands in the field before its text: nothing, or the values held as chips. */
247
+ entryStart() {
248
+ return null;
249
+ }
250
+ /** Backspace in the field: whether the combobox used it. */
251
+ onBackspace() {
252
+ return false;
253
+ }
254
+ /** Tab takes the option the arrows reached, and lets the focus move on. */
255
+ onTab() {
256
+ const active = this._state.matches[this._state.active];
257
+ if (this.isOpen() && active !== void 0) this.choose(active);
258
+ return false;
259
+ }
260
+ /** The active option is the one the arrows reached: it is marked, and alone `aria-selected`. */
261
+ markActive(previous, item) {
262
+ previous?.classList.remove("active");
263
+ item?.classList.add("active");
264
+ previous?.setAttribute("aria-selected", "false");
265
+ item?.setAttribute("aria-selected", "true");
266
+ }
267
+ /** Whether an option's value is held, which its check shows. */
268
+ drawHeld(item, held) {
269
+ item.classList.toggle("chosen", held);
270
+ }
271
+ /** Whether the placeholder shows while the field's text is empty. */
272
+ placeholderShown() {
273
+ return true;
274
+ }
275
+ /** Whether the clear button has something to clear. */
276
+ isFilled() {
277
+ return this._refs.native.value !== "";
278
+ }
279
+ drawTexts() {
280
+ this._refs.clear.setAttribute("aria-label", jlText("clear"));
281
+ this._refs.toggle.setAttribute("aria-label", jlText("showOptions"));
282
+ this.renderEmptyText();
283
+ this.renderLoadingText();
284
+ this.renderValidity();
285
+ }
286
+ /** The values held, as a list. */
287
+ held() {
288
+ return heldValues(this.value.get());
289
+ }
290
+ /** A value's label: its option's, the one kept from when it was chosen, or the value itself as free text. */
291
+ displayOf(value) {
292
+ const option = this.options.get().find((candidate) => candidate.value === value);
293
+ if (option !== void 0) return option.label;
294
+ return this._state.labels.get(value) ?? (this.allowCustom.get() ? value : "");
295
+ }
296
+ rememberLabel(option) {
297
+ this._state.labels.set(option.value, option.label);
298
+ }
299
+ optionWithLabel(text) {
300
+ const folded = foldText(text);
301
+ return this.options.get().find((option) => option.disabled !== true && foldText(option.label) === folded);
302
+ }
303
+ /** Holds the values as people chose them, and says so when they changed. */
304
+ commitValues(values) {
305
+ const changed = !sameValues(values, this.held());
306
+ this.value.set(this.shaped(values));
307
+ if (changed) this.events.emit("change", this.value.get());
308
+ }
309
+ /** Empties the field's text, as after a choice. */
310
+ clearTyped() {
311
+ this._state.query = "";
312
+ this._refs.native.value = "";
313
+ }
314
+ /** Makes an option the active one, as after a choice that keeps the list open. */
315
+ activate(option) {
316
+ this._state.active = this._state.matches.indexOf(option);
317
+ this.renderActive();
318
+ }
319
+ announce(message) {
320
+ const status = this._refs.status;
321
+ if (status.textContent !== message) status.textContent = message;
322
+ }
323
+ /** The field's text (unless people are typing), the placeholder, the form's value, the validity, the clear button. */
324
+ renderValue() {
325
+ this._state.labels = keptLabels(this._state.labels, this.held(), this.options.get());
326
+ if (this._state.query === "") this._refs.native.value = this.fieldText();
327
+ this.renderPlaceholder();
328
+ this.renderFormValue();
329
+ this.renderValidity();
330
+ this.renderClear();
331
+ }
332
+ /** Without the attribute: the form says its state before the `disabled` input has taken the attribute's change. */
333
+ drawDisabled() {
334
+ this._refs.native.disabled = this.isOff();
335
+ this._refs.toggle.disabled = this.isOff();
336
+ this.closeList();
337
+ this.renderClear();
338
+ }
339
+ renderEmptyText() {
340
+ this._refs.empty.textContent = this.emptyText.get() || jlText("noResults");
341
+ }
342
+ renderLoadingText() {
343
+ this._refs.loadingText.textContent = this.loadingLabel();
344
+ }
345
+ loadingLabel() {
346
+ return this.loadingText.get() || jlText("loading");
347
+ }
348
+ isOpen() {
349
+ return this._refs.popup.matches(":popover-open");
350
+ }
351
+ onType() {
352
+ const text = this._refs.native.value;
353
+ this._state.query = text;
354
+ this.tellInput(text);
355
+ this.renderClear();
356
+ this.openList(true);
357
+ }
358
+ /** Says what was typed: at once, or once the typing pauses for `debounce` milliseconds. */
359
+ tellInput(text) {
360
+ this.cancelInput();
361
+ const delay = this.debounce.get();
362
+ if (delay <= NO_DEBOUNCE) {
363
+ this.events.emit("input", text);
364
+ return;
365
+ }
366
+ this._state.typing = window.setTimeout(() => this.events.emit("input", text), delay);
367
+ }
368
+ /** A choice made before the typing paused: the text it would have told is gone. */
369
+ cancelInput() {
370
+ window.clearTimeout(this._state.typing);
371
+ }
372
+ onKey(event) {
373
+ const handler = (/* @__PURE__ */ new Map([
374
+ ["ArrowDown", () => this.onArrow(1, event.altKey)],
375
+ ["ArrowUp", () => this.onArrow(-1, event.altKey)],
376
+ ["Backspace", () => this.onBackspace()],
377
+ ["Enter", () => this.onEnter()],
378
+ ["Escape", () => this.onEscape()],
379
+ ["Tab", () => this.onTab()]
380
+ ])).get(event.key);
381
+ if (handler === void 0) return;
382
+ if (handler()) {
383
+ event.preventDefault();
384
+ return;
385
+ }
386
+ if (event.key === "Enter") this.submitImplicitly(event);
387
+ }
388
+ /** Down opens the list or moves down it; Alt + Down opens it as it is, Alt + Up closes it. */
389
+ onArrow(direction, alt) {
390
+ if (alt) {
391
+ this.toggleWithAlt(direction);
392
+ return true;
393
+ }
394
+ if (!this.isOpen()) {
395
+ this.openList(false);
396
+ this.activateEdgeIfNone(direction);
397
+ return true;
398
+ }
399
+ this.moveActive(direction);
400
+ return true;
401
+ }
402
+ toggleWithAlt(direction) {
403
+ if (direction > 0) {
404
+ this.openList(false);
405
+ return;
406
+ }
407
+ this.closeList();
408
+ }
409
+ /** Enter chooses the active option; with free text allowed, it takes the text. It submits the form otherwise. */
410
+ onEnter() {
411
+ const active = this._state.matches[this._state.active];
412
+ if (this.isOpen() && active !== void 0) {
413
+ this.choose(active);
414
+ return true;
415
+ }
416
+ if (this._state.query !== "") {
417
+ this.commitText();
418
+ return true;
419
+ }
420
+ if (this.isOpen()) {
421
+ this.closeList();
422
+ return true;
423
+ }
424
+ return false;
425
+ }
426
+ /** Escape closes the list, then a second one puts away unfinished typing. */
427
+ onEscape() {
428
+ if (this.isOpen()) {
429
+ this.closeList();
430
+ return true;
431
+ }
432
+ if (this._state.query === "") return false;
433
+ this._state.query = "";
434
+ this.renderValue();
435
+ return true;
436
+ }
437
+ toggleList() {
438
+ if (this.isOpen()) {
439
+ this.closeList();
440
+ return;
441
+ }
442
+ this.openList(false);
443
+ this.focus();
444
+ }
445
+ /**
446
+ * Opens the list, or refreshes it, for what was typed. While typing, the first match is active, so Enter takes
447
+ * it; otherwise the first option held is. With free text allowed and nothing matching, the list stays closed,
448
+ * unless the page is still finding options.
449
+ */
450
+ openList(typing) {
451
+ const loading = this.loading.get();
452
+ if (this.isOff() || this.options.get().length === 0 && this._state.query === "" && !loading) return;
453
+ this.renderOptions();
454
+ const matches = this._state.matches;
455
+ const autoActive = typing && this._state.query.trim() !== "" && !this.allowCustom.get();
456
+ this._state.active = autoActive ? edgeEnabledIndex(matches, 1) : this.chosenIndex();
457
+ this.renderActive();
458
+ if (matches.length === 0 && this.allowCustom.get() && !loading) {
459
+ this.closeList();
460
+ return;
461
+ }
462
+ if (!this.isOpen()) this.showPopup();
463
+ this.revealActive();
464
+ }
465
+ showPopup() {
466
+ const popup = this._refs.popup;
467
+ popup.style.width = `${this._refs.control.offsetWidth}px`;
468
+ popup.showPopover();
469
+ this._state.opening.abort();
470
+ this._state.opening = new AbortController();
471
+ followAnchor(this._refs.control, popup, PLACEMENT, this._state.opening.signal);
472
+ this._refs.native.setAttribute("aria-expanded", "true");
473
+ this.reflect("open", true);
474
+ }
475
+ closeList() {
476
+ this._state.active = NONE;
477
+ this.renderActive();
478
+ if (!this.isOpen()) return;
479
+ this._refs.popup.hidePopover();
480
+ this._state.opening.abort();
481
+ this._refs.native.setAttribute("aria-expanded", "false");
482
+ this.reflect("open", false);
483
+ this.announce("");
484
+ }
485
+ /** The first option held among those shown. */
486
+ chosenIndex() {
487
+ const held = new Set(this.held());
488
+ return this._state.matches.findIndex((option) => held.has(option.value) && option.disabled !== true);
489
+ }
490
+ activateEdgeIfNone(direction) {
491
+ if (this._state.active !== NONE) return;
492
+ this._state.active = edgeEnabledIndex(this._state.matches, direction);
493
+ this.renderActive();
494
+ this.revealActive();
495
+ }
496
+ moveActive(direction) {
497
+ this._state.active = nextEnabledIndex(this._state.matches, this._state.active, direction);
498
+ this.renderActive();
499
+ this.revealActive();
500
+ }
501
+ optionAt(event) {
502
+ const item = event.target.closest("[data-index]");
503
+ return item === null ? void 0 : this._state.matches[Number(item.dataset.index)];
504
+ }
505
+ hover(event) {
506
+ const option = this.optionAt(event);
507
+ const index = option === void 0 ? NONE : this._state.matches.indexOf(option);
508
+ if (index === this._state.active || option?.disabled === true) return;
509
+ this._state.active = index;
510
+ this.renderActive();
511
+ }
512
+ choose(option) {
513
+ if (option === void 0 || option.disabled === true) return;
514
+ this.cancelInput();
515
+ this.rememberLabel(option);
516
+ this.chooseOption(option);
517
+ }
518
+ /** Leaving the field, or Enter on typed text. */
519
+ commitText() {
520
+ this.closeList();
521
+ this.cancelInput();
522
+ const text = this._refs.native.value.trim();
523
+ this._state.query = "";
524
+ this.commitTyped(text);
525
+ }
526
+ clearValue() {
527
+ this.cancelInput();
528
+ this.closeList();
529
+ this.clearTyped();
530
+ this.commitValues([]);
531
+ this.focus();
532
+ }
533
+ /** What screen readers hear as the list shows: how many options match, or that none does. */
534
+ countText(count) {
535
+ if (count === 0) return this.emptyText.get() || jlText("noResults");
536
+ return count === 1 ? jlText("oneResult") : jlText("resultCount", { count });
537
+ }
538
+ /** New options: the field may now show the value's label, and an open list (or one being typed in) refreshes. */
539
+ renderOptionsChange() {
540
+ this.renderValue();
541
+ this.refreshList();
542
+ }
543
+ refreshList() {
544
+ const typing = this._state.query !== "";
545
+ if (this.isOpen() || typing && this.matches(":focus-within")) this.openList(typing);
546
+ }
547
+ renderLoading(loading) {
548
+ this.reflect("loading", loading);
549
+ setAriaFlag(this._refs.listbox, "aria-busy", loading);
550
+ this.refreshList();
551
+ }
552
+ /**
553
+ * The options to show for what was typed, in their order: elements already made are moved, not made again. While
554
+ * the page finds options, the list says so above them, and says nothing of an empty answer yet.
555
+ */
556
+ renderOptions() {
557
+ const matches = FILTERS[this.filter.get()](this.options.get(), this._state.query);
558
+ const held = new Set(this.held());
559
+ const listbox = this._refs.listbox;
560
+ const loading = this.loading.get();
561
+ this._state.matches = matches;
562
+ matches.forEach((option, index) => {
563
+ const item = this.drawOption(option, index, held);
564
+ const current = listbox.children.item(index);
565
+ if (current !== item) listbox.insertBefore(item, current);
566
+ });
567
+ while (listbox.children.length > matches.length) listbox.lastElementChild?.remove();
568
+ listbox.hidden = matches.length === 0;
569
+ this._refs.loading.hidden = !loading;
570
+ this._refs.empty.hidden = loading || matches.length > 0;
571
+ this.announce(loading ? this.loadingLabel() : this.countText(matches.length));
572
+ }
573
+ /** The option's element, made once, with where it now sits, whether it is held, and the typed text marked. */
574
+ drawOption(option, index, held) {
575
+ const item = this._items.get(option) ?? this.optionNode(option);
576
+ this._items.set(option, item);
577
+ item.id = `${this._listId}-${index}`;
578
+ item.dataset.index = String(index);
579
+ this.drawHeld(item, held.has(option.value));
580
+ item.querySelector(".option-label")?.replaceChildren(...highlighted(option.label, this._state.query));
581
+ return item;
582
+ }
583
+ optionNode(option) {
584
+ return /* @__PURE__ */ jsxs("div", {
585
+ class: "option",
586
+ role: "option",
587
+ "aria-selected": "false",
588
+ "aria-disabled": option.disabled === true ? "true" : void 0,
589
+ children: [/* @__PURE__ */ jsxs("span", {
590
+ class: "text",
591
+ children: [/* @__PURE__ */ jsx("span", { class: "option-label" }), option.description === void 0 ? null : /* @__PURE__ */ jsx("span", {
592
+ class: "description",
593
+ children: option.description
594
+ })]
595
+ }), /* @__PURE__ */ jsx(JlIcon, {
596
+ class: "check",
597
+ name: "check",
598
+ size: CHECK_SIZE
599
+ })]
600
+ });
601
+ }
602
+ /** aria-activedescendant follows the option the arrows reached. */
603
+ renderActive() {
604
+ const option = this._state.matches[this._state.active];
605
+ const item = option === void 0 ? null : this._items.get(option) ?? null;
606
+ this.markActive(this._state.activeItem, item);
607
+ this._state.activeItem = item;
608
+ setOrRemove(this._refs.native, "aria-activedescendant", item?.id ?? "");
609
+ }
610
+ revealActive() {
611
+ const item = this._state.activeItem;
612
+ if (item !== null) keepInView(this._refs.listbox, item);
613
+ }
614
+ renderPlaceholder() {
615
+ setOrRemove(this._refs.native, "placeholder", this.placeholderShown() ? this.placeholder.get() : "");
616
+ }
617
+ renderValidity() {
618
+ if (this.required.get() && this.held().length === 0) {
619
+ this.internals.setValidity({ valueMissing: true }, jlText("chooseOption"), this._refs.native);
620
+ return;
621
+ }
622
+ this.internals.setValidity({});
623
+ }
624
+ renderClear() {
625
+ this._refs.clear.hidden = !(this.clearable.get() && !this.isOff() && this.isFilled());
626
+ }
627
+ renderDisabled(disabled) {
628
+ this.reflect("disabled", disabled);
629
+ this.drawDisabled();
630
+ }
631
+ };
632
+ export { COMBOBOX_FILTERS, JlComboboxBase };
package/dist/index.d.ts CHANGED
@@ -113,6 +113,7 @@ export * from './molecules/jl-list-item';
113
113
  export * from './molecules/jl-load-more';
114
114
  export * from './molecules/jl-load-state';
115
115
  export * from './molecules/jl-menu';
116
+ export * from './molecules/jl-multi-combobox';
116
117
  export * from './molecules/jl-number-input';
117
118
  export * from './molecules/jl-pagination';
118
119
  export * from './molecules/jl-popover';
package/dist/index.js CHANGED
@@ -43,6 +43,7 @@ import { CARD_PADDINGS, JlCard } from "./molecules/jl-card.js";
43
43
  import { JlChipGroup } from "./molecules/jl-chip-group.js";
44
44
  import { JlCopyButton } from "./molecules/jl-copy-button.js";
45
45
  import { CODE_LANGUAGES, JlCodeBlock } from "./molecules/jl-code-block.js";
46
+ import { COMBOBOX_FILTERS } from "./base/jl-combobox-base.js";
46
47
  import { JlCombobox } from "./molecules/jl-combobox.js";
47
48
  import { JlDatePicker } from "./molecules/jl-date-picker.js";
48
49
  import { EMPTY_SIZES, JlEmpty } from "./molecules/jl-empty.js";
@@ -54,6 +55,7 @@ import { JlListItem } from "./molecules/jl-list-item.js";
54
55
  import { JlLoadMore } from "./molecules/jl-load-more.js";
55
56
  import { JlLoadState, LOAD_SKELETONS, LOAD_STATES } from "./molecules/jl-load-state.js";
56
57
  import { JlMenu } from "./molecules/jl-menu.js";
58
+ import { JlMultiCombobox } from "./molecules/jl-multi-combobox.js";
57
59
  import { JlNumberInput } from "./molecules/jl-number-input.js";
58
60
  import { JlPagination } from "./molecules/jl-pagination.js";
59
61
  import { JlPopover } from "./molecules/jl-popover.js";
@@ -79,7 +81,7 @@ import { JlPageHeader } from "./organisms/jl-page-header.js";
79
81
  import { JlPageOutlet } from "./organisms/jl-page-outlet.js";
80
82
  import { JlSideNav, SIDE_NAV_COLLAPSIBLES } from "./organisms/jl-side-nav.js";
81
83
  import { JlTabBar } from "./organisms/jl-tab-bar.js";
82
- import { JlTable, TABLE_EXPANSIONS, TABLE_SORT_DIRECTIONS } from "./organisms/jl-table.js";
84
+ import { JlTable, TABLE_EXPANSIONS, TABLE_SORT_DIRECTIONS, TABLE_SORT_MODES } from "./organisms/jl-table.js";
83
85
  import { JlToaster, TOASTER_PLACEMENTS, download, downloadTexts, toast } from "./organisms/jl-toaster.js";
84
86
  import { JlTree } from "./organisms/jl-tree.js";
85
87
  import { JlAppShell } from "./templates/jl-app-shell.js";
@@ -87,4 +89,4 @@ import { JlAuthLayout } from "./templates/jl-auth-layout.js";
87
89
  import { JlPage, PAGE_WIDTHS } from "./templates/jl-page.js";
88
90
  import { JlSplitView, SPLIT_ORIENTATIONS, SPLIT_PANES } from "./templates/jl-split-view.js";
89
91
  import { JlStack, STACK_GAPS } from "./templates/jl-stack.js";
90
- export { ACCORDION_VARIANTS, AVATAR_SHAPES, AVATAR_SIZES, AVATAR_STATUSES, BASE_STYLES, BUTTON_TYPES, BUTTON_VARIANTS, CARD_PADDINGS, CHIP_SIZES, CODE_LANGUAGES, COMMON_ICONS, CONFIRM_TONES, CONTROL_SIZES, CONTROL_STYLES, DEFAULT_TEXTS, DIALOG_SIZES, DIVIDER_ALIGNS, DRAWER_PLACEMENTS, DRAWER_SIZES, EMPTY_SIZES, FACTS_ORIENTATIONS, FIELD_ORIENTATIONS, FLOATING_STYLES, INPUT_TYPES, JL_LANGUAGES, JlAccordion, JlAccordionItem, JlAccountButton, JlAlert, JlAppIcon, JlAppShell, JlAuthLayout, JlAvatar, JlAvatarGroup, JlBadge, JlBreadcrumb, JlButton, JlCalendar, JlCard, JlCheckbox, JlChip, JlChipGroup, JlCodeBlock, JlCombobox, JlCommandPalette, JlConfirm, JlCopyButton, JlDatePicker, JlDialog, JlDivider, JlDrawer, JlElement, JlEmpty, JlFacts, JlField, JlFileDrop, JlFlow, JlFormControl, JlIcon, JlInput, JlInputList, JlKbd, JlLink, JlListItem, JlLoadMore, JlLoadState, JlLoadingBar, JlMenu, JlNavbar, JlNumberInput, JlPage, JlPageHeader, JlPageOutlet, JlPagination, JlPopover, JlProgress, JlProgressRing, JlRadioGroup, JlRating, JlSearch, JlSearchButton, JlSegmented, JlSelect, JlSideNav, JlSkeleton, JlSlider, JlSpinner, JlSplitView, JlStack, JlStat, JlSteps, JlSwitch, JlTabBar, JlTabPanel, JlTable, JlTabs, JlTagInput, JlTextarea, JlTimeline, JlToast, JlToaster, JlTooltip, JlTree, LINK_VARIANTS, LOAD_SKELETONS, LOAD_STATES, PAGE_WIDTHS, PLACEMENTS, PROGRESS_SIZES, PROGRESS_TONES, RADIO_GROUP_ORIENTATIONS, RADIO_GROUP_VARIANTS, SIDE_NAV_COLLAPSIBLES, SKELETON_SHAPES, SPLIT_ORIENTATIONS, SPLIT_PANES, STACK_GAPS, STAT_TRENDS, STEPS_ORIENTATIONS, SWITCH_SIZES, TABLE_EXPANSIONS, TABLE_SORT_DIRECTIONS, TAB_VARIANTS, TOASTER_PLACEMENTS, TOAST_TONES, TOKEN_STYLES, TONES, TONE_ICONS, TONE_ICON_FILLED, adoptTokens, cellOf, clampedNumber, computePosition, confirmDialog, define, download, downloadTexts, followAnchor, formatDuration, formatRelativeTime, guardLeave, hasIcon, iconElement, iconNames, jlText, jlTexts, jsonOf, oneOf, placeFloating, registerIcons, setFavicon, toast };
92
+ export { ACCORDION_VARIANTS, AVATAR_SHAPES, AVATAR_SIZES, AVATAR_STATUSES, BASE_STYLES, BUTTON_TYPES, BUTTON_VARIANTS, CARD_PADDINGS, CHIP_SIZES, CODE_LANGUAGES, COMBOBOX_FILTERS, COMMON_ICONS, CONFIRM_TONES, CONTROL_SIZES, CONTROL_STYLES, DEFAULT_TEXTS, DIALOG_SIZES, DIVIDER_ALIGNS, DRAWER_PLACEMENTS, DRAWER_SIZES, EMPTY_SIZES, FACTS_ORIENTATIONS, FIELD_ORIENTATIONS, FLOATING_STYLES, INPUT_TYPES, JL_LANGUAGES, JlAccordion, JlAccordionItem, JlAccountButton, JlAlert, JlAppIcon, JlAppShell, JlAuthLayout, JlAvatar, JlAvatarGroup, JlBadge, JlBreadcrumb, JlButton, JlCalendar, JlCard, JlCheckbox, JlChip, JlChipGroup, JlCodeBlock, JlCombobox, JlCommandPalette, JlConfirm, JlCopyButton, JlDatePicker, JlDialog, JlDivider, JlDrawer, JlElement, JlEmpty, JlFacts, JlField, JlFileDrop, JlFlow, JlFormControl, JlIcon, JlInput, JlInputList, JlKbd, JlLink, JlListItem, JlLoadMore, JlLoadState, JlLoadingBar, JlMenu, JlMultiCombobox, JlNavbar, JlNumberInput, JlPage, JlPageHeader, JlPageOutlet, JlPagination, JlPopover, JlProgress, JlProgressRing, JlRadioGroup, JlRating, JlSearch, JlSearchButton, JlSegmented, JlSelect, JlSideNav, JlSkeleton, JlSlider, JlSpinner, JlSplitView, JlStack, JlStat, JlSteps, JlSwitch, JlTabBar, JlTabPanel, JlTable, JlTabs, JlTagInput, JlTextarea, JlTimeline, JlToast, JlToaster, JlTooltip, JlTree, LINK_VARIANTS, LOAD_SKELETONS, LOAD_STATES, PAGE_WIDTHS, PLACEMENTS, PROGRESS_SIZES, PROGRESS_TONES, RADIO_GROUP_ORIENTATIONS, RADIO_GROUP_VARIANTS, SIDE_NAV_COLLAPSIBLES, SKELETON_SHAPES, SPLIT_ORIENTATIONS, SPLIT_PANES, STACK_GAPS, STAT_TRENDS, STEPS_ORIENTATIONS, SWITCH_SIZES, TABLE_EXPANSIONS, TABLE_SORT_DIRECTIONS, TABLE_SORT_MODES, TAB_VARIANTS, TOASTER_PLACEMENTS, TOAST_TONES, TOKEN_STYLES, TONES, TONE_ICONS, TONE_ICON_FILLED, adoptTokens, cellOf, clampedNumber, computePosition, confirmDialog, define, download, downloadTexts, followAnchor, formatDuration, formatRelativeTime, guardLeave, hasIcon, iconElement, iconNames, jlText, jlTexts, jsonOf, oneOf, placeFloating, registerIcons, setFavicon, toast };
@@ -0,0 +1,12 @@
1
+ import type { ComboboxOption } from '../types/jl-combobox.types';
2
+ /** The values a combobox holds, as a list: none, one, or several with `multiple`. */
3
+ export declare function heldValues(value: string | readonly string[]): string[];
4
+ /** The values with one taken out when it was held, or added at the end when it was not. */
5
+ export declare const toggledValue: (values: readonly string[], value: string) => string[];
6
+ /** Whether two lists hold the same values in the same order. */
7
+ export declare const sameValues: (a: readonly string[], b: readonly string[]) => boolean;
8
+ /**
9
+ * The label of each value held: its option's among the options given, else the one kept before, so a value chosen
10
+ * among options the page has since replaced keeps its label. A value no longer held is forgotten.
11
+ */
12
+ export declare function keptLabels(kept: ReadonlyMap<string, string>, values: readonly string[], options: readonly ComboboxOption[]): Map<string, string>;