Skip to content

Commit 8e4eff7

Browse files
committed
feat(clearOnSelected): allow users to clear the input instead of filling
In some use-cases, like when you're using autocomplete to make tags on a page, or using the output in any other way than prefilling the query, you don't want the suggested value to show in the input, but would rather like it to be empty. Before this PR it was basically impossible to have your input not filled, since it came back on blur and a few different places (see `resetInputValue` fixes #241
1 parent c194736 commit 8e4eff7

File tree

7 files changed

+33
-3
lines changed

7 files changed

+33
-3
lines changed

README.md

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

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

293+
* `clearOnSelected` – If `true`, the autocomplete will empty the search box when a suggestion is selected. This is useful if you want to use this as a way to input tags using the `selected` event.
294+
293295
* `openOnFocus` – If `true`, the dropdown menu will open when the input is focused. Defaults to `false`.
294296

295297
* `appendTo` – If set with a DOM selector, doesn't wrap the input and appends the wrapper and dropdown menu to the first DOM element matching the selector. It automatically positions the wrapper under the input, and sets it to the same width as the input. Can't be used with `hint: true`, because `hint` requires the wrapper around the input.

examples/basic.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h3>Basic example</h3>
5151
autocomplete('#search-input', { hint: false }, [
5252
{
5353
source: autocomplete.sources.hits(index, { hitsPerPage: 5 }),
54-
displayKey: 'my_attribute',
54+
displayKey: 'name',
5555
templates: {
5656
suggestion: function(suggestion) {
5757
return suggestion._highlightResult.name.value;

src/angular/directive.js

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ angular.module('algolia.autocomplete', [])
7575
openOnFocus: scope.options.openOnFocus,
7676
templates: scope.options.templates,
7777
debug: scope.options.debug,
78+
clearOnSelected: scope.options.clearOnSelected,
7879
cssClasses: scope.options.cssClasses,
7980
datasets: scope.datasets,
8081
keyboardShortcuts: scope.options.keyboardShortcuts,

src/autocomplete/typeahead.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function Typeahead(o) {
3131
this.openOnFocus = !!o.openOnFocus;
3232
this.minLength = _.isNumber(o.minLength) ? o.minLength : 1;
3333
this.autoWidth = (o.autoWidth === undefined) ? true : !!o.autoWidth;
34+
this.clearOnSelected = !!o.clearOnSelected;
3435

3536
o.hint = !!o.hint;
3637

@@ -417,7 +418,11 @@ _.mixin(Typeahead.prototype, {
417418
if (typeof datum.value !== 'undefined') {
418419
this.input.setQuery(datum.value);
419420
}
420-
this.input.setInputValue(datum.value, true);
421+
if (this.clearOnSelected) {
422+
this.input.setInputValue('', true);
423+
} else {
424+
this.input.setInputValue(datum.value, true);
425+
}
421426

422427
this._setLanguageDirection();
423428

src/jquery/plugin.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ methods = {
6060
openOnFocus: o.openOnFocus,
6161
templates: o.templates,
6262
debug: o.debug,
63+
clearOnSelected: o.clearOnSelected,
6364
cssClasses: o.cssClasses,
6465
datasets: datasets,
6566
keyboardShortcuts: o.keyboardShortcuts,

src/standalone/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function autocomplete(selector, options, datasets, typeaheadObject) {
4545
openOnFocus: options.openOnFocus,
4646
templates: options.templates,
4747
debug: options.debug,
48+
clearOnSelected: options.clearOnSelected,
4849
cssClasses: options.cssClasses,
4950
datasets: datasets,
5051
keyboardShortcuts: options.keyboardShortcuts,

test/unit/typeahead_spec.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ describe('Typeahead', function() {
342342
});
343343

344344
describe('when debug flag is set', function() {
345-
346345
beforeEach(function() {
347346
this.view = new Typeahead({
348347
input: this.$input,
@@ -368,6 +367,27 @@ describe('Typeahead', function() {
368367
});
369368
});
370369

370+
describe('when clearOnSelected flag is set to true', function() {
371+
it('clears input when selected', function() {
372+
var spy = jasmine.createSpy();
373+
var view = new Typeahead({
374+
input: this.$input,
375+
clearOnSelected: true,
376+
hint: true,
377+
datasets: {}
378+
});
379+
view.dropdown.getDatumForCursor.and.returnValue(testDatum);
380+
381+
382+
var $e = jasmine.createSpyObj('event', ['preventDefault']);
383+
view.$input.on('autocomplete:selected', spy);
384+
view.input.trigger('enterKeyed', $e);
385+
386+
expect(spy).toHaveBeenCalled();
387+
expect(view.input.setInputValue).toHaveBeenCalledWith('', true);
388+
});
389+
});
390+
371391
describe('when input triggers enterKeyed', function() {
372392
beforeEach(function() {
373393
this.dropdown.getDatumForCursor.and.returnValue(testDatum);

0 commit comments

Comments
 (0)