• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 18007030997

25 Sep 2025 12:11PM UTC coverage: 91.587% (+0.005%) from 91.582%
18007030997

Pull #16246

github

web-flow
Merge 410ba571d into 894a1d18a
Pull Request #16246: Update Combo and Simple Combo Keyboard Navigation & Add Escape Key behavior

13841 of 16226 branches covered (85.3%)

25 of 26 new or added lines in 5 files covered. (96.15%)

19 existing lines in 4 files now uncovered.

27794 of 30347 relevant lines covered (91.59%)

34867.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.65
/projects/igniteui-angular/src/lib/simple-combo/simple-combo.component.ts
1
import { NgTemplateOutlet } from '@angular/common';
2
import {
3
    AfterViewInit, ChangeDetectorRef, Component, DoCheck, ElementRef, EventEmitter, HostListener, Inject, Injector,
4
    Optional, Output, ViewChild, DOCUMENT
5
} from '@angular/core';
6
import { ControlValueAccessor, FormGroupDirective, NG_VALUE_ACCESSOR } from '@angular/forms';
7
import { takeUntil } from 'rxjs/operators';
8

9
import { IgxComboAddItemComponent } from '../combo/combo-add-item.component';
10
import { IgxComboDropDownComponent } from '../combo/combo-dropdown.component';
11
import { IgxComboItemComponent } from '../combo/combo-item.component';
12
import { IgxComboAPIService } from '../combo/combo.api';
13
import { IgxComboBaseDirective, IGX_COMBO_COMPONENT } from '../combo/combo.common';
14
import { IgxSelectionAPIService } from '../core/selection';
15
import { CancelableEventArgs, IBaseCancelableBrowserEventArgs, IBaseEventArgs, PlatformUtil } from '../core/utils';
16
import { IgxButtonDirective } from '../directives/button/button.directive';
17
import { IgxForOfDirective } from '../directives/for-of/for_of.directive';
18
import { IgxRippleDirective } from '../directives/ripple/ripple.directive';
19
import { IgxTextSelectionDirective } from '../directives/text-selection/text-selection.directive';
20
import { IgxIconService } from '../icon/icon.service';
21
import { IgxInputGroupType, IGX_INPUT_GROUP_TYPE } from '../input-group/public_api';
22
import { IgxComboFilteringPipe, IgxComboGroupingPipe } from '../combo/combo.pipes';
23
import { IgxDropDownItemNavigationDirective } from '../drop-down/drop-down-navigation.directive';
24
import { IgxIconComponent } from '../icon/icon.component';
25
import { IgxSuffixDirective } from '../directives/suffix/suffix.directive';
26
import { IgxInputDirective } from '../directives/input/input.directive';
27
import { IgxInputGroupComponent } from '../input-group/input-group.component';
28

29
/** Emitted when an igx-simple-combo's selection is changing.  */
30
export interface ISimpleComboSelectionChangingEventArgs extends CancelableEventArgs, IBaseEventArgs {
31
    /** An object which represents the value that is currently selected */
32
    oldValue: any;
33
    /** An object which represents the value that will be selected after this event */
34
    newValue: any;
35
    /** An object which represents the item that is currently selected */
36
    oldSelection: any;
37
    /** An object which represents the item that will be selected after this event */
38
    newSelection: any;
39
    /** The text that will be displayed in the combo text box */
40
    displayText: string;
41
}
42

43
/**
44
 * Represents a drop-down list that provides filtering functionality, allowing users to choose a single option from a predefined list.
45
 *
46
 * @igxModule IgxSimpleComboModule
47
 * @igxTheme igx-combo-theme
48
 * @igxKeywords combobox, single combo selection
49
 * @igxGroup Grids & Lists
50
 *
51
 * @remarks
52
 * It provides the ability to filter items as well as perform single selection on the provided data.
53
 * Additionally, it exposes keyboard navigation and custom styling capabilities.
54
 * @example
55
 * ```html
56
 * <igx-simple-combo [itemsMaxHeight]="250" [data]="locationData"
57
 *  [displayKey]="'field'" [valueKey]="'field'"
58
 *  placeholder="Location" searchPlaceholder="Search...">
59
 * </igx-simple-combo>
60
 * ```
61
 */
