Skip to content

Commit 7c82a75

Browse files
authored
Merge pull request #7766 from IgniteUI/PMiteva/cancellable_onSearchInput_10.1
Make onSearchInput cancellable
2 parents 4d2ca73 + e0b15ac commit 7c82a75

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes for each version of this project will be documented in this
77
### General
88
- `igxCombo`
99
- **Behavioral Change** - Change default positioning strategy from `ConnectedPositioningStrategy` to `AutoPositionStrategy`. The [`Auto`](https://www.infragistics.com/products/ignite-ui-angular/angular/components/overlay_position.html#auto) strategy will initially try to show the element like the Connected strategy does. If the element goes out of the viewport Auto will flip the starting point and the direction, i.e. if the direction is 'bottom', it will switch it to 'top' and so on. If after flipping direction the content goes out of the view, auto strategy will revert to initial start point and direction and will push the content into the view. Note after pushing the content it may hide the combo's input.
10+
- 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.
1011
- `IgxOverlay`
1112
- Added new property `closeOnEscape` in `OverlaySettings` that controls whether the overlay should close on escape keypress. By default `closeOnEsc` is set to `false`.
1213
- **Behavioral Change** - `modal` overlays shown directly through the Overlay Service no longer close on Escape by default. That behavior can now be specified using the `closeOnEscape` property.

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

Lines changed: 24 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,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

Lines changed: 19 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, IBaseEventArgs {
101+
/** The text that has been typed into the search input */
102+
searchText: 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,16 @@ 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+
searchText: event,
992+
owner: this,
993+
cancel: false
994+
};
995+
this.onSearchInput.emit(args);
996+
if (args.cancel) {
997+
this.searchValue = null;
998+
return;
999+
}
9851000
}
9861001
this.checkMatch();
9871002
}
@@ -1436,8 +1451,8 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
14361451
/** Returns a string that should be populated in the combo's text box */
14371452
private concatDisplayText(selection: any[]): string {
14381453
const value = this.displayKey !== null && this.displayKey !== undefined ?
1439-
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1440-
selection.join(', ');
1454+
this.convertKeysToItems(selection).map(entry => entry[this.displayKey]).join(', ') :
1455+
selection.join(', ');
14411456
return value;
14421457
}
14431458

0 commit comments

Comments
 (0)