Skip to content

Commit

Permalink
Revert "fix: do not fire custom-value-set event twice (#3478)" (#3482)
Browse files Browse the repository at this point in the history
This reverts commit eaba82b.
  • Loading branch information
web-padawan committed Feb 22, 2022
1 parent d040976 commit b55b28a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 66 deletions.
15 changes: 2 additions & 13 deletions packages/combo-box/src/vaadin-combo-box-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,6 @@ export const ComboBoxMixin = (subclass) =>
this.selectedItem = null;

if (this.allowCustomValue) {
delete this._lastCustomValue;
this.value = '';
}
} else {
Expand All @@ -725,27 +724,17 @@ export const ComboBoxMixin = (subclass) =>
// to prevent a repetitive input value being saved after pressing ESC and Tab.
!itemMatchingByLabel
) {
const customValue = this._inputElementValue;

// User's logic in `custom-value-set` event listener might cause input to blur,
// which will result in attempting to commit the same custom value once again.
if (this._lastCustomValue === customValue) {
return;
}

// Store reference to the last custom value for checking it
this._lastCustomValue = customValue;

// An item matching by label was not found, but custom values are allowed.
// Dispatch a custom-value-set event with the input value.
const e = new CustomEvent('custom-value-set', {
detail: customValue,
detail: this._inputElementValue,
composed: true,
cancelable: true,
bubbles: true
});
this.dispatchEvent(e);
if (!e.defaultPrevented) {
const customValue = this._inputElementValue;
this._selectItemForValue(customValue);
this.value = customValue;
}
Expand Down
53 changes: 0 additions & 53 deletions packages/combo-box/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,59 +271,6 @@ describe('Properties', () => {

expect(spy.calledOnce).to.be.true;
});

it('should not fire twice when the custom value set listener causes blur', () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);

// Emulate opening the overlay that causes blur
comboBox.addEventListener('custom-value-set', () => {
comboBox.blur();
});

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

expect(spy.calledOnce).to.be.true;
});

it('should fire twice when another custom value is committed by the user', () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);

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

input.value = 'bar';
input.dispatchEvent(new CustomEvent('input'));
focusout(input);

expect(spy.calledTwice).to.be.true;
});

it('should fire when setting the same custom value after clearing', () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);

input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
focusout(input);

input.value = '';
input.dispatchEvent(new CustomEvent('input'));
focusout(input);

spy.resetHistory();
input.value = 'foo';
input.dispatchEvent(new CustomEvent('input'));
focusout(input);

expect(spy.calledOnce).to.be.true;
});
});
});

Expand Down

0 comments on commit b55b28a

Please sign in to comment.