62
@Component({
63
    selector: 'igx-simple-combo',
64
    templateUrl: 'simple-combo.component.html',
65
    providers: [
66
        IgxComboAPIService,
67
        { provide: IGX_COMBO_COMPONENT, useExisting: IgxSimpleComboComponent },
68
        { provide: NG_VALUE_ACCESSOR, useExisting: IgxSimpleComboComponent, multi: true }
69
    ],
70
    imports: [IgxInputGroupComponent, IgxInputDirective, IgxTextSelectionDirective, IgxSuffixDirective, NgTemplateOutlet, IgxIconComponent, IgxComboDropDownComponent, IgxDropDownItemNavigationDirective, IgxForOfDirective, IgxComboItemComponent, IgxComboAddItemComponent, IgxButtonDirective, IgxRippleDirective, IgxComboFilteringPipe, IgxComboGroupingPipe]
71
})
72
export class IgxSimpleComboComponent extends IgxComboBaseDirective implements ControlValueAccessor, AfterViewInit, DoCheck {
3✔
73
    /** @hidden @internal */
74
    @ViewChild(IgxComboDropDownComponent, { static: true })
75
    public dropdown: IgxComboDropDownComponent;
76

77
    /** @hidden @internal */
78
    @ViewChild(IgxComboAddItemComponent)
79
    public addItem: IgxComboAddItemComponent;
80

81
    /**
82
     * Emitted when item selection is changing, before the selection completes
83
     *
84
     * ```html
85
     * <igx-simple-combo (selectionChanging)='handleSelection()'></igx-simple-combo>
86
     * ```
87
     */
88
    @Output()
89
    public selectionChanging = new EventEmitter<ISimpleComboSelectionChangingEventArgs>();
133✔
90

91
    @ViewChild(IgxTextSelectionDirective, { static: true })
92
    private textSelection: IgxTextSelectionDirective;
93

94
    public override get value(): any {
95
        return this._value[0];
58✔
96
    }
97

98
    /**
99
     * Get current selection state
100
     *
101
     * @returns The selected item, if any
102
     * ```typescript
103
     * let mySelection = this.combo.selection;
104
     * ```
105
     */
106
    public override get selection(): any {
107
        return super.selection[0];
141✔
108
    }
109

110
    /** @hidden @internal */
111
    public composing = false;
133✔
112

113
    private _updateInput = true;
133✔
114

115
    private _collapsing = false;
133✔
116

117
    /** @hidden @internal */
118
    public get filteredData(): any[] | null {
119
        return this._filteredData;
1,670✔
120
    }
121
    /** @hidden @internal */
122
    public set filteredData(val: any[] | null) {
123
        this._filteredData = this.groupKey ? (val || []).filter((e) => e.isHeader !== true) : val;
7,326!
124
        this.checkMatch();
299✔
125
    }
126

127
    /** @hidden @internal */
128
    public override get searchValue(): string {
129
        return this._searchValue;
14,463✔
130
    }
131
    public override set searchValue(val: string) {
132
        this._searchValue = val;
216✔
133
    }
134

135
    private get selectedItem(): any {
136
        return this.selectionService.get(this.id).values().next().value;
139✔
137
    }
138

139
    protected get hasSelectedItem(): boolean {
140
        return !!this.selectionService.get(this.id).size;
1,096✔
141
    }
142

143
    constructor(elementRef: ElementRef,
144
        cdr: ChangeDetectorRef,
145
        selectionService: IgxSelectionAPIService,
146
        comboAPI: IgxComboAPIService,
147
        private platformUtil: PlatformUtil,
133✔
148
        @Inject(DOCUMENT) document: any,
149
        @Optional() @Inject(IGX_INPUT_GROUP_TYPE) _inputGroupType: IgxInputGroupType,
150
        @Optional() _injector: Injector,
151
        @Optional() @Inject(IgxIconService) _iconService?: IgxIconService,
152
        @Optional() private formGroupDirective?: FormGroupDirective
133✔
153
    ) {
154
        super(
133✔
155
            elementRef,
156
            cdr,
157
            selectionService,
158
            comboAPI,
159
            document,
160
            _inputGroupType,
161
            _injector,
162
            _iconService
163
        );
164
        this.comboAPI.register(this);
133✔
165
    }
166

167
    /** @hidden @internal */
168
    @HostListener('keydown.ArrowDown', ['$event'])
169
    @HostListener('keydown.Alt.ArrowDown', ['$event'])
170
    public onArrowDown(event: Event): void {
171
        if (this.collapsed) {
7✔
172
            event.preventDefault();
3✔
173
            event.stopPropagation();
3✔
174
            this.open();
3✔
175
        } else {
176
            if (this.virtDir.igxForOf.length > 0 && !this.hasSelectedItem) {
4✔
177
                this.dropdown.navigateNext();
3✔
178
                this.dropdownContainer.nativeElement.focus();
3✔
179
            } else if (this.allowCustomValues) {
1✔
180
                this.addItem?.element.nativeElement.focus();
1✔
181
            }
182
        }
183
    }
184

185
    /**
186
     * Select a defined item
187
     *
188
     * @param item the item to be selected
189
     * ```typescript
190
     * this.combo.select("New York");
191
     * ```
192
     */
193
    public select(item: any): void {
194
        if (item !== undefined) {
76✔
195
            const newSelection = this.selectionService.add_items(this.id, item instanceof Array ? item : [item], true);
76✔
196
            this.setSelection(newSelection);
76✔
197
        }
198
    }
199

200
    /**
201
     * Deselect the currently selected item
202
     *
203
     * @param item the items to be deselected
204
     * ```typescript
205
     * this.combo.deselect("New York");
206
     * ```
207
     */
208
    public deselect(): void {
209
        this.clearSelection();
4✔
210
    }
211

212
    /** @hidden @internal */
213
    public writeValue(value: any): void {
214
        const oldSelection = super.selection;
103✔
215
        this.selectionService.select_items(this.id, this.isValid(value) ? [value] : [], true);
103✔
216
        this.cdr.markForCheck();
103✔
217
        this._displayValue = this.createDisplayText(super.selection, oldSelection);
103✔
218
        this._value = this.valueKey ? super.selection.map(item => item[this.valueKey]) : super.selection;
103✔
219
        this.filterValue = this._displayValue?.toString() || '';
103✔
220
    }
221

222
    /** @hidden @internal */
223
    public override ngAfterViewInit(): void {
224
        this.virtDir.contentSizeChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
118✔
225
            if (super.selection.length > 0) {
1✔
226
                const index = this.virtDir.igxForOf.findIndex(e => {
1✔
227
                    let current = e? e[this.valueKey] : undefined;
10!
228
                    if (this.valueKey === null || this.valueKey === undefined) {
10!
229
                        current = e;
×
230
                    }
231
                    return current === super.selection[0];
10✔
232
                });
233
                if (!this.isRemote) {
1!
234
                    // navigate to item only if we have local data
235
                    // as with remote data this will fiddle with igxFor's scroll handler
236
                    // and will trigger another chunk load which will break the visualization
237
                    this.dropdown.navigateItem(index);
×
238
                }
239
            }
240
        });
