From 696026e24e4d4c827c27700327175608294c58d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Tr=C3=A9bel?= Date: Fri, 18 Nov 2016 14:00:05 +0100 Subject: [PATCH] Fix item selection against manual input --- assets/scripts/src/choices.js | 19 +++++++++++++++++-- tests/spec/choices_spec.js | 4 ++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/assets/scripts/src/choices.js b/assets/scripts/src/choices.js index 693d6bf..5b550d6 100644 --- a/assets/scripts/src/choices.js +++ b/assets/scripts/src/choices.js @@ -411,7 +411,6 @@ class Choices { // If we actually have anything to add to our dropdown // append it and highlight the first choice this.choiceList.appendChild(choiceListFragment); - this._highlightChoice(); } else { const activeItems = this.store.getItemsFilteredByActive(); const canAddItem = this._canAddItem(activeItems, this.input.value); @@ -1032,9 +1031,9 @@ class Choices { if (canAddItem.response) { this._addItem(choice.value, choice.label, choice.id, choice.groupId); - this._triggerChange(choice.value); } } + this._triggerChange(choice.value); this.clearInput(this.passedElement); @@ -1357,6 +1356,22 @@ class Choices { }; const onEnterKey = () => { + + if (hasActiveDropdown) { + const highlighted = this.dropdown.querySelector(`.${this.config.classNames.highlightedState}`); + + // If we have a highlighted choice + if (highlighted) { + this._handleChoiceAction(activeItems, highlighted); + } + } else if (passedElementType === 'select-one') { + // Open single select dropdown if it's not active + if (!hasActiveDropdown) { + this.showDropdown(true); + e.preventDefault(); + } + } + // If enter key is pressed and the input has a value if (target.value) { const value = this.input.value; diff --git a/tests/spec/choices_spec.js b/tests/spec/choices_spec.js index 1671dc6..0b96342 100644 --- a/tests/spec/choices_spec.js +++ b/tests/spec/choices_spec.js @@ -284,7 +284,7 @@ describe('Choices', () => { this.choices = new Choices(this.input); this.choices.input.focus(); - for (var i = 0; i < 2; i++) { + for (var i = 0; i < 3; i++) { // Key down to third choice this.choices._onKeyDown({ target: this.choices.input, @@ -317,7 +317,7 @@ describe('Choices', () => { preventDefault: () => {} }); - expect(this.choices.currentState.items.length).toBe(2); + expect(this.choices.currentState.items.length).toBe(1); }); it('should trigger add/change event on selection', function() {