Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit a63e905

Browse files
author
Sam Graber
authored
Merge pull request #405 from SamGraber/debounce
Allow the consumer to override the default debounce
2 parents e2a51f0 + ae6f7c9 commit a63e905

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

bootstrapper/inputs/inputsNg2.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ <h3><a href="https://github.com/SamGraber/TypeScript-Angular-Components/blob/mas
4444
<template let-option>Value - {{option.value}}</template>
4545
</rlTypeahead>
4646
<rlTypeahead [(value)]="typeaheadSelection" prefix="Search for a" label="Typeahead with create" [getItems]="getOptions" clientSearch="true" transform="value" [create]="createOption" allowCollapse="true"></rlTypeahead>
47+
<rlTypeahead [(value)]="typeaheadSelection" prefix="Search for a" label="Typeahead long debounce" [getItems]="searchOptions" transform="value" allowCollapse="true" [debounce]="10000"></rlTypeahead>
4748
</div>
4849
<div>
4950
<rlTypeaheadList [(value)]="selections" label="Typeahead list" [getItems]="getOptions" clientSearch="true" transform="value"></rlTypeaheadList>

source/components/inputs/typeahead/typeahead.tests.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import __search = services.search;
1010

1111
import { ComponentValidator } from '../../../services/componentValidator/componentValidator.service';
1212

13-
import { TypeaheadComponent, DEFAULT_SERVER_SEARCH_DEBOUNCE } from './typeahead';
13+
import { TypeaheadComponent, DEFAULT_SERVER_SEARCH_DEBOUNCE, DEFAULT_CLIENT_SEARCH_DEBOUNCE } from './typeahead';
1414

1515
interface ITransformMock {
1616
getValue: sinon.SinonSpy;
@@ -69,6 +69,38 @@ describe('TypeaheadComponent', () => {
6969
expect(typeahead.collapsed).to.be.true;
7070
});
7171

72+
describe('debounce', () => {
73+
it('should use the default server search debounce by default', () => {
74+
typeahead.ngOnInit();
75+
expect(typeahead.loadDelay).to.equal(DEFAULT_SERVER_SEARCH_DEBOUNCE);
76+
});
77+
78+
it('should use the default client search debounce if client searching is on', () => {
79+
typeahead.clientSearch = true;
80+
typeahead.ngOnInit();
81+
expect(typeahead.loadDelay).to.equal(DEFAULT_CLIENT_SEARCH_DEBOUNCE);
82+
});
83+
84+
it('should use the custom debounce when client searching is off', () => {
85+
const debounce = 10000;
86+
typeahead.debounce = debounce;
87+
88+
typeahead.ngOnInit();
89+
90+
expect(typeahead.loadDelay).to.equal(debounce);
91+
});
92+
93+
it('should use the custom debounce when client searching is on', () => {
94+
const debounce = 10000;
95+
typeahead.debounce = debounce;
96+
97+
typeahead.clientSearch = true;
98+
typeahead.ngOnInit();
99+
100+
expect(typeahead.loadDelay).to.equal(debounce);
101+
});
102+
});
103+
72104
describe('loadItems', (): void => {
73105
let items: string[];
74106

source/components/inputs/typeahead/typeahead.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class TypeaheadComponent<T> extends ValidatedInputComponent<T> implements
4141
@Input() allowCollapse: boolean;
4242
@Input() create: { (value: string): T };
4343
@Input() caseSensitiveSearching: boolean;
44+
@Input() debounce: number;
4445
@Output() selector: EventEmitter<T> = new EventEmitter<T>();
4546

4647

@@ -158,7 +159,12 @@ export class TypeaheadComponent<T> extends ValidatedInputComponent<T> implements
158159
ngOnInit(): void {
159160
super.ngOnInit();
160161

161-
this.loadDelay = this.clientSearch ? DEFAULT_CLIENT_SEARCH_DEBOUNCE : DEFAULT_SERVER_SEARCH_DEBOUNCE;
162+
if (this.debounce) {
163+
this.loadDelay = this.debounce;
164+
} else {
165+
this.loadDelay = this.clientSearch ? DEFAULT_CLIENT_SEARCH_DEBOUNCE : DEFAULT_SERVER_SEARCH_DEBOUNCE;
166+
}
167+
162168
this.prefix = this.prefix || 'Search for';
163169
this.placeholder = this.label != null ? this.prefix + ' ' + this.label.toLowerCase() : 'Search';
164170

0 commit comments

Comments
 (0)