241
        this.dropdown.opening.pipe(takeUntil(this.destroy$)).subscribe((args) => {
118✔
242
            if (args.cancel) {
72!
243
                return;
×
244
            }
245
            this._collapsing = false;
72✔
246
            const filtered = this.filteredData.find(this.findAllMatches);
72✔
247
            if (filtered === undefined || filtered === null) {
72✔
248
                this.filterValue = this.searchValue = this.comboInput.value;
43✔
249
                return;
43✔
250
            }
251
        });
252
        this.dropdown.opened.pipe(takeUntil(this.destroy$)).subscribe(() => {
118✔
253
            if (this.composing) {
26✔
254
                this.comboInput.focus();
4✔
255
            }
256
        });
257
        this.dropdown.closing.pipe(takeUntil(this.destroy$)).subscribe((args) => {
118✔
258
            if (args.cancel) {
37!
259
                return;
×
260
            }
261
            if (this.getEditElement() && !args.event) {
37✔
262
                this._collapsing = true;
35✔
263
            } else {
264
                this.clearOnBlur();
2✔
265
                this._onTouchedCallback();
2✔
266
            }
267
            this.comboInput.focus();
37✔
268
        });
269

270
        // in reactive form the control is not present initially
271
        // and sets the selection to an invalid value in writeValue method
