Skip to content

Commit

Permalink
fix: do not fire custom-value-set on input blur or outside click (#8308
Browse files Browse the repository at this point in the history
…) (#8316)
  • Loading branch information
web-padawan authored Dec 11, 2024
1 parent 252e905 commit 5ff1d09
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ class MultiSelectComboBoxInternal extends ComboBoxDataProviderMixin(ComboBoxMixi
return 'vaadin-multi-select-combo-box';
}

constructor() {
super();

this.addEventListener('custom-value-set', this.__onCustomValueSet.bind(this));
}

/**
* Override method inherited from the combo-box
* to allow opening dropdown when readonly.
Expand Down Expand Up @@ -449,6 +455,15 @@ class MultiSelectComboBoxInternal extends ComboBoxDataProviderMixin(ComboBoxMixi

super.clearCache();
}

/** @private */
__onCustomValueSet(event) {
// Prevent setting custom value on input blur or outside click,
// so it can be only committed explicitly by pressing Enter.
if (this._ignoreCommitValue) {
event.stopImmediatePropagation();
}
}
}

customElements.define(MultiSelectComboBoxInternal.is, MultiSelectComboBoxInternal);
19 changes: 18 additions & 1 deletion packages/multi-select-combo-box/test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
import sinon from 'sinon';
import './not-animated-styles.js';
import '../vaadin-multi-select-combo-box.js';
Expand Down Expand Up @@ -320,6 +320,23 @@ describe('basic', () => {
await sendKeys({ down: 'Enter' });
expect(comboBox.selectedItems).to.deep.equal(['apple']);
});

it('should not fire custom-value-set event when pressing Tab', async () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);
await sendKeys({ type: 'pear' });
await sendKeys({ down: 'Tab' });
expect(spy.called).to.be.false;
});

it('should not fire custom-value-set event on outside click', async () => {
const spy = sinon.spy();
comboBox.addEventListener('custom-value-set', spy);
await sendKeys({ type: 'ap' });
await sendMouse({ type: 'click', position: [200, 200] });
await resetMouse();
expect(spy.called).to.be.false;
});
});

describe('helper text', () => {
Expand Down
17 changes: 16 additions & 1 deletion packages/multi-select-combo-box/test/selecting-items.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { sendKeys } from '@web/test-runner-commands';
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
import sinon from 'sinon';
import './not-animated-styles.js';
import '../vaadin-multi-select-combo-box.js';
Expand Down Expand Up @@ -113,6 +113,21 @@ describe('selecting items', () => {
expect(comboBox.selectedItems).to.deep.equal(['apple']);
});

it('should not select an item on outside click when it is focused', async () => {
await sendKeys({ down: 'ArrowDown' });
await sendKeys({ down: 'ArrowDown' });
await sendMouse({ type: 'click', position: [200, 200] });
await resetMouse();
expect(comboBox.selectedItems).to.deep.equal([]);
});

it('should not select an item on blur when it is focused', async () => {
await sendKeys({ down: 'ArrowDown' });
await sendKeys({ down: 'ArrowDown' });
await sendKeys({ down: 'Tab' });
expect(comboBox.selectedItems).to.deep.equal([]);
});

it('should un-select item when using clear() method', () => {
comboBox.selectedItems = ['orange'];
comboBox.clear();
Expand Down

0 comments on commit 5ff1d09

Please sign in to comment.