Skip to content

Commit 5ff1d09

Browse files
authored
fix: do not fire custom-value-set on input blur or outside click (#8308) (#8316)
1 parent 252e905 commit 5ff1d09

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

packages/multi-select-combo-box/src/vaadin-multi-select-combo-box-internal.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ class MultiSelectComboBoxInternal extends ComboBoxDataProviderMixin(ComboBoxMixi
149149
return 'vaadin-multi-select-combo-box';
150150
}
151151

152+
constructor() {
153+
super();
154+
155+
this.addEventListener('custom-value-set', this.__onCustomValueSet.bind(this));
156+
}
157+
152158
/**
153159
* Override method inherited from the combo-box
154160
* to allow opening dropdown when readonly.
@@ -449,6 +455,15 @@ class MultiSelectComboBoxInternal extends ComboBoxDataProviderMixin(ComboBoxMixi
449455

450456
super.clearCache();
451457
}
458+
459+
/** @private */
460+
__onCustomValueSet(event) {
461+
// Prevent setting custom value on input blur or outside click,
462+
// so it can be only committed explicitly by pressing Enter.
463+
if (this._ignoreCommitValue) {
464+
event.stopImmediatePropagation();
465+
}
466+
}
452467
}
453468

454469
customElements.define(MultiSelectComboBoxInternal.is, MultiSelectComboBoxInternal);

packages/multi-select-combo-box/test/basic.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
22
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
3-
import { sendKeys } from '@web/test-runner-commands';
3+
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
66
import '../vaadin-multi-select-combo-box.js';
@@ -320,6 +320,23 @@ describe('basic', () => {
320320
await sendKeys({ down: 'Enter' });
321321
expect(comboBox.selectedItems).to.deep.equal(['apple']);
322322
});
323+
324+
it('should not fire custom-value-set event when pressing Tab', async () => {
325+
const spy = sinon.spy();
326+
comboBox.addEventListener('custom-value-set', spy);
327+
await sendKeys({ type: 'pear' });
328+
await sendKeys({ down: 'Tab' });
329+
expect(spy.called).to.be.false;
330+
});
331+
332+
it('should not fire custom-value-set event on outside click', async () => {
333+
const spy = sinon.spy();
334+
comboBox.addEventListener('custom-value-set', spy);
335+
await sendKeys({ type: 'ap' });
336+
await sendMouse({ type: 'click', position: [200, 200] });
337+
await resetMouse();
338+
expect(spy.called).to.be.false;
339+
});
323340
});
324341

325342
describe('helper text', () => {

packages/multi-select-combo-box/test/selecting-items.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@esm-bundle/chai';
22
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
3-
import { sendKeys } from '@web/test-runner-commands';
3+
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
66
import '../vaadin-multi-select-combo-box.js';
@@ -113,6 +113,21 @@ describe('selecting items', () => {
113113
expect(comboBox.selectedItems).to.deep.equal(['apple']);
114114
});
115115

116+
it('should not select an item on outside click when it is focused', async () => {
117+
await sendKeys({ down: 'ArrowDown' });
118+
await sendKeys({ down: 'ArrowDown' });
119+
await sendMouse({ type: 'click', position: [200, 200] });
120+
await resetMouse();
121+
expect(comboBox.selectedItems).to.deep.equal([]);
122+
});
123+
124+
it('should not select an item on blur when it is focused', async () => {
125+
await sendKeys({ down: 'ArrowDown' });
126+
await sendKeys({ down: 'ArrowDown' });
127+
await sendKeys({ down: 'Tab' });
128+
expect(comboBox.selectedItems).to.deep.equal([]);
129+
});
130+
116131
it('should un-select item when using clear() method', () => {
117132
comboBox.selectedItems = ['orange'];
118133
comboBox.clear();

0 commit comments

Comments
 (0)