Skip to content

Commit

Permalink
fix(combo): make onSearchInput cancellable #7282
Browse files Browse the repository at this point in the history
  • Loading branch information
PlamenaMiteva committed Jul 9, 2020
1 parent 5053978 commit f7884d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
25 changes: 23 additions & 2 deletions projects/igniteui-angular/src/lib/combo/combo.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { configureTestSuite } from '../test-utils/configure-suite';
import { DisplayDensity } from '../core/density';
import { AbsoluteScrollStrategy, ConnectedPositioningStrategy } from '../services/public_api';
import { IgxSelectionAPIService } from '../core/selection';
import { CancelableEventArgs } from '../core/utils';

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

const args = {
change: 'Fake',
cancel: false
};
combo.handleInputChange('Fake');
expect(matchSpy).toHaveBeenCalledTimes(2);
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(1);
expect(combo.onSearchInput.emit).toHaveBeenCalledWith('Fake');
expect(combo.onSearchInput.emit).toHaveBeenCalledWith(args);

args.change = '';
combo.handleInputChange('');
expect(matchSpy).toHaveBeenCalledTimes(3);
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(2);
expect(combo.onSearchInput.emit).toHaveBeenCalledWith('');
expect(combo.onSearchInput.emit).toHaveBeenCalledWith(args);

combo.filterable = false;
combo.handleInputChange();
expect(matchSpy).toHaveBeenCalledTimes(4);
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(2);
});
it('should be able to cancel onSearchInput', () => {
combo = new IgxComboComponent({ nativeElement: null }, mockCdr, mockSelection as any, mockComboService, null, mockInjector);
combo.ngOnInit();
combo.data = data;
combo.filterable = true;
combo.onSearchInput.subscribe((e) => {
e.cancel = true;
});
const matchSpy = spyOn<any>(combo, 'checkMatch').and.callThrough();
spyOn(combo.onSearchInput, 'emit').and.callThrough();

combo.handleInputChange('Item1');
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(1);
expect(matchSpy).toHaveBeenCalledTimes(0);
});
});
describe('Initialization and rendering tests: ', () => {
configureTestSuite();
Expand Down
22 changes: 18 additions & 4 deletions projects/igniteui-angular/src/lib/combo/combo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export interface IComboSelectionChangeEventArgs extends CancelableEventArgs, IBa
event?: Event;
}

/** Event emitted when the igx-combo's search input changes */
export interface IComboSearchInputEventArgs extends CancelableEventArgs {
/** The change that has been made to the search input */
change: string;
}

export interface IComboItemAdditionEvent extends IBaseEventArgs {
oldCollection: any[];
addedItem: any;
Expand Down Expand Up @@ -483,7 +489,7 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
* ```
*/
@Output()
public onSearchInput = new EventEmitter();
public onSearchInput = new EventEmitter<IComboSearchInputEventArgs>();

/**
* Emitted when new chunk of data is loaded from the virtualization
Expand Down Expand Up @@ -981,7 +987,15 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
*/
public handleInputChange(event?: string) {
if (event !== undefined) {
this.onSearchInput.emit(event);
const args: IComboSearchInputEventArgs = {
change: event,
cancel: false
};
this.onSearchInput.emit(args);
if (args.cancel) {
this.searchValue = null;
return;
}
}
this.checkMatch();
}
Expand Down Expand Up @@ -1436,8 +1450,8 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
/** Returns a string that should be populated in the combo's text box */
private concatDisplayText(selection: any[]): string {
const value = this.displayKey !== null && this.displayKey !== undefined ?
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
selection.join(', ');
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
selection.join(', ');
return value;
}

Expand Down

0 comments on commit f7884d3

Please sign in to comment.