272
        if (!this.isValid(this.selectedItem)) {
118✔
273
            this.selectionService.clear(this.id);
97✔
274
            this._displayValue = '';
97✔
275
        }
276

277
        super.ngAfterViewInit();
118✔
278
    }
279

280
    /** @hidden @internal */
281
    public ngDoCheck(): void {
282
        if (this.data?.length && super.selection.length && !this._displayValue) {
467✔
283
            this._displayValue = this.createDisplayText(super.selection, []);
44✔
284
            this._value = this.valueKey ? super.selection.map(item => item[this.valueKey]) : super.selection;
44✔
285
        }
286
    }
287

288
    /** @hidden @internal */
289
    public override handleInputChange(event?: any): void {
290
        if (this.collapsed && this.comboInput.focused) {
80✔
291
            this.open();
11✔
292
        }
293
        if (event !== undefined) {
80✔
294
            this.filterValue = this.searchValue = typeof event === 'string' ? event : event.target.value;
78✔
295
        }
296
        if (!this.comboInput.value.trim() && super.selection.length) {
80✔
297
            // handle clearing of input by space
298
            this.clearSelection();
1✔
299
            this._onChangeCallback(null);
1✔
300
            this.filterValue = '';
1✔
301
        }
302
        if (super.selection.length) {
80✔
303
            const args: ISimpleComboSelectionChangingEventArgs = {
9✔
304
                newValue: undefined,
305
                oldValue: this.selectedItem,
306
                newSelection: undefined,
307
                oldSelection: this.selection,
308
                displayText: typeof event === 'string' ? event : event?.target?.value,
9✔
309
                owner: this,
310
                cancel: false
311
            };
312
            this.selectionChanging.emit(args);
9✔
313
            if (!args.cancel) {
9✔
314
                this.selectionService.select_items(this.id, [], true);
5✔
315
            }
316
        }
317
        // when filtering the focused item should be the first item or the currently selected item
318
        if (!this.dropdown.focusedItem || this.dropdown.focusedItem.id !== this.dropdown.items[0].id) {
80✔
319
            this.dropdown.navigateFirst();
29✔
320
        }
321
        super.handleInputChange(event);
80✔
322
        this.composing = true;
80✔
323
    }
324

325
    /** @hidden @internal */
326
    public handleInputClick(): void {
327
        if (this.collapsed) {
3✔
328
            this.open();
1✔
329
            this.comboInput.focus();
1✔
330
        }
331
    }
332

333
    /** @hidden @internal */
