Skip to content

Commit f6af807

Browse files
committed
feat(searchbar): debounce input events
Closes #5429
1 parent 6b93bc1 commit f6af807

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

ionic/components/searchbar/searchbar.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Ion} from '../ion';
55
import {Config} from '../../config/config';
66
import {Icon} from '../icon/icon';
77
import {Button} from '../button/button';
8-
import {isDefined} from '../../util/util';
8+
import {isDefined, debounce} from '../../util/util';
99

1010

1111
/**
@@ -20,14 +20,12 @@ export class SearchbarInput {
2020
* @private
2121
* Don't send the input's input event
2222
*/
23-
private stopInput(event) {
24-
event.preventDefault();
25-
event.stopPropagation();
23+
private stopInput(ev) {
24+
ev.preventDefault();
25+
ev.stopPropagation();
2626
}
2727

28-
constructor(private _elementRef: ElementRef) {
29-
30-
}
28+
constructor(private _elementRef: ElementRef) {}
3129
}
3230

3331

@@ -69,6 +67,8 @@ export class SearchbarInput {
6967
directives: [FORM_DIRECTIVES, NgIf, NgClass, Icon, Button, SearchbarInput]
7068
})
7169
export class Searchbar extends Ion {
70+
private _tmr: number;
71+
7272
/**
7373
* @private
7474
*/
@@ -84,6 +84,11 @@ export class Searchbar extends Ion {
8484
*/
8585
@Input() hideCancelButton: any;
8686

87+
/**
88+
* @input {number} How long, in milliseconds, to wait to trigger the `input` event after each keystroke. Default `250`.
89+
*/
90+
@Input() debounce: number = 250;
91+
8792
/**
8893
* @input {string} Sets input placeholder to the value passed in
8994
*/
@@ -253,9 +258,14 @@ export class Searchbar extends Ion {
253258
* Update the Searchbar input value when the input changes
254259
*/
255260
inputChanged(ev) {
256-
this.value = ev.target.value;
257-
this.onChange(this.value);
258-
this.input.emit(this);
261+
let value = ev.target.value;
262+
263+
clearTimeout(this._tmr);
264+
this._tmr = setTimeout(() => {
265+
this.value = value;
266+
this.onChange(value);
267+
this.input.emit(this);
268+
}, Math.round(this.debounce));
259269
}
260270

261271
/**

ionic/components/searchbar/test/floating/main.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ion-content>
22
<h5 padding-left> Search - Default </h5>
3-
<ion-searchbar [(ngModel)]="defaultSearch" (input)="triggerInput($event)" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" (cancel)="onCancelSearchbar($event)" (clear)="onClearSearchbar($event)" class="e2eDefaultFloatingSearchbar"></ion-searchbar>
3+
<ion-searchbar [(ngModel)]="defaultSearch" debounce="500" (input)="triggerInput($event)" (blur)="inputBlurred($event)" (focus)="inputFocused($event)" (cancel)="onCancelSearchbar($event)" (clear)="onClearSearchbar($event)" class="e2eDefaultFloatingSearchbar"></ion-searchbar>
44

55
<p padding-left>
66
defaultSearch: <b>{{ defaultSearch }}</b>

0 commit comments

Comments
 (0)