@kirbydesign/extensions-angular 4.2.0 → 4.4.0-rc.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.
@@ -0,0 +1,827 @@
1
+ import * as i0 from '@angular/core';
2
+ import { EventEmitter, RendererStyleFlags2, ElementRef, TemplateRef, forwardRef, HostListener, ContentChildren, ViewChildren, ViewChild, ContentChild, HostBinding, Output, Input, ChangeDetectionStrategy, Component } from '@angular/core';
3
+ import { NG_VALUE_ACCESSOR } from '@angular/forms';
4
+ import { CardComponent } from '@kirbydesign/designsystem/card';
5
+ import { UniqueIdGenerator } from '@kirbydesign/designsystem/helpers';
6
+ import { ItemComponent } from '@kirbydesign/designsystem/item';
7
+ import { ListItemTemplateDirective } from '@kirbydesign/designsystem/list';
8
+ import { HorizontalDirection, PopoverComponent } from '@kirbydesign/designsystem/popover';
9
+ import { InputSize, InputComponent, AffixDirective } from '@kirbydesign/designsystem/form-field';
10
+ import { FORM_FIELD_CONTROL } from '@kirbydesign/designsystem/types';
11
+ import * as i1 from '@kirbydesign/designsystem/shared';
12
+ import { forwardAttributes } from '@kirbydesign/designsystem/shared';
13
+ import { IconComponent } from '@kirbydesign/designsystem/icon';
14
+ import { NgTemplateOutlet } from '@angular/common';
15
+ import { CdkFixedSizeVirtualScroll, CdkVirtualScrollViewport, CdkVirtualForOf } from '@angular/cdk/scrolling';
16
+
17
+ var OpenState;
18
+ (function (OpenState) {
19
+ OpenState[OpenState["closed"] = 0] = "closed";
20
+ OpenState[OpenState["opening"] = 1] = "opening";
21
+ OpenState[OpenState["open"] = 2] = "open";
22
+ })(OpenState || (OpenState = {}));
23
+
24
+ class ComboboxComponent {
25
+ static { this.OPEN_DELAY_IN_MS = 100; }
26
+ compareAlphabetically(textA, textB) {
27
+ if (textA < textB) {
28
+ return -1;
29
+ }
30
+ if (textA > textB) {
31
+ return 1;
32
+ }
33
+ return 0;
34
+ }
35
+ getItemText(item) {
36
+ if (this.isTypeString(item)) {
37
+ return item;
38
+ }
39
+ if (this.objectHasItemTextProperty(item)) {
40
+ const objectItem = item;
41
+ const textValue = objectItem[this.itemTextProperty];
42
+ if (typeof textValue === 'string') {
43
+ return textValue;
44
+ }
45
+ }
46
+ return '';
47
+ }
48
+ get dropdownMaxHeight() {
49
+ return 8 * this.itemHeight;
50
+ }
51
+ get dropdownMinHeight() {
52
+ return this.itemHeight;
53
+ }
54
+ get viewportHeight() {
55
+ const itemCount = this.searchItems?.length ?? 0;
56
+ if (itemCount === 0) {
57
+ return this.dropdownMinHeight;
58
+ }
59
+ return Math.min(this.dropdownMaxHeight, Math.max(this.dropdownMinHeight, itemCount * this.itemHeight));
60
+ }
61
+ isTypeString(item) {
62
+ return typeof item === 'string';
63
+ }
64
+ objectHasItemTextProperty(item) {
65
+ return item != undefined && typeof item === 'object' && this.itemTextProperty in item;
66
+ }
67
+ objectHasItemIdProperty(item) {
68
+ return item != undefined && typeof item === 'object' && this.itemIdProperty in item;
69
+ }
70
+ getItemId(item) {
71
+ if (this.isTypeString(item)) {
72
+ return item;
73
+ }
74
+ if (this.objectHasItemIdProperty(item)) {
75
+ const objectItem = item;
76
+ const idValue = objectItem[this.itemIdProperty];
77
+ if (this.isTypeString(idValue)) {
78
+ return idValue;
79
+ }
80
+ }
81
+ console.error('Each item must have an id property for scroll to work. Ensure that the itemIdProperty input is set correctly, and that each item has a unique id value.');
82
+ return '';
83
+ }
84
+ get searchItems() {
85
+ return this._searchItems;
86
+ }
87
+ set searchItems(value) {
88
+ this._searchItems = value;
89
+ if (!this._searchItems) {
90
+ return;
91
+ }
92
+ this.focusedItem = this._searchItems[0];
93
+ }
94
+ get items() {
95
+ return this._items;
96
+ }
97
+ /**
98
+ * The list of items to display in the dropdown.
99
+ * @param value
100
+ */
101
+ set items(value) {
102
+ this._items = value;
103
+ this.searchItems = this._items;
104
+ }
105
+ get selectedItem() {
106
+ return this._selectedItem;
107
+ }
108
+ /**
109
+ * The currently selected item.
110
+ * @param value
111
+ */
112
+ set selectedItem(value) {
113
+ // Allow clearing by setting undefined
114
+ if (value !== this._selectedItem) {
115
+ this._selectedItem = value;
116
+ this.focusedItem = this._selectedItem;
117
+ // Keep ControlValueAccessor value in sync with the selected data item
118
+ this.value = this._selectedItem;
119
+ }
120
+ }
121
+ get focusedItem() {
122
+ return this._focusedItem;
123
+ }
124
+ set focusedItem(item) {
125
+ // the focus might not have changed, but where in the list might
126
+ this.setAriaPosinsetOnElement(item);
127
+ if (this._focusedItem === item) {
128
+ return;
129
+ }
130
+ this._focusedItem = item;
131
+ }
132
+ getIndexOfItem(item) {
133
+ return this.searchItems.indexOf(item);
134
+ }
135
+ getKirbyElement(item) {
136
+ const kirbyItems = this.getKirbyItems();
137
+ if (!kirbyItems || !item) {
138
+ return undefined;
139
+ }
140
+ const itemId = this.getItemId(item);
141
+ return kirbyItems.find((el) => el.nativeElement.id === itemId);
142
+ }
143
+ setAriaPosinsetOnElement(item) {
144
+ const element = this.getKirbyElement(item);
145
+ const index = this.getIndexOfItem(item);
146
+ const setsize = this.searchItems.length;
147
+ if (!element) {
148
+ return;
149
+ }
150
+ this.renderer.setAttribute(element.nativeElement, 'aria-setsize', setsize.toString());
151
+ this.renderer.setAttribute(element.nativeElement, 'aria-posintset', `${index + 1}`);
152
+ }
153
+ /**
154
+ * The direction in which the dropdown should open relative to the input.
155
+ * @param direction
156
+ */
157
+ set popout(direction) {
158
+ this._popout = direction || HorizontalDirection.right;
159
+ }
160
+ get popout() {
161
+ return this._popout;
162
+ }
163
+ get _isDisabled() {
164
+ return this.disabled ? 'disabled' : null;
165
+ }
166
+ /**
167
+ * When `true`, the combobox will be styled with error styles.
168
+ */
169
+ get hasError() {
170
+ return this._hasError;
171
+ }
172
+ set hasError(value) {
173
+ if (this._hasError !== value) {
174
+ this._hasError = value;
175
+ this.hasErrorChange.emit(this._hasError);
176
+ }
177
+ }
178
+ // Prevent Ionic blur on scroll
179
+ get _noBlurOnScroll() {
180
+ return true;
181
+ }
182
+ get _isBlockLevel() {
183
+ return this.expand === 'block';
184
+ }
185
+ get _isOpening() {
186
+ return this.state === OpenState.opening;
187
+ }
188
+ get isOpen() {
189
+ return this.state === OpenState.open;
190
+ }
191
+ get hasValue() {
192
+ return this.selectedItem != null;
193
+ }
194
+ get interactiveElement() {
195
+ return this.textInput.nativeElement;
196
+ }
197
+ forwardAriaLabelToCombobox() {
198
+ forwardAttributes(this.elementRef.nativeElement, this._attributesToForward, this.renderer, this.rootElement.nativeElement);
199
+ }
200
+ set kirbyItemsSlotted(kirbyItems) {
201
+ const hasSlottedItems = this.disposeItemClickListeners?.length > 0;
202
+ if (hasSlottedItems) {
203
+ this.unlistenAllSlottedItems();
204
+ }
205
+ kirbyItems.forEach((kirbyItem) => {
206
+ this.renderer.setAttribute(kirbyItem.nativeElement, 'role', 'option');
207
+ const disposeClickListener = this.renderer.listen(kirbyItem.nativeElement, 'click', () => {
208
+ const id = kirbyItem.nativeElement.getAttribute('id');
209
+ const item = this.items.find((it) => this.getItemId(it) === id);
210
+ if (item) {
211
+ this.onItemSelect(item);
212
+ }
213
+ });
214
+ this.disposeItemClickListeners.push(disposeClickListener);
215
+ });
216
+ this._kirbyItemsSlotted = kirbyItems;
217
+ }
218
+ get kirbyItemsSlotted() {
219
+ return this._kirbyItemsSlotted;
220
+ }
221
+ constructor(renderer, elementRef, cdr, resizeObserverService) {
222
+ this.renderer = renderer;
223
+ this.elementRef = elementRef;
224
+ this.cdr = cdr;
225
+ this.resizeObserverService = resizeObserverService;
226
+ this.state = OpenState.closed;
227
+ this._popout = HorizontalDirection.right;
228
+ this._attributesToForward = ['aria-label', 'aria-labelledby'];
229
+ this._listboxId = UniqueIdGenerator.scopedTo('kirby-x-combobox').next();
230
+ this._comboboxId = UniqueIdGenerator.scopedTo('kirby-input').next();
231
+ this._defaultSearchFunction = (searchTerm, itemsToSearch) => {
232
+ if (!searchTerm) {
233
+ return itemsToSearch;
234
+ }
235
+ const lowerSearchTerm = searchTerm.toLowerCase();
236
+ return itemsToSearch
237
+ .filter((item) => this.getItemText(item).toLowerCase().includes(lowerSearchTerm))
238
+ .sort((a, b) => {
239
+ const textA = this.getItemText(a).toLowerCase();
240
+ const textB = this.getItemText(b).toLowerCase();
241
+ const textAStartsWith = textA.startsWith(searchTerm);
242
+ const textBStartsWith = textB.startsWith(searchTerm);
243
+ if (textAStartsWith && textBStartsWith) {
244
+ return this.compareAlphabetically(textA, textB);
245
+ }
246
+ if (textAStartsWith) {
247
+ return -1;
248
+ }
249
+ if (textBStartsWith) {
250
+ return 1;
251
+ }
252
+ return this.compareAlphabetically(textA, textB);
253
+ });
254
+ };
255
+ /**
256
+ * The text to display when the search returns no results.
257
+ */
258
+ this.noSearchResultsText = 'No results found.';
259
+ /**
260
+ * The name of the property to use as the display text for each item.
261
+ */
262
+ this.itemTextProperty = 'text';
263
+ /**
264
+ * The height of each item in the dropdown, in pixels. This is used to calculate the height of the dropdown based on the number of items.
265
+ */
266
+ this.itemHeight = 44;
267
+ /**
268
+ * The name of the property to use as the unique identifier for each item.
269
+ */
270
+ this.itemIdProperty = 'id';
271
+ /**
272
+ * A function that takes a search term and the list of items, and returns a filtered list of items to display in the dropdown.
273
+ */
274
+ this.searchFunction = this._defaultSearchFunction;
275
+ this._searchItems = [];
276
+ this._items = [];
277
+ this._selectedItem = undefined;
278
+ // _focusedItem keeps track of which element has focus and will be selected
279
+ // if it is activated (by pressing ENTER)
280
+ this._focusedItem = undefined;
281
+ /**
282
+ * The placeholder text to display in the input when no item is selected and the input is empty.
283
+ */
284
+ this.placeholder = 'Please search...';
285
+ /**
286
+ * When `true`, the user cannot interact with the combobox.
287
+ */
288
+ this.disabled = false;
289
+ /**
290
+ * Will emit the new value when error changes.
291
+ */
292
+ this.hasErrorChange = new EventEmitter();
293
+ this._hasError = false;
294
+ /**
295
+ * The size of the input element.
296
+ */
297
+ this.size = InputSize.medium;
298
+ /**
299
+ * Emitted when an item is selected (tap on mobile, click/keypress on web)
300
+ */
301
+ this.change = new EventEmitter();
302
+ this.value = undefined;
303
+ this.disposeItemClickListeners = [];
304
+ this.onChange = () => {
305
+ /* empty */
306
+ };
307
+ this.onTouched = () => {
308
+ /* empty */
309
+ };
310
+ }
311
+ onToggle(event) {
312
+ event.stopPropagation();
313
+ this.textInput?.nativeElement.focus();
314
+ this.toggle();
315
+ }
316
+ toggle() {
317
+ if (this.disabled) {
318
+ return;
319
+ }
320
+ this.isOpen ? this.close() : this.open();
321
+ }
322
+ ngAfterViewInit() {
323
+ if (this.expand === 'block') {
324
+ const { width: initialWidth } = this.elementRef.nativeElement.getBoundingClientRect();
325
+ this.setPopoverCardStyle('max-width', 'initial');
326
+ this.setPopoverCardStyle('min-width', 'initial');
327
+ // Ensure initial width is set even if the resize observer callback also fires initially:
328
+ this.setPopoverCardStyle('--kirby-card-width', `${initialWidth}px`);
329
+ this.resizeObserverService.observe(this.elementRef, (entry) => {
330
+ const newWidth = entry.contentRect.width;
331
+ if (newWidth > 0) {
332
+ this.setPopoverCardStyle('--kirby-card-width', `${newWidth}px`);
333
+ }
334
+ });
335
+ }
336
+ this.forwardAriaLabelToCombobox();
337
+ }
338
+ /* Utility that makes it easier to set styles on card element */
339
+ setPopoverCardStyle(style, value) {
340
+ this.renderer.setStyle(this.cardElement?.nativeElement, style, value, RendererStyleFlags2.DashCase);
341
+ }
342
+ ngOnDestroy() {
343
+ this.unlistenAllSlottedItems();
344
+ this.unobserveResize();
345
+ }
346
+ open() {
347
+ if (this.disabled) {
348
+ return;
349
+ }
350
+ if (!this.isOpen) {
351
+ this.state = OpenState.opening;
352
+ setTimeout(() => this.showPopOver(), ComboboxComponent.OPEN_DELAY_IN_MS);
353
+ this.focusedItem = this.selectedItem;
354
+ }
355
+ }
356
+ showPopOver() {
357
+ if (this.state === OpenState.opening) {
358
+ this.state = OpenState.open;
359
+ this.popover?.show();
360
+ this.scrollToIndexIntoViewWhenOpeningPopup();
361
+ this.cdr.markForCheck();
362
+ }
363
+ }
364
+ close() {
365
+ if (this.disabled) {
366
+ return;
367
+ }
368
+ if (this.isOpen) {
369
+ this.state = OpenState.closed;
370
+ this.setInputDisplayValue(this.getItemText(this.selectedItem));
371
+ this.searchItems = this.items;
372
+ this.popover?.hide();
373
+ }
374
+ }
375
+ onItemSelect(item) {
376
+ // Guard: slotted click listeners should map back to a data item, but be safe.
377
+ if (item instanceof HTMLElement)
378
+ return;
379
+ this.selectItem(item);
380
+ this.close();
381
+ }
382
+ /**
383
+ * Sets the select's value. Part of the ControlValueAccessor interface
384
+ * required to integrate with Angular's core forms API.
385
+ *
386
+ * @param value New value to be written to the model.
387
+ */
388
+ writeValue(value) {
389
+ this.selectItemByInput(value);
390
+ // When written from outside, reflect selected text in the input
391
+ this.setInputDisplayValue(this.getItemText(this.value));
392
+ this.cdr.markForCheck();
393
+ }
394
+ /**
395
+ * Saves a callback function to be invoked when the select's value
396
+ * changes from user input. Part of the ControlValueAccessor interface
397
+ * required to integrate with Angular's core forms API.
398
+ *
399
+ * @param fn Callback to be triggered when the value changes.
400
+ */
401
+ registerOnChange(fn) {
402
+ this.onChange = fn;
403
+ }
404
+ /**
405
+ * Saves a callback function to be invoked when the select is blurred
406
+ * by the user. Part of the ControlValueAccessor interface required
407
+ * to integrate with Angular's core forms API.
408
+ *
409
+ * @param fn Callback to be triggered when the component has been touched.
410
+ */
411
+ registerOnTouched(fn) {
412
+ this.onTouched = fn;
413
+ }
414
+ /**
415
+ * Disables the select. Part of the ControlValueAccessor interface required
416
+ * to integrate with Angular's core forms API.
417
+ *
418
+ * @param isDisabled Sets whether the component is disabled.
419
+ */
420
+ setDisabledState(isDisabled) {
421
+ this.disabled = isDisabled;
422
+ this.cdr.markForCheck();
423
+ }
424
+ selectItem(item) {
425
+ if (item != this.selectedItem) {
426
+ this.selectedItem = item;
427
+ this.focusedItem = item;
428
+ this.change.emit(this.value);
429
+ this.onChange(this.value);
430
+ this.setInputDisplayValue(this.getItemText(item));
431
+ this.searchItems = this.items;
432
+ }
433
+ }
434
+ selectItemByInput(input) {
435
+ this.selectedItem = this.findItemByInput(input);
436
+ }
437
+ updateSearchResults(event) {
438
+ if (!this.isOpen) {
439
+ this.open();
440
+ }
441
+ const input = event.target;
442
+ this.searchItems = input.value ? this.searchFunction(input.value, this.items) : this.items;
443
+ // Screen readers (VoiceOver/NVDA) may move real browser focus to a list-item button.
444
+ // When the list re-renders after filtering, that button is removed from the DOM and
445
+ // focus falls to `document.body`. Restore it to the input so the user can keep typing
446
+ // and the focusout handler doesn't mistake the body-focus for a genuine blur-away.
447
+ if (document.activeElement !== this.textInput?.nativeElement) {
448
+ this.textInput?.nativeElement.focus();
449
+ }
450
+ }
451
+ onPopoverWillHide() {
452
+ this.state = OpenState.closed;
453
+ this.rootElement.nativeElement.focus();
454
+ this.onTouched();
455
+ }
456
+ onPopoverClick() {
457
+ this.close();
458
+ }
459
+ onMouseDown(event) {
460
+ if (this.disabled) {
461
+ event.preventDefault();
462
+ event.stopImmediatePropagation();
463
+ }
464
+ }
465
+ onTouchStart(event) {
466
+ if (this.isOpen) {
467
+ event.stopPropagation();
468
+ }
469
+ }
470
+ onFocusOut() {
471
+ // Defer the check to allow the browser (and screen readers like VoiceOver/NVDA)
472
+ // to settle focus. `relatedTarget` is often `null` with screen readers, so we
473
+ // inspect `document.activeElement` after the microtask queue has flushed.
474
+ setTimeout(() => {
475
+ const activeElement = document.activeElement;
476
+ const isOnTriggerButton = activeElement && this.elementRef.nativeElement.contains(activeElement);
477
+ const isInsidePopover = activeElement && activeElement.closest('kirby-popover');
478
+ if (isOnTriggerButton || isInsidePopover) {
479
+ // Focus is still within the component — nothing to do.
480
+ return;
481
+ }
482
+ // When a screen reader (VoiceOver/NVDA) moves real focus to a list-item button
483
+ // and the user then types a character, CDK virtual scroll re-renders the list and
484
+ // removes the focused button from the DOM. The browser then drops focus to
485
+ // `document.body`. Detect this case: if the popover is still showing and focus is
486
+ // on the body, it means focus was lost due to DOM removal — restore it to the
487
+ // input rather than closing the popover.
488
+ if (this.isOpen && activeElement === document.body) {
489
+ this.textInput?.nativeElement.focus();
490
+ return;
491
+ }
492
+ if (this.isOpen) {
493
+ this.close();
494
+ }
495
+ this.onTouched();
496
+ }, 0);
497
+ }
498
+ onTab() {
499
+ if (this.isOpen) {
500
+ this.selectItem(this.focusedItem);
501
+ this.close();
502
+ }
503
+ }
504
+ onEscape() {
505
+ this.close();
506
+ }
507
+ onEnterO(event) {
508
+ event.preventDefault();
509
+ event.stopPropagation();
510
+ if (this.isOpen) {
511
+ this.selectItem(this.focusedItem);
512
+ }
513
+ this.toggle();
514
+ }
515
+ onArrowKeys(event) {
516
+ if (this.disabled)
517
+ return;
518
+ const keyEvent = event; // safe in this context
519
+ // Avoid page scroll
520
+ event.preventDefault();
521
+ if (!this.isOpen) {
522
+ this.open();
523
+ if (this.selectedItem) {
524
+ this.setFocusOnSelectedItem();
525
+ }
526
+ else {
527
+ this.setFocusOnFirstOrLastItem(keyEvent);
528
+ }
529
+ }
530
+ else {
531
+ this.handleFocusNavigation(keyEvent);
532
+ }
533
+ }
534
+ handleFocusNavigation(keyEvent) {
535
+ switch (keyEvent.key) {
536
+ case 'ArrowDown':
537
+ this.shiftFocusIndex(1);
538
+ break;
539
+ case 'ArrowUp':
540
+ this.shiftFocusIndex(-1);
541
+ break;
542
+ case 'PageDown':
543
+ this.shiftFocusIndex(10);
544
+ break;
545
+ case 'PageUp':
546
+ this.shiftFocusIndex(-10);
547
+ break;
548
+ case 'Home':
549
+ this.setFocusOnFirstItem();
550
+ break;
551
+ case 'End':
552
+ this.setFocusOnLastItem();
553
+ break;
554
+ default:
555
+ break;
556
+ }
557
+ }
558
+ shiftFocusIndex(numberOfItems) {
559
+ // Handle up/down navigation when open
560
+ const currentIndex = this.searchItems.indexOf(this.focusedItem);
561
+ let newIndex;
562
+ if (numberOfItems > 0) {
563
+ newIndex = Math.min(this.searchItems.length - 1, currentIndex + numberOfItems);
564
+ }
565
+ else {
566
+ newIndex = Math.max(0, currentIndex + numberOfItems);
567
+ }
568
+ if (newIndex !== currentIndex) {
569
+ this.focusedItem = this.searchItems[newIndex];
570
+ }
571
+ this.scrollFocusedItemIntoViewWhileNavigating();
572
+ }
573
+ setFocusOnFirstOrLastItem(keyEvent) {
574
+ switch (keyEvent.key) {
575
+ case 'ArrowDown':
576
+ case 'PageDown':
577
+ case 'Home':
578
+ this.setFocusOnFirstItem();
579
+ break;
580
+ case 'ArrowUp':
581
+ case 'PageUp':
582
+ case 'End':
583
+ this.setFocusOnLastItem();
584
+ break;
585
+ default:
586
+ break;
587
+ }
588
+ }
589
+ setFocusOnSelectedItem() {
590
+ if (this.selectedItem) {
591
+ this.focusedItem = this.selectedItem;
592
+ }
593
+ }
594
+ setFocusOnLastItem() {
595
+ if (this.searchItems.length > 0) {
596
+ this.focusedItem = this.searchItems[this.searchItems.length - 1];
597
+ }
598
+ }
599
+ setFocusOnFirstItem() {
600
+ if (this.searchItems.length > 0) {
601
+ this.focusedItem = this.searchItems[0];
602
+ }
603
+ }
604
+ unlistenAllSlottedItems() {
605
+ let disposeClickListener;
606
+ while ((disposeClickListener = this.disposeItemClickListeners.pop()) !== undefined) {
607
+ disposeClickListener();
608
+ }
609
+ }
610
+ unobserveResize() {
611
+ this.resizeObserverService.unobserve(this.elementRef);
612
+ }
613
+ /**
614
+ * Resolve a model/data item based on current `items` by comparing displayed text.
615
+ * This is used to map an incoming form value or input `selectedItem` to an item
616
+ * from the list.
617
+ */
618
+ findItemByInput(input) {
619
+ if (!this.items || this.items.length === 0) {
620
+ return input;
621
+ }
622
+ return this.items.find((it) => this.getItemText(it) === input) ?? input;
623
+ }
624
+ setInputDisplayValue(value) {
625
+ // We intentionally update the DOM input imperatively ("hard way")
626
+ // to ensure the visible value is cleared/updated immediately.
627
+ if (this.textInput?.nativeElement) {
628
+ this.renderer.setProperty(this.textInput.nativeElement, 'value', value);
629
+ }
630
+ }
631
+ scrollFocusedItemIntoViewWhileNavigating() {
632
+ const kirbyItems = this.getKirbyItems();
633
+ if (!kirbyItems || kirbyItems.length === 0)
634
+ return;
635
+ if (!this.focusedItem) {
636
+ return;
637
+ }
638
+ const id = this.getItemId(this.focusedItem);
639
+ if (!id)
640
+ return;
641
+ const match = kirbyItems.toArray().find((el) => {
642
+ if (el.nativeElement.id === undefined) {
643
+ console.error('Each item must have an id attribute for scroll to work. Ensure that the ´[attr.id]´ is set correctly, and that each item has a unique id value.');
644
+ }
645
+ return el.nativeElement.id === id;
646
+ });
647
+ if (!match) {
648
+ this.scrollToIndexIntoViewWhenOpeningPopup();
649
+ return;
650
+ }
651
+ const scrollIntoView = match.nativeElement.scrollIntoView;
652
+ if (typeof scrollIntoView === 'function') {
653
+ scrollIntoView.call(match.nativeElement, { block: 'nearest' });
654
+ }
655
+ }
656
+ getKirbyItems() {
657
+ return this.kirbyItemsSlotted && this.kirbyItemsSlotted.length
658
+ ? this.kirbyItemsSlotted
659
+ : this.kirbyItemsDefault;
660
+ }
661
+ scrollToIndexIntoViewWhenOpeningPopup() {
662
+ const focusedIndex = this.searchItems.indexOf(this.focusedItem);
663
+ if (focusedIndex < 0)
664
+ return;
665
+ if (focusedIndex === 0) {
666
+ this.virtualScrollViewport?.setRenderedRange({ start: 0, end: 20 });
667
+ this.virtualScrollViewport?.checkViewportSize();
668
+ return;
669
+ }
670
+ this.virtualScrollViewport?.scrollToIndex(focusedIndex);
671
+ }
672
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ComboboxComponent, deps: [{ token: i0.Renderer2 }, { token: i0.ElementRef }, { token: i0.ChangeDetectorRef }, { token: i1.ResizeObserverService }], target: i0.ɵɵFactoryTarget.Component }); }
673
+ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.7", type: ComboboxComponent, isStandalone: true, selector: "kirby-x-combobox", inputs: { noSearchResultsText: "noSearchResultsText", itemTextProperty: "itemTextProperty", itemHeight: "itemHeight", itemIdProperty: "itemIdProperty", searchFunction: "searchFunction", items: "items", selectedItem: "selectedItem", placeholder: "placeholder", popout: "popout", expand: "expand", disabled: "disabled", hasError: "hasError", size: "size" }, outputs: { hasErrorChange: "hasErrorChange", change: "change" }, host: { listeners: { "mousedown": "onMouseDown($event)", "touchstart": "onTouchStart($event)", "focusout": "onFocusOut()", "keydown.tab": "onTab()", "keydown.escape": "onEscape()", "keydown.enter": "onEnterO($event)", "keydown.arrowUp": "onArrowKeys($event)", "keydown.arrowDown": "onArrowKeys($event)", "keydown.pageUp": "onArrowKeys($event)", "keydown.pageDown": "onArrowKeys($event)", "keydown.home": "onArrowKeys($event)", "keydown.end": "onArrowKeys($event)" }, properties: { "attr.disabled": "this._isDisabled", "attr.no-blur": "this._noBlurOnScroll", "class.expand": "this._isBlockLevel", "class.is-opening": "this._isOpening", "class.is-open": "this.isOpen", "class.has-value": "this.hasValue" } }, providers: [
674
+ {
675
+ provide: NG_VALUE_ACCESSOR,
676
+ useExisting: forwardRef(() => ComboboxComponent),
677
+ multi: true,
678
+ },
679
+ {
680
+ provide: FORM_FIELD_CONTROL,
681
+ useExisting: forwardRef(() => ComboboxComponent),
682
+ },
683
+ ], queries: [{ propertyName: "itemTemplate", first: true, predicate: ListItemTemplateDirective, descendants: true, read: TemplateRef }, { propertyName: "slottedItems", predicate: ListItemTemplateDirective, read: ElementRef }, { propertyName: "kirbyItemsSlotted", predicate: ItemComponent, read: ElementRef }], viewQueries: [{ propertyName: "cardElement", first: true, predicate: CardComponent, descendants: true, read: ElementRef }, { propertyName: "popover", first: true, predicate: PopoverComponent, descendants: true }, { propertyName: "rootElement", first: true, predicate: ["rootElement"], descendants: true, read: ElementRef, static: true }, { propertyName: "textInput", first: true, predicate: InputComponent, descendants: true, read: ElementRef, static: true }, { propertyName: "virtualScrollViewport", first: true, predicate: CdkVirtualScrollViewport, descendants: true }, { propertyName: "kirbyItemsDefault", predicate: ItemComponent, descendants: true, read: ElementRef }], ngImport: i0, template: "<div (click)=\"onToggle($event)\" #rootElement>\n <input\n kirby-input\n type=\"text\"\n aria-haspopup=\"listbox\"\n role=\"combobox\"\n [hasError]=\"hasError\"\n [size]=\"size\"\n [disabled]=\"disabled\"\n [placeholder]=\"placeholder\"\n [attr.id]=\"_comboboxId\"\n [attr.aria-controls]=\"_listboxId\"\n [attr.aria-expanded]=\"isOpen\"\n [attr.aria-activedescendant]=\"isOpen && focusedItem ? getItemId(focusedItem) : null\"\n (input)=\"updateSearchResults($event)\"\n />\n <kirby-icon [name]=\"isOpen ? 'arrow-up' : 'arrow-down'\" kirby-affix=\"suffix\"></kirby-icon>\n</div>\n\n<kirby-popover\n [target]=\"rootElement\"\n [popout]=\"popout\"\n (click)=\"onPopoverClick()\"\n (willHide)=\"onPopoverWillHide()\"\n>\n <kirby-card [attr.id]=\"_listboxId\" [attr.aria-labelledby]=\"_comboboxId\">\n @if (searchItems.length === 0) {\n <kirby-item [attr.aria-live]=\"'polite'\">\n <p class=\"kirby-item-title no-results\">{{ noSearchResultsText }}</p>\n </kirby-item>\n } @else {\n <cdk-virtual-scroll-viewport\n class=\"combobox-viewport\"\n role=\"listbox\"\n minBufferPx=\"1240\"\n maxBufferPx=\"1620\"\n [itemSize]=\"itemHeight\"\n [style.height.px]=\"viewportHeight\"\n >\n <ng-container *cdkVirtualFor=\"let item of searchItems\">\n <ng-container\n *ngTemplateOutlet=\"\n itemTemplate || defaultItemTemplate;\n context: {\n $implicit: item,\n selected: item === selectedItem,\n focused: item === focusedItem,\n }\n \"\n ></ng-container>\n </ng-container>\n </cdk-virtual-scroll-viewport>\n }\n </kirby-card>\n</kirby-popover>\n\n<ng-template #defaultItemTemplate let-item let-selected=\"selected\" let-focused=\"focused\">\n <kirby-item\n role=\"option\"\n [selectable]=\"true\"\n [selected]=\"selected\"\n [class.focused]=\"focused\"\n [attr.id]=\"getItemId(item)\"\n [attr.aria-selected]=\"selected\"\n (click)=\"onItemSelect(item)\"\n >\n <p class=\"kirby-item-title\">{{ getItemText(item) }}</p>\n @if (selected) {\n <kirby-icon name=\"checkmark-selected\" slot=\"end\"></kirby-icon>\n }\n </kirby-item>\n</ng-template>\n", styles: [".kirby-line-clamp{display:-webkit-box;-webkit-line-clamp:var(--line-clamp, none);-webkit-box-orient:vertical;overflow:hidden}@media(hover:hover)and (pointer:fine){:host{outline:none;border-radius:999px}:host:focus{transition:all 80ms linear 0ms;box-shadow:0 0 0 2px var(--kirby-background-color),0 0 0 4px #4d90fe}:host:focus:not(:focus-visible){box-shadow:0 0 0 0 transparent}:host:focus-visible{transition:all 80ms linear 0ms;box-shadow:0 0 0 2px var(--kirby-background-color),0 0 0 4px #4d90fe}}:host{display:inline-block;position:relative;max-width:calc(100vw - 32px)}:host.clicked{box-shadow:none}:host.expand{display:block}:host.expand kirby-card{width:100%;min-width:initial;max-width:initial}:host div>input[kirby-input]{padding-right:40px}:host div>input[kirby-input]:disabled~kirby-icon{color:var(--kirby-semi-dark-shade)}:host div>kirby-icon{display:flex;align-items:center;position:absolute;top:0;bottom:0;right:12px}:host.has-value>div>input[kirby-input]{text-overflow:ellipsis}kirby-popover{--max-height: 352px}kirby-card{display:flex;flex-direction:column;max-height:352px;overflow:hidden;box-shadow:0 0 5px #1c1c1c0a,0 10px 15px -10px #1c1c1c26;max-width:calc(100vw - 32px);min-width:288px}@media(min-width:321px){kirby-card{min-width:343px}}.combobox-viewport{display:block;width:100%}.no-results{color:var(--kirby-text-color-semi-dark)}\n"], dependencies: [{ kind: "component", type: InputComponent, selector: "input[kirby-input]", inputs: ["type", "size", "borderless", "hasError", "autocomplete", "autocorrect", "value", "maxlength", "inputmode"], outputs: ["hasErrorChange"] }, { kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "component", type: PopoverComponent, selector: "kirby-popover", inputs: ["popout", "target"], outputs: ["willHide"] }, { kind: "component", type: CardComponent, selector: "kirby-card", inputs: ["title", "subtitle", "backgroundImageUrl", "hasPadding", "sizes", "variant"] }, { kind: "component", type: ItemComponent, selector: "kirby-item", inputs: ["disabled", "selected", "disclosure", "selectable", "reorderable", "size", "href", "rotateIcon"] }, { kind: "component", type: IconComponent, selector: "kirby-icon", inputs: ["size", "name"] }, { kind: "directive", type: AffixDirective, selector: "[kirby-affix]", inputs: ["kirby-affix"] }, { kind: "directive", type: CdkFixedSizeVirtualScroll, selector: "cdk-virtual-scroll-viewport[itemSize]", inputs: ["itemSize", "minBufferPx", "maxBufferPx"] }, { kind: "component", type: CdkVirtualScrollViewport, selector: "cdk-virtual-scroll-viewport", inputs: ["orientation", "appendOnly"], outputs: ["scrolledIndexChange"] }, { kind: "directive", type: CdkVirtualForOf, selector: "[cdkVirtualFor][cdkVirtualForOf]", inputs: ["cdkVirtualForOf", "cdkVirtualForTrackBy", "cdkVirtualForTemplate", "cdkVirtualForTemplateCacheSize"] }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
684
+ }
685
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.7", ngImport: i0, type: ComboboxComponent, decorators: [{
686
+ type: Component,
687
+ args: [{ selector: 'kirby-x-combobox', providers: [
688
+ {
689
+ provide: NG_VALUE_ACCESSOR,
690
+ useExisting: forwardRef(() => ComboboxComponent),
691
+ multi: true,
692
+ },
693
+ {
694
+ provide: FORM_FIELD_CONTROL,
695
+ useExisting: forwardRef(() => ComboboxComponent),
696
+ },
697
+ ], imports: [
698
+ InputComponent,
699
+ NgTemplateOutlet,
700
+ PopoverComponent,
701
+ CardComponent,
702
+ ItemComponent,
703
+ IconComponent,
704
+ AffixDirective,
705
+ CdkFixedSizeVirtualScroll,
706
+ CdkVirtualScrollViewport,
707
+ CdkVirtualForOf,
708
+ ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<div (click)=\"onToggle($event)\" #rootElement>\n <input\n kirby-input\n type=\"text\"\n aria-haspopup=\"listbox\"\n role=\"combobox\"\n [hasError]=\"hasError\"\n [size]=\"size\"\n [disabled]=\"disabled\"\n [placeholder]=\"placeholder\"\n [attr.id]=\"_comboboxId\"\n [attr.aria-controls]=\"_listboxId\"\n [attr.aria-expanded]=\"isOpen\"\n [attr.aria-activedescendant]=\"isOpen && focusedItem ? getItemId(focusedItem) : null\"\n (input)=\"updateSearchResults($event)\"\n />\n <kirby-icon [name]=\"isOpen ? 'arrow-up' : 'arrow-down'\" kirby-affix=\"suffix\"></kirby-icon>\n</div>\n\n<kirby-popover\n [target]=\"rootElement\"\n [popout]=\"popout\"\n (click)=\"onPopoverClick()\"\n (willHide)=\"onPopoverWillHide()\"\n>\n <kirby-card [attr.id]=\"_listboxId\" [attr.aria-labelledby]=\"_comboboxId\">\n @if (searchItems.length === 0) {\n <kirby-item [attr.aria-live]=\"'polite'\">\n <p class=\"kirby-item-title no-results\">{{ noSearchResultsText }}</p>\n </kirby-item>\n } @else {\n <cdk-virtual-scroll-viewport\n class=\"combobox-viewport\"\n role=\"listbox\"\n minBufferPx=\"1240\"\n maxBufferPx=\"1620\"\n [itemSize]=\"itemHeight\"\n [style.height.px]=\"viewportHeight\"\n >\n <ng-container *cdkVirtualFor=\"let item of searchItems\">\n <ng-container\n *ngTemplateOutlet=\"\n itemTemplate || defaultItemTemplate;\n context: {\n $implicit: item,\n selected: item === selectedItem,\n focused: item === focusedItem,\n }\n \"\n ></ng-container>\n </ng-container>\n </cdk-virtual-scroll-viewport>\n }\n </kirby-card>\n</kirby-popover>\n\n<ng-template #defaultItemTemplate let-item let-selected=\"selected\" let-focused=\"focused\">\n <kirby-item\n role=\"option\"\n [selectable]=\"true\"\n [selected]=\"selected\"\n [class.focused]=\"focused\"\n [attr.id]=\"getItemId(item)\"\n [attr.aria-selected]=\"selected\"\n (click)=\"onItemSelect(item)\"\n >\n <p class=\"kirby-item-title\">{{ getItemText(item) }}</p>\n @if (selected) {\n <kirby-icon name=\"checkmark-selected\" slot=\"end\"></kirby-icon>\n }\n </kirby-item>\n</ng-template>\n", styles: [".kirby-line-clamp{display:-webkit-box;-webkit-line-clamp:var(--line-clamp, none);-webkit-box-orient:vertical;overflow:hidden}@media(hover:hover)and (pointer:fine){:host{outline:none;border-radius:999px}:host:focus{transition:all 80ms linear 0ms;box-shadow:0 0 0 2px var(--kirby-background-color),0 0 0 4px #4d90fe}:host:focus:not(:focus-visible){box-shadow:0 0 0 0 transparent}:host:focus-visible{transition:all 80ms linear 0ms;box-shadow:0 0 0 2px var(--kirby-background-color),0 0 0 4px #4d90fe}}:host{display:inline-block;position:relative;max-width:calc(100vw - 32px)}:host.clicked{box-shadow:none}:host.expand{display:block}:host.expand kirby-card{width:100%;min-width:initial;max-width:initial}:host div>input[kirby-input]{padding-right:40px}:host div>input[kirby-input]:disabled~kirby-icon{color:var(--kirby-semi-dark-shade)}:host div>kirby-icon{display:flex;align-items:center;position:absolute;top:0;bottom:0;right:12px}:host.has-value>div>input[kirby-input]{text-overflow:ellipsis}kirby-popover{--max-height: 352px}kirby-card{display:flex;flex-direction:column;max-height:352px;overflow:hidden;box-shadow:0 0 5px #1c1c1c0a,0 10px 15px -10px #1c1c1c26;max-width:calc(100vw - 32px);min-width:288px}@media(min-width:321px){kirby-card{min-width:343px}}.combobox-viewport{display:block;width:100%}.no-results{color:var(--kirby-text-color-semi-dark)}\n"] }]
709
+ }], ctorParameters: () => [{ type: i0.Renderer2 }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }, { type: i1.ResizeObserverService }], propDecorators: { noSearchResultsText: [{
710
+ type: Input
711
+ }], itemTextProperty: [{
712
+ type: Input
713
+ }], itemHeight: [{
714
+ type: Input
715
+ }], itemIdProperty: [{
716
+ type: Input
717
+ }], searchFunction: [{
718
+ type: Input
719
+ }], items: [{
720
+ type: Input
721
+ }], selectedItem: [{
722
+ type: Input
723
+ }], placeholder: [{
724
+ type: Input
725
+ }], popout: [{
726
+ type: Input
727
+ }], expand: [{
728
+ type: Input
729
+ }], disabled: [{
730
+ type: Input
731
+ }], _isDisabled: [{
732
+ type: HostBinding,
733
+ args: ['attr.disabled']
734
+ }], hasErrorChange: [{
735
+ type: Output
736
+ }], hasError: [{
737
+ type: Input
738
+ }], size: [{
739
+ type: Input
740
+ }], _noBlurOnScroll: [{
741
+ type: HostBinding,
742
+ args: ['attr.no-blur']
743
+ }], change: [{
744
+ type: Output
745
+ }], _isBlockLevel: [{
746
+ type: HostBinding,
747
+ args: ['class.expand']
748
+ }], _isOpening: [{
749
+ type: HostBinding,
750
+ args: ['class.is-opening']
751
+ }], isOpen: [{
752
+ type: HostBinding,
753
+ args: ['class.is-open']
754
+ }], hasValue: [{
755
+ type: HostBinding,
756
+ args: ['class.has-value']
757
+ }], itemTemplate: [{
758
+ type: ContentChild,
759
+ args: [ListItemTemplateDirective, { read: TemplateRef }]
760
+ }], slottedItems: [{
761
+ type: ContentChildren,
762
+ args: [ListItemTemplateDirective, { read: ElementRef }]
763
+ }], cardElement: [{
764
+ type: ViewChild,
765
+ args: [CardComponent, { read: ElementRef }]
766
+ }], popover: [{
767
+ type: ViewChild,
768
+ args: [PopoverComponent]
769
+ }], rootElement: [{
770
+ type: ViewChild,
771
+ args: ['rootElement', { static: true, read: ElementRef }]
772
+ }], textInput: [{
773
+ type: ViewChild,
774
+ args: [InputComponent, { static: true, read: ElementRef }]
775
+ }], virtualScrollViewport: [{
776
+ type: ViewChild,
777
+ args: [CdkVirtualScrollViewport]
778
+ }], kirbyItemsDefault: [{
779
+ type: ViewChildren,
780
+ args: [ItemComponent, { read: ElementRef }]
781
+ }], kirbyItemsSlotted: [{
782
+ type: ContentChildren,
783
+ args: [ItemComponent, { read: ElementRef }]
784
+ }], onMouseDown: [{
785
+ type: HostListener,
786
+ args: ['mousedown', ['$event']]
787
+ }], onTouchStart: [{
788
+ type: HostListener,
789
+ args: ['touchstart', ['$event']]
790
+ }], onFocusOut: [{
791
+ type: HostListener,
792
+ args: ['focusout']
793
+ }], onTab: [{
794
+ type: HostListener,
795
+ args: ['keydown.tab']
796
+ }], onEscape: [{
797
+ type: HostListener,
798
+ args: ['keydown.escape']
799
+ }], onEnterO: [{
800
+ type: HostListener,
801
+ args: ['keydown.enter', ['$event']]
802
+ }], onArrowKeys: [{
803
+ type: HostListener,
804
+ args: ['keydown.arrowUp', ['$event']]
805
+ }, {
806
+ type: HostListener,
807
+ args: ['keydown.arrowDown', ['$event']]
808
+ }, {
809
+ type: HostListener,
810
+ args: ['keydown.pageUp', ['$event']]
811
+ }, {
812
+ type: HostListener,
813
+ args: ['keydown.pageDown', ['$event']]
814
+ }, {
815
+ type: HostListener,
816
+ args: ['keydown.home', ['$event']]
817
+ }, {
818
+ type: HostListener,
819
+ args: ['keydown.end', ['$event']]
820
+ }] } });
821
+
822
+ /**
823
+ * Generated bundle index. Do not edit.
824
+ */
825
+
826
+ export { ComboboxComponent };
827
+ //# sourceMappingURL=kirbydesign-extensions-angular-combobox.mjs.map