334
    public override handleKeyDown(event: KeyboardEvent): void {
335
        if (event.key === this.platformUtil.KEYMAP.ENTER) {
99✔
336
            const filtered = this.filteredData.find(this.findAllMatches);
9✔
337
            if (filtered === null || filtered === undefined) {
9!
338
                return;
×
339
            }
340
            if (!this.dropdown.collapsed) {
9✔
341
                const focusedItem = this.dropdown.focusedItem;
8✔
342
                if (focusedItem && !focusedItem.isHeader) {
8✔
343
                    this.select(focusedItem.itemID);
7✔
344
                    event.preventDefault();
7✔
345
                    event.stopPropagation();
7✔
346
                    this.close();
7✔
347
                } else {
348
                    event.preventDefault();
1✔
349
                    event.stopPropagation();
1✔
350
                    this.comboInput.focus();
1✔
351
                }
352
            }
353
            // manually trigger text selection as it will not be triggered during editing
354
            this.textSelection.trigger();
9✔
355
            return;
9✔
356
        }
357
        if (event.key === this.platformUtil.KEYMAP.BACKSPACE
90✔
358
            || event.key === this.platformUtil.KEYMAP.DELETE) {
359
            this._updateInput = false;
2✔
360
            this.clearSelection(true);
2✔
361
        }
362
        if (!this.collapsed && event.key === this.platformUtil.KEYMAP.TAB) {
90✔
363
            const filtered = this.filteredData.find(this.findAllMatches);
14✔
364
            if (filtered === null || filtered === undefined) {
14✔
365
                this.clearOnBlur();
6✔
366
                this.close();
6✔
367
                return;
6✔
368
            }
369
            const focusedItem = this.dropdown.focusedItem;
8✔
370
            if (focusedItem && !focusedItem.isHeader) {
8✔
371
                this.select(focusedItem.itemID);
7✔
372
                this.close();
7✔
373
                this.textSelection.trigger();
7✔
374
            } else {
375
                this.clearOnBlur();
1✔
376
                this.close();
1✔
377
            }
378
        }
379
        if (event.key === this.platformUtil.KEYMAP.ESCAPE) {
84✔
380
            if (this.collapsed) {
1!
381
                const oldSelection = this.selection;
1✔
382
                this.clearSelection(true);
1✔
383
                if (this.selection !== oldSelection) {
1✔
384
                    this.comboInput.value = this.filterValue = this.searchValue = '';
1✔
385
                }
386
                this.dropdown.focusedItem = null;
1✔
387
                this.comboInput.focus();
1✔
388
            } else {
NEW
UNCOV
389
                this.close();
×
390
            }
391
        }
392
        this.composing = false;
84✔
393
        super.handleKeyDown(event);
84✔
394
    }
395

396
    /** @hidden @internal */
397
    public handleKeyUp(event: KeyboardEvent): void {
398
        if (event.key === this.platformUtil.KEYMAP.ARROW_DOWN) {
67✔
399
            this.dropdown.focusedItem = this.hasSelectedItem && this.filteredData.length > 0
3!
UNCOV
400
                ? this.dropdown.items.find(i => i.itemID === this.selectedItem)
×
401
                : this.dropdown.items[0];
402
            this.dropdownContainer.nativeElement.focus();
3✔
403
        }
404
    }
405

406
    /** @hidden @internal */
407
    public handleItemKeyDown(event: KeyboardEvent): void {
408
        if (event.key === this.platformUtil.KEYMAP.ARROW_UP && event.altKey) {
8!
UNCOV
409
            this.close();
×
UNCOV
410
            this.comboInput.focus();
×
UNCOV
411
            return;
×
412
        }
413
        if (event.key === this.platformUtil.KEYMAP.ENTER) {
8!
UNCOV
414
            this.comboInput.focus();
×
415
        }
416
    }
417

418
    /** @hidden @internal */
419
    public handleItemClick(): void {
420
        this.close();
27✔
421
        this.comboInput.focus();
27✔
422
    }
423

424
    /** @hidden @internal */
425
    public override onBlur(): void {
426
        // when clicking the toggle button to close the combo and immediately clicking outside of it
427
        // the collapsed state is not modified as the dropdown is still not closed
428
        if (this.collapsed || this._collapsing) {
14✔
429
            this.clearOnBlur();
10✔
430
        }
431
        super.onBlur();
14✔
432
    }
433

434
    /** @hidden @internal */
435
    public getEditElement(): HTMLElement {
436
        return this.comboInput.nativeElement;
37✔
437
    }
438

439
    /** @hidden @internal */
440
    public handleClear(event: Event): void {
441
        if (this.disabled) {
7✔
442
            return;
1✔
443
        }
444

445
        const oldSelection = this.selection;
6✔
446
        this.clearSelection(true);
6✔
447

448
        if (!this.collapsed) {
6✔
449
            this.focusSearchInput(true);
2✔
450
        }
451
        event.stopPropagation();
6✔
452

453
        if (this.selection !== oldSelection) {
6✔
454
            this.comboInput.value = this.filterValue = this.searchValue = '';
5✔
455
        }
456

457
        this.dropdown.focusedItem = null;
6✔
458
        this.composing = false;
6✔
459
        this.comboInput.focus();
6✔
460
    }
461

462
    /** @hidden @internal */
463
    public handleOpened(): void {
464
        this.triggerCheck();
26✔
465
        if (!this.comboInput.focused) {
26✔
466
            this.dropdownContainer.nativeElement.focus();
12✔
467
        }
468
        this.opened.emit({ owner: this });
26✔
469
    }
