Skip to content

Commit ce2db57

Browse files
authored
Merge pull request #7767 from IgniteUI/PMiteva/cancellable_onSearchInput_10.0
Make onSearchInput cancellable v.10.0
2 parents c3d9374 + 6cc5c95 commit ce2db57

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ All notable changes for each version of this project will be documented in this
204204

205205
- `IgxCombo`:
206206
- Added `autoFocusSearch` input that allows to manipulate the combo's opening behavior. When the property is `true` (by default), the combo's search input is focused on open. When set to `false`, the focus goes to the combo items container, which can be used to prevent the software keyboard from activating on mobile devices when opening the combo.
207+
- Make `onSearchInput` event cancellable. The event args type has been changed to `IComboSearchInputEventArgs`, which have the following properties: `searchText` - holds the text typed into the search input, `owner` - holds a reference to the combo component and `cancel` - indicates whether the event should be canceled.
207208

208209
- `IgxToast`:
209210
- Added functionality for displaying various content into the toast component. It also allows users to access toast styles through its host element.

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

+24-2
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,42 @@ describe('igxCombo', () => {
530531
expect(matchSpy).toHaveBeenCalledTimes(1);
531532
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(0);
532533

534+
const args = {
535+
searchText: 'Fake',
536+
owner: combo,
537+
cancel: false
538+
};
533539
combo.handleInputChange('Fake');
534540
expect(matchSpy).toHaveBeenCalledTimes(2);
535541
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(1);
536-
expect(combo.onSearchInput.emit).toHaveBeenCalledWith('Fake');
542+
expect(combo.onSearchInput.emit).toHaveBeenCalledWith(args);
537543

544+
args.searchText = '';
538545
combo.handleInputChange('');
539546
expect(matchSpy).toHaveBeenCalledTimes(3);
540547
expect(combo.onSearchInput.emit).toHaveBeenCalledTimes(2);
541-
expect(combo.onSearchInput.emit).toHaveBeenCalledWith('');
548+
expect(combo.onSearchInput.emit).toHaveBeenCalledWith(args);
542549

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

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ export interface IComboSelectionChangeEventArgs extends CancelableEventArgs, IBa
9797
event?: Event;
9898
}
9999

100+
/** Event emitted when the igx-combo's search input changes */
101+
export interface IComboSearchInputEventArgs extends CancelableEventArgs, IBaseEventArgs {
102+
/** The text that has been typed into the search input */
103+
searchText: string;
104+
}
105+
100106
export interface IComboItemAdditionEvent extends IBaseEventArgs {
101107
oldCollection: any[];
102108
addedItem: any;
@@ -484,7 +490,7 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
484490
* ```
485491
*/
486492
@Output()
487-
public onSearchInput = new EventEmitter();
493+
public onSearchInput = new EventEmitter<IComboSearchInputEventArgs>();
488494

489495
/**
490496
* Emitted when new chunk of data is loaded from the virtualization
@@ -982,7 +988,16 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
982988
*/
983989
public handleInputChange(event?: string) {
984990
if (event !== undefined) {
985-
this.onSearchInput.emit(event);
991+
const args: IComboSearchInputEventArgs = {
992+
searchText: event,
993+
owner: this,
994+
cancel: false
995+
};
996+
this.onSearchInput.emit(args);
997+
if (args.cancel) {
998+
this.searchValue = null;
999+
return;
1000+
}
9861001
}
9871002
this.checkMatch();
9881003
}
@@ -1437,8 +1452,8 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
14371452
/** Returns a string that should be populated in the combo's text box */
14381453
private concatDisplayText(selection: any[]): string {
14391454
const value = this.displayKey !== null && this.displayKey !== undefined ?
1440-
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1441-
selection.join(', ');
1455+
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1456+
selection.join(', ');
14421457
return value;
14431458
}
14441459

0 commit comments

Comments
 (0)