Skip to content

Commit f7884d3

Browse files
committed
fix(combo): make onSearchInput cancellable #7282
1 parent 5053978 commit f7884d3

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

projects/igniteui-angular/src/lib/combo/combo.component.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { configureTestSuite } from '../test-utils/configure-suite';
1818
import { DisplayDensity } from '../core/density';
1919
import { AbsoluteScrollStrategy, ConnectedPositioningStrategy } from '../services/public_api';
2020
import { IgxSelectionAPIService } from '../core/selection';
21+
import { CancelableEventArgs } from '../core/utils';
2122

2223
const CSS_CLASS_COMBO = 'igx-combo';
2324
const CSS_CLASS_COMBO_DROPDOWN = 'igx-combo__drop-down';
@@ -530,21 +531,41 @@ describe('igxCombo', () => {
530531
expect(matchSpy).toHaveBeenCalledTimes(1);
531532
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(0);
532533

534+
const args = {
535+
change: 'Fake',
536+
cancel: false
537+
};
533538
combo.handleInputChange('Fake');
534539
expect(matchSpy).toHaveBeenCalledTimes(2);
535540
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(1);
536-
expect(combo.onSearchInput.emit).toHaveBeenCalledWith('Fake');
541+
expect(combo.onSearchInput.emit).toHaveBeenCalledWith(args);
537542

543+
args.change = '';
538544
combo.handleInputChange('');
539545
expect(matchSpy).toHaveBeenCalledTimes(3);
540546
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(2);
541-
expect(combo.onSearchInput.emit).toHaveBeenCalledWith('');
547+
expect(combo.onSearchInput.emit).toHaveBeenCalledWith(args);
542548

543549
combo.filterable = false;
544550
combo.handleInputChange();
545551
expect(matchSpy).toHaveBeenCalledTimes(4);
546552
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(2);
547553
});
554+
it('should be able to cancel onSearchInput', () => {
555+
combo = new IgxComboComponent({ nativeElement: null }, mockCdr, mockSelection as any, mockComboService, null, mockInjector);
556+
combo.ngOnInit();
557+
combo.data = data;
558+
combo.filterable = true;
559+
combo.onSearchInput.subscribe((e) => {
560+
e.cancel = true;
561+
});
562+
const matchSpy = spyOn<any>(combo, 'checkMatch').and.callThrough();
563+
spyOn(combo.onSearchInput, 'emit').and.callThrough();
564+
565+
combo.handleInputChange('Item1');
566+
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(1);
567+
expect(matchSpy).toHaveBeenCalledTimes(0);
568+
});
548569
});
549570
describe('Initialization and rendering tests: ', () => {
550571
configureTestSuite();

projects/igniteui-angular/src/lib/combo/combo.component.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ export interface IComboSelectionChangeEventArgs extends CancelableEventArgs, IBa
9696
event?: Event;
9797
}
9898

99+
/** Event emitted when the igx-combo's search input changes */
100+
export interface IComboSearchInputEventArgs extends CancelableEventArgs {
101+
/** The change that has been made to the search input */
102+
change: string;
103+
}
104+
99105
export interface IComboItemAdditionEvent extends IBaseEventArgs {
100106
oldCollection: any[];
101107
addedItem: any;
@@ -483,7 +489,7 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
483489
* ```
484490
*/
485491
@Output()
486-
public onSearchInput = new EventEmitter();
492+
public onSearchInput = new EventEmitter<IComboSearchInputEventArgs>();
487493

488494
/**
489495
* Emitted when new chunk of data is loaded from the virtualization
@@ -981,7 +987,15 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
981987
*/
982988
public handleInputChange(event?: string) {
983989
if (event !== undefined) {
984-
this.onSearchInput.emit(event);
990+
const args: IComboSearchInputEventArgs = {
991+
change: event,
992+
cancel: false
993+
};
994+
this.onSearchInput.emit(args);
995+
if (args.cancel) {
996+
this.searchValue = null;
997+
return;
998+
}
985999
}
9861000
this.checkMatch();
9871001
}
@@ -1436,8 +1450,8 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
14361450
/** Returns a string that should be populated in the combo's text box */
14371451
private concatDisplayText(selection: any[]): string {
14381452
const value = this.displayKey !== null && this.displayKey !== undefined ?
1439-
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1440-
selection.join(', ');
1453+
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1454+
selection.join(', ');
14411455
return value;
14421456
}
14431457

0 commit comments

Comments
 (0)