470

471
    /** @hidden @internal */
472
    public override handleClosing(e: IBaseCancelableBrowserEventArgs): void {
473
        const args: IBaseCancelableBrowserEventArgs = { owner: this, event: e.event, cancel: e.cancel };
39✔
474
        this.closing.emit(args);
39✔
475
        e.cancel = args.cancel;
39✔
476
        if (e.cancel) {
39✔
477
            return;
1✔
478
        }
479

480
        this.composing = false;
38✔
481
        // explicitly update selection so that we don't have to force CD
482
        const isTab = (e.event as KeyboardEvent)?.key === this.platformUtil.KEYMAP.TAB;
38✔
483
        this.textSelection.selected = !isTab;
38✔
484
    }
485

486
    /** @hidden @internal */
487
    public focusSearchInput(opening?: boolean): void {
488
        if (opening) {
8✔
489
            this.dropdownContainer.nativeElement.focus();
2✔
490
        } else {
491
            this.comboInput.nativeElement.focus();
6✔
492
        }
493
    }
494

495
    /** @hidden @internal */
496
    public override onClick(event: Event): void {
497
        super.onClick(event);
8✔
498
        if (this.comboInput.value.length === 0) {
8✔
499
            this.virtDir.scrollTo(0);
6✔
500
        }
501
    }
502

503
    protected findAllMatches = (element: any): boolean => {
133✔
504
        const value = this.displayKey ? element[this.displayKey] : element;
1,819✔
505
        if (value === null || value === undefined || value === '') {
1,819✔
506
            // we can accept null, undefined and empty strings as empty display values
507
            return true;
13✔
508
        }
509
        const searchValue = this.searchValue || this.comboInput.value;
1,806✔
510
        return !!searchValue && value.toString().toLowerCase().includes(searchValue.toLowerCase());
1,806✔
511
    };
512

513
    protected setSelection(newSelection: any): void {
514
        const newValueAsArray = newSelection ? Array.from(newSelection) as IgxComboItemComponent[] : [];
92✔
515
        const oldValueAsArray = Array.from(this.selectionService.get(this.id) || []);
92!
516
        const newItems = this.convertKeysToItems(newValueAsArray);
92✔
517
        const oldItems = this.convertKeysToItems(oldValueAsArray);
92✔
518
        const displayText = this.createDisplayText(this.convertKeysToItems(newValueAsArray), oldValueAsArray);
92✔
519
        const args: ISimpleComboSelectionChangingEventArgs = {
92✔
520
            newValue: newValueAsArray[0],
521
            oldValue: oldValueAsArray[0],
522
            newSelection: newItems[0],
523
            oldSelection: oldItems[0],
524
            displayText,
525
            owner: this,
526
            cancel: false
527
        };
528
        if (args.newSelection !== args.oldSelection) {
92✔
529
            this.selectionChanging.emit(args);
87✔
530
        }
531
        // TODO: refactor below code as it sets the selection and the display text
532
        if (!args.cancel) {
92✔
533
            let argsSelection = this.isValid(args.newValue)
90✔
534
                ? args.newValue
535
                : [];
536
            argsSelection = Array.isArray(argsSelection) ? argsSelection : [argsSelection];
90✔
537
            this.selectionService.select_items(this.id, argsSelection, true);
90✔
538
            this._value = argsSelection;
90✔
539
            if (this._updateInput) {
90✔
540
                this.comboInput.value = this._displayValue = this.searchValue = displayText !== args.displayText
88!
541
                    ? args.displayText
542
                    : this.createDisplayText(super.selection, [args.oldValue]);
543
            }
544
            this._onChangeCallback(args.newValue);
90✔
545
            this._updateInput = true;
90✔
546
        } else if (this.isRemote) {
2✔
547
            this.registerRemoteEntries(newValueAsArray, false);
1✔
548
        } else {
549
            args.displayText = this.createDisplayText(oldItems, []);
1✔
550

551
            const oldSelectionArray = args.oldSelection ? [args.oldSelection] : [];
1!
552
            this.comboInput.value = this._displayValue = this.searchValue = this.createDisplayText(oldSelectionArray, []);
1✔
553

554
            if (this.isRemote) {
1!
UNCOV
555
                this.registerRemoteEntries(newValueAsArray, false);
×
556
            }
557
        }
558
    }
