Skip to content

Commit d9ee675

Browse files
authored
refactor: simplify multi-select-combo-box Enter handling (#6372)
1 parent 7cc2a9d commit d9ee675

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,33 +160,25 @@ class MultiSelectComboBoxInternal extends ComboBoxDataProviderMixin(ComboBoxMixi
160160
* @override
161161
*/
162162
_onEnter(event) {
163-
this.__enterPressed = true;
164-
165-
super._onEnter(event);
166-
}
167-
168-
/**
169-
* @protected
170-
* @override
171-
*/
172-
_closeOrCommit() {
173-
if (this.readonly) {
174-
this.close();
175-
return;
176-
}
177-
178-
if (this.__enterPressed) {
179-
this.__enterPressed = null;
163+
if (this.opened) {
164+
// Do not submit the surrounding form.
165+
event.preventDefault();
166+
// Do not trigger global listeners.
167+
event.stopPropagation();
180168

181-
// Keep selected item focused after committing on Enter.
182-
const focusedItem = this.filteredItems[this._focusedIndex];
183-
this._commitValue();
184-
this._focusedIndex = this.filteredItems.indexOf(focusedItem);
169+
if (this.readonly) {
170+
this.close();
171+
} else {
172+
// Keep selected item focused after committing on Enter.
173+
const focusedItem = this.filteredItems[this._focusedIndex];
174+
this._commitValue();
175+
this._focusedIndex = this.filteredItems.indexOf(focusedItem);
176+
}
185177

186178
return;
187179
}
188180

189-
super._closeOrCommit();
181+
super._onEnter(event);
190182
}
191183

192184
/**

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
2+
import { fixtureSync, keyboardEventFor, nextRender } from '@vaadin/testing-helpers';
33
import { sendKeys } from '@web/test-runner-commands';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
@@ -132,4 +132,33 @@ describe('selecting items', () => {
132132
expect(comboBox.selectedItems).to.deep.equal(['orange']);
133133
});
134134
});
135+
136+
describe('enter key propagation', () => {
137+
const verifyEnterKeyPropagation = (allowPropagation) => {
138+
const enterEvent = keyboardEventFor('keydown', 13, [], 'Enter');
139+
const stopPropagationSpy = sinon.spy(enterEvent, 'stopPropagation');
140+
inputElement.dispatchEvent(enterEvent);
141+
expect(stopPropagationSpy.called).to.equal(!allowPropagation);
142+
};
143+
144+
beforeEach(async () => {
145+
comboBox.items = ['apple', 'banana', 'lemon', 'orange'];
146+
});
147+
148+
it('should stop propagation of the keyboard Enter event when dropdown is opened', async () => {
149+
await sendKeys({ press: 'ArrowDown' });
150+
verifyEnterKeyPropagation(false);
151+
});
152+
153+
it('should stop propagation of the keyboard Enter event when input value is invalid', async () => {
154+
await sendKeys({ type: 'lime' });
155+
verifyEnterKeyPropagation(false);
156+
});
157+
158+
it('should stop propagation of the keyboard Enter event when input is invalid and not opened', async () => {
159+
comboBox.autoOpenDisabled = true;
160+
await sendKeys({ type: 'lime' });
161+
verifyEnterKeyPropagation(false);
162+
});
163+
});
135164
});

0 commit comments

Comments
 (0)