Skip to content

Commit

Permalink
fix: allow changing value in value-changed listener
Browse files Browse the repository at this point in the history
fix #651

The else-if added at line 886 prevents duplicate events.
  • Loading branch information
pekam committed May 17, 2019
1 parent ae80779 commit 10432a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/vaadin-combo-box-mixin.html
Expand Up @@ -707,6 +707,11 @@
const value = this._getItemValue(selectedItem);
if (this.value !== value) {
this.value = value;
if (this.value !== value) {
// The value was changed to something else in value-changed listener,
// so prevent from resetting it to the previous value.
return;
}
}

this._updateHasValue(true);
Expand Down Expand Up @@ -878,9 +883,7 @@
if (this.opened) {
this._focusedIndex = this.filteredItems.indexOf(e.detail.item);
this.close();
}

if (this.selectedItem !== e.detail.item) {
} else if (this.selectedItem !== e.detail.item) {
this.selectedItem = e.detail.item;
this._detectAndDispatchChange();
}
Expand Down
38 changes: 38 additions & 0 deletions test/selecting-items.html
Expand Up @@ -174,6 +174,44 @@
expect(newComboBox.value).to.equal('foo');
});

it('should allow changing the value in value-changed listener', done => {
combobox.open();
const items = combobox.$.overlay._selector.querySelectorAll('vaadin-combo-box-item');

combobox.addEventListener('value-changed', () => {
if (combobox.value === 'foo') {
combobox.value = 'bar';
setTimeout(() => {
expect(combobox.value).to.eql('bar');
expect(combobox.selectedItem).to.eql('bar');
expect(items[0].selected).to.be.false;
expect(items[1].selected).to.be.true;
done();
});
}
});
items[0].click();
});

it('should allow clearing the value in value-changed listener', done => {
combobox.open();
const item = combobox.$.overlay._selector.querySelector('vaadin-combo-box-item');

combobox.addEventListener('value-changed', () => {
if (combobox.value) {
combobox.value = '';
setTimeout(() => {
expect(combobox.value).to.eql('');
expect(combobox.selectedItem).not.to.be.ok;
expect(item.selected).to.be.false;
expect(combobox.hasAttribute('has-value')).to.be.false;
done();
});
}
});
item.click();
});

describe('`change` event', () => {
it('should fire `value-changed` and `selected-item-changed` before `changed`', done => {
combobox.addEventListener('change', () => {
Expand Down

0 comments on commit 10432a7

Please sign in to comment.