559

560
    protected createDisplayText(newSelection: any[], oldSelection: any[]): string {
561
        if (this.isRemote) {
329✔
562
            const selection = this.valueKey ? newSelection.map(item => item[this.valueKey]) : newSelection;
31!
563
            return this.getRemoteSelection(selection, oldSelection);
31✔
564
        }
565

566
        if (this.displayKey !== null
298✔
567
            && this.displayKey !== undefined
568
            && newSelection.length > 0) {
569
            return newSelection.filter(e => e).map(e => e[this.displayKey])[0]?.toString() || '';
138✔
570
        }
571

572
        return newSelection[0]?.toString() || '';
162✔
573
    }
574

575
    protected override getRemoteSelection(newSelection: any[], oldSelection: any[]): string {
576
        if (!newSelection.length) {
31✔
577
            this.registerRemoteEntries(oldSelection, false);
6✔
578
            return '';
6✔
579
        }
580

581
        this.registerRemoteEntries(oldSelection, false);
25✔
582
        this.registerRemoteEntries(newSelection);
25✔
583
        return Object.keys(this._remoteSelection).map(e => this._remoteSelection[e])[0] || '';
25✔
584
    }
585

586
    /** Contains key-value pairs of the selected valueKeys and their resp. displayKeys */
587
    protected override registerRemoteEntries(ids: any[], add = true) {
25✔
588
        const selection = this.getValueDisplayPairs(ids)[0];
57✔
589

590
        if (add && selection) {
57✔
591
            this._remoteSelection[selection[this.valueKey]] = selection[this.displayKey].toString();
23✔
592
        } else {
593
            this._remoteSelection = {};
34✔
594
        }
595
    }
596

597
    private clearSelection(ignoreFilter?: boolean): void {
598
        let newSelection = this.selectionService.get_empty();
29✔
599
        if (this.filteredData.length !== this.data.length && !ignoreFilter) {
29✔
600
            newSelection = this.selectionService.delete_items(this.id, this.selectionService.get_all_ids(this.filteredData, this.valueKey));
1✔
601
        }
602
        if (this.selectionService.get(this.id).size > 0 || this.comboInput.value.trim()) {
29✔
603
            this.setSelection(newSelection);
16✔
604
        }
605
    }
606

607
    private clearOnBlur(): void {
608
        if (this.isRemote) {
19✔
609
            const searchValue = this.searchValue || this.comboInput.value;
1!
610
            const remoteValue = Object.keys(this._remoteSelection).map(e => this._remoteSelection[e])[0] || '';
1!
611
            if (searchValue !== remoteValue) {
1!
UNCOV
612
                this.clear();
×
613
            }
614
            return;
1✔
615
        }
616

617
        const filtered = this.filteredData.find(this.findMatch);
18✔
618
        // selecting null in primitive data returns undefined as the search text is '', but the item is null
619
        if (filtered === undefined && this.selectedItem !== null || !super.selection.length) {
18✔
620
            this.clear();
15✔
621
        }
622
    }
623

624
    private getElementVal(element: any): string {
625
        const elementVal = this.displayKey ? element[this.displayKey] : element;
×
UNCOV
626
        return String(elementVal);
×
627
    }
628

629
    private clear(): void {
630
        this.clearSelection(true);
15✔
631
        const oldSelection = this.selection;
15✔
632
        if (this.selection !== oldSelection) {
15!
UNCOV
633
            this.comboInput.value = this._displayValue = this.searchValue = '';
×
634
        }
635
    }
636

637
    private isValid(value: any): boolean {
638
        if (this.formGroupDirective && value === null) {
311✔
639
            return false;
11✔
640
        }
641

642
        if (this.required) {
300✔
643
            return value !== null && value !== '' && value !== undefined
48✔
644
        }
645

646
        return value !== undefined;
252✔
647
    }
648
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc