Fix item selection against manual input

This commit is contained in:
Stéphane Trébel 2016-11-18 14:00:05 +01:00 committed by Josh Johnson
parent 217015caef
commit 696026e24e
2 changed files with 19 additions and 4 deletions

View file

@ -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;

View file

@ -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() {