Skip to content

Commit 4dc7c52

Browse files
cdtinneyHaroenv
authored andcommitted
feat: add flag for toggling tab autocompletion (#260)
* feat(typeahead): add flag to disable tab autocomplete * feat: close dropdown on tab autocompletion
1 parent 179febf commit 4dc7c52

File tree

6 files changed

+42
-1
lines changed

6 files changed

+42
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ When initializing an autocomplete, there are a number of options you can configu
283283

284284
* `autoselectOnBlur` – If `true`, when the input is blurred, the first rendered suggestion in the dropdown will automatically have the `cursor` class, and pressing `<ENTER>` will select it. This option should be used on mobile, see [#113](https://github.com/algolia/autocomplete.js/issues/113)
285285

286+
* `tabAutocomplete` – If `true`, pressing tab will select the first rendered suggestion in the dropdown. Defaults to `true`.
287+
286288
* `hint` – If `false`, the autocomplete will not show a hint. Defaults to `true`.
287289

288290
* `debug` – If `true`, the autocomplete will not close on `blur`. Defaults to `false`.

src/angular/directive.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ angular.module('algolia.autocomplete', [])
7272
minLength: scope.options.minLength,
7373
autoselect: scope.options.autoselect,
7474
autoselectOnBlur: scope.options.autoselectOnBlur,
75+
tabAutocomplete: scope.options.tabAutocomplete,
7576
openOnFocus: scope.options.openOnFocus,
7677
templates: scope.options.templates,
7778
debug: scope.options.debug,

src/autocomplete/typeahead.js

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function Typeahead(o) {
3232
this.minLength = _.isNumber(o.minLength) ? o.minLength : 1;
3333
this.autoWidth = (o.autoWidth === undefined) ? true : !!o.autoWidth;
3434
this.clearOnSelected = !!o.clearOnSelected;
35+
this.tabAutocomplete = (o.tabAutocomplete === undefined) ? true : !!o.tabAutocomplete;
3536

3637
o.hint = !!o.hint;
3738

@@ -285,6 +286,12 @@ _.mixin(Typeahead.prototype, {
285286
},
286287

287288
_onTabKeyed: function onTabKeyed(type, $e) {
289+
if (!this.tabAutocomplete) {
290+
// Closing the dropdown enables further tabbing
291+
this.dropdown.close();
292+
return;
293+
}
294+
288295
var datum;
289296

290297
if (datum = this.dropdown.getDatumForCursor()) {

src/jquery/plugin.js

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ methods = {
5757
minLength: o.minLength,
5858
autoselect: o.autoselect,
5959
autoselectOnBlur: o.autoselectOnBlur,
60+
tabAutocomplete: o.tabAutocomplete,
6061
openOnFocus: o.openOnFocus,
6162
templates: o.templates,
6263
debug: o.debug,

src/standalone/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ function autocomplete(selector, options, datasets, typeaheadObject) {
4242
minLength: options.minLength,
4343
autoselect: options.autoselect,
4444
autoselectOnBlur: options.autoselectOnBlur,
45+
tabAutocomplete: options.tabAutocomplete,
4546
openOnFocus: options.openOnFocus,
4647
templates: options.templates,
4748
debug: options.debug,

test/unit/typeahead_spec.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ describe('Typeahead', function() {
464464
});
465465

466466
describe('when cursor is not in use', function() {
467-
it('should autocomplete', function() {
467+
it('should autocomplete if tabAutocomplete is true', function() {
468468
var spy;
469469

470470
this.input.getQuery.and.returnValue('bi');
@@ -478,6 +478,35 @@ describe('Typeahead', function() {
478478
expect(this.input.setInputValue).toHaveBeenCalledWith(testDatum.value);
479479
expect(spy).toHaveBeenCalled();
480480
});
481+
482+
it('should not autocomplete if tabAutocomplete is false', function() {
483+
this.view.tabAutocomplete = false;
484+
485+
var spy;
486+
487+
this.input.getQuery.and.returnValue('bi');
488+
this.input.getHint.and.returnValue(testDatum.value);
489+
this.input.isCursorAtEnd.and.returnValue(true);
490+
this.dropdown.getDatumForTopSuggestion.and.returnValue(testDatum);
491+
this.$input.on('autocomplete:autocompleted', spy = jasmine.createSpy());
492+
493+
this.input.trigger('tabKeyed');
494+
495+
expect(this.input.setInputValue).not.toHaveBeenCalledWith(testDatum.value);
496+
expect(spy).not.toHaveBeenCalled();
497+
});
498+
499+
it('should close the dropdown if tabAutocomplete is false', function() {
500+
this.view.tabAutocomplete = false;
501+
502+
this.input.getQuery.and.returnValue('bi');
503+
this.input.getHint.and.returnValue(testDatum.value);
504+
this.input.isCursorAtEnd.and.returnValue(true);
505+
506+
this.input.trigger('tabKeyed');
507+
508+
expect(this.dropdown.close).toHaveBeenCalled();
509+
});
481510
});
482511
});
483512

0 commit comments

Comments
 (0)