Skip to content

Commit

Permalink
fix: ensure no multiple hasInputValue changes on clear button click (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mukherjeesudebi authored May 17, 2023
1 parent e33fbdc commit 38a5a13
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/vaadin-combo-box-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,9 @@
_inputValueChanged(e) {
// Handle only input events from our inputElement.
if (e.composedPath().indexOf(this.inputElement) !== -1) {
this._inputElementValue = this.inputElement[this._propertyForValue];
if (!e.__fromClearButton) {
this._inputElementValue = this.inputElement[this._propertyForValue];
}
this._filterFromInput(e);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vaadin-combo-box.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
pattern="[[pattern]]"
prevent-invalid-input="[[preventInvalidInput]]"

value="{{_inputElementValue}}"
value="[[_inputElementValue]]"
autocomplete="off"

invalid="[[invalid]]"
Expand Down
32 changes: 28 additions & 4 deletions test/events.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@

it('should not fire the event when modifying input', async() => {
input.value = 'foobar';
input.dispatchEvent(new CustomEvent('input'));
input.dispatchEvent(new CustomEvent('input', {bubbles: true, composed: true}));
expect(hasInputValueChangedSpy.called).to.be.false;
});

it('should fire the event once when removing input', async() => {
input.value = '';
input.dispatchEvent(new CustomEvent('input'));
input.dispatchEvent(new CustomEvent('input', {bubbles: true, composed: true}));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
Expand All @@ -104,7 +104,7 @@
describe('without user input', () => {
it('should fire the event once when entering input', async() => {
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
input.dispatchEvent(new CustomEvent('input', {bubbles: true, composed: true}));
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
Expand All @@ -116,7 +116,7 @@
});
});

describe('with value', () => {
describe('with custom value', () => {
beforeEach(async() => {
comboBox.clearButtonVisible = true;
clearButton = comboBox.inputElement.$.clearButton;
Expand All @@ -143,6 +143,30 @@
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});

describe('with dropdown value', () => {
beforeEach(async() => {
comboBox.items = ['foo', 'bar', 'baz', 'qux'];
comboBox.clearButtonVisible = true;
clearButton = comboBox.inputElement.$.clearButton;
// Open the combo-box
arrowDown(input);
// select the first option
arrowDown(input);
enter(input);
valueChangedSpy.reset();
hasInputValueChangedSpy.reset();
});

it('should be fired on clear button click', () => {
expect(input.value).to.equal('foo');
clearButton.click();
expect(input.value).to.be.empty;
expect(valueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
</script>
Expand Down
17 changes: 1 addition & 16 deletions test/lazy-loading.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
comboBox.inputElement.inputElement.value = value;
} else {
comboBox.inputElement.value = value;
comboBox.inputElement.dispatchEvent(new CustomEvent('input'));
}
};

Expand Down Expand Up @@ -984,22 +985,6 @@
expect(comboBox.value).to.equal('other value');
});

it('should keep previous value if allow-custom-value is set', () => {
comboBox.allowCustomValue = true;
comboBox.open();
setInputValue('other value');
comboBox.inputElement.dispatchEvent(new CustomEvent('input'));
comboBox.close();
expect(comboBox.value).to.eql('other value');

comboBox.focus();
setInputValue('item 12');
comboBox.filter = 'item 12';

expect(comboBox.value).to.eql('other value');
expect(comboBox.inputElement.value).to.eql('other value');
});

});

describe('after empty data set loaded', () => {
Expand Down
1 change: 1 addition & 0 deletions test/using-object-values.html
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@

combobox.readonly = true;
combobox.inputElement.value = 'not found';
combobox.inputElement.dispatchEvent(new CustomEvent('input'));
combobox.focus();
fire('focusout', combobox.inputElement);

Expand Down
26 changes: 25 additions & 1 deletion test/vaadin-combo-box-light-events.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
});
});

describe('with value', () => {
describe('with custom value', () => {
beforeEach(async() => {
comboBox.clearButtonVisible = true;
clearButton = comboBox.inputElement.$.clearButton;
Expand All @@ -145,6 +145,30 @@
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});

describe('with dropdown value', () => {
beforeEach(async() => {
comboBox.items = ['foo', 'bar', 'baz', 'qux'];
comboBox.clearButtonVisible = true;
clearButton = comboBox.inputElement.$.clearButton;
// Open the combo-box
arrowDown(input);
// select the first option
arrowDown(input);
enter(input);
valueChangedSpy.reset();
hasInputValueChangedSpy.reset();
});

it('should be fired on clear button click', () => {
expect(input.value).to.equal('foo');
clearButton.click();
expect(input.value).to.be.empty;
expect(valueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledOnce).to.be.true;
expect(hasInputValueChangedSpy.calledBefore(valueChangedSpy)).to.be.true;
});
});
});
});
</script>
Expand Down
4 changes: 4 additions & 0 deletions test/vaadin-combo-box.html
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@

comboBox.open();
comboBox.inputElement.value = 'a';
comboBox.inputElement.dispatchEvent(new CustomEvent('input'));
comboBox.$.overlay._selector.querySelector('vaadin-combo-box-item').click();
expect(spy).to.not.have.been.called;
});
Expand All @@ -282,6 +283,7 @@

comboBox.open();
comboBox.inputElement.value = 'a';
comboBox.inputElement.dispatchEvent(new CustomEvent('input'));
comboBox.close();
comboBox.inputElement.blur();
expect(spy).to.not.have.been.called;
Expand All @@ -297,6 +299,7 @@

comboBox.open();
comboBox.inputElement.value = 'foo';
comboBox.inputElement.dispatchEvent(new CustomEvent('input'));
comboBox.close();

expect(spy.called).to.be.false;
Expand All @@ -312,6 +315,7 @@

comboBox.open();
comboBox.inputElement.value = 'bar';
comboBox.inputElement.dispatchEvent(new CustomEvent('input'));
comboBox.close();

expect(spy.calledOnce).to.be.true;
Expand Down

0 comments on commit 38a5a13

Please sign in to comment.