Skip to content

Commit 175fb95

Browse files
authored
refactor: update select Escape logic to stop propagation (#10482)
1 parent 8d693b0 commit 175fb95

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/select/src/vaadin-select-base-mixin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,21 @@ export const SelectBaseMixin = (superClass) =>
324324
this.opened = !this.readonly;
325325
}
326326

327+
/**
328+
* Override an event listener from `KeyboardMixin`
329+
* stop propagation when closing overlay on Escape.
330+
*
331+
* @param {!KeyboardEvent} event
332+
* @protected
333+
* @override
334+
*/
335+
_onEscape(event) {
336+
if (this.opened) {
337+
event.stopPropagation();
338+
this.opened = false;
339+
}
340+
}
341+
327342
/** @private */
328343
_onToggleMouseDown(event) {
329344
// Prevent mousedown event to avoid blur and preserve focused state
@@ -343,6 +358,8 @@ export const SelectBaseMixin = (superClass) =>
343358
* @override
344359
*/
345360
_onKeyDown(e) {
361+
super._onKeyDown(e);
362+
346363
if (e.target === this.focusElement && !this.readonly && !this.disabled && !this.opened) {
347364
if (/^(Enter|SpaceBar|\s|ArrowDown|Down|ArrowUp|Up)$/u.test(e.key)) {
348365
e.preventDefault();

packages/select/test/keyboard.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { resetMouse, sendKeys, sendMouseToElement } from '@vaadin/test-runner-commands';
3-
import { aTimeout, fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers';
3+
import { aTimeout, fixtureSync, keyboardEventFor, nextRender, nextUpdate } from '@vaadin/testing-helpers';
44
import sinon from 'sinon';
55
import './not-animated-styles.js';
66
import '../src/vaadin-select.js';
@@ -111,6 +111,18 @@ describe('keyboard', () => {
111111
expect(select.hasAttribute('focus-ring')).to.be.true;
112112
expect(valueButton.hasAttribute('focus-ring')).to.be.true;
113113
});
114+
115+
it('should stop propagation on Esc when closing overlay', async () => {
116+
await sendKeys({ press: 'Tab' });
117+
118+
await sendKeys({ press: 'Enter' });
119+
await nextRender();
120+
121+
const event = keyboardEventFor('keydown', 27, [], 'Escape');
122+
const spy = sinon.spy(event, 'stopPropagation');
123+
menu.dispatchEvent(event);
124+
expect(spy.calledOnce).to.be.true;
125+
});
114126
});
115127

116128
describe('selection', () => {

0 commit comments

Comments
 (0)