Skip to content

Commit 6c25f2b

Browse files
authored
fix: stop Escape keydown propagation if the event was handled (#3039)
1 parent 7098c39 commit 6c25f2b

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎packages/combo-box/src/vaadin-combo-box-mixin.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,14 @@ export const ComboBoxMixin = (subclass) =>
569569
_onEscape(e) {
570570
if (this.autoOpenDisabled) {
571571
// Auto-open is disabled
572-
if (this.value !== this._inputElementValue) {
572+
if (this.opened || (this.value !== this._inputElementValue && this._inputElementValue.length > 0)) {
573+
// The overlay is open or
573574
// The input value has changed but the change hasn't been committed, so cancel it.
575+
e.stopPropagation();
574576
this._focusedIndex = -1;
575577
this.cancel();
576-
} else if (this.clearButtonVisible && !this.opened) {
578+
} else if (this.clearButtonVisible && !this.opened && !!this.value) {
579+
e.stopPropagation();
577580
// The clear button is visible and the overlay is closed, so clear the value.
578581
this._clear();
579582
}
@@ -591,7 +594,8 @@ export const ComboBoxMixin = (subclass) =>
591594
// No item is focused, cancel the change and close the overlay
592595
this.cancel();
593596
}
594-
} else if (this.clearButtonVisible) {
597+
} else if (this.clearButtonVisible && !!this.value) {
598+
e.stopPropagation();
595599
// The clear button is visible and the overlay is closed, so clear the value.
596600
this._clear();
597601
}

‎packages/combo-box/test/keyboard.test.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
fire,
99
fixtureSync,
1010
focusout,
11+
keyboardEventFor,
1112
keyDownOn,
1213
nextFrame
1314
} from '@vaadin/testing-helpers';
@@ -69,6 +70,23 @@ describe('keyboard', () => {
6970

7071
expect(getFocusedIndex()).to.equal(0);
7172
});
73+
74+
it('should propagate escape key event if dropdown is closed', () => {
75+
const event = keyboardEventFor('keydown', 27, [], 'Escape');
76+
const keyDownSpy = sinon.spy(event, 'stopPropagation');
77+
input.dispatchEvent(event);
78+
expect(keyDownSpy.called).to.be.false;
79+
});
80+
81+
it('should not propagate esc keydown event when overlay is closed, clear button is visible and value is not empty', () => {
82+
comboBox.value = 'bar';
83+
comboBox.clearButtonVisible = true;
84+
85+
const event = keyboardEventFor('keydown', 27, [], 'Escape');
86+
const keyDownSpy = sinon.spy(event, 'stopPropagation');
87+
input.dispatchEvent(event);
88+
expect(keyDownSpy.called).to.be.true;
89+
});
7290
});
7391

7492
describe('navigating after overlay opened', () => {
@@ -231,7 +249,7 @@ describe('keyboard', () => {
231249
expect(input.value).to.equal('foobar');
232250
});
233251

234-
it('should revert to the custom value after keyboar navigation', () => {
252+
it('should revert to the custom value after keyboard navigation', () => {
235253
comboBox.allowCustomValue = true;
236254
comboBox.value = 'foobar';
237255
arrowDownKeyDown(input);
@@ -557,5 +575,14 @@ describe('keyboard', () => {
557575
escKeyDown(input);
558576
expect(comboBox.value).to.equal('bar');
559577
});
578+
579+
it('should not propagate when input value is not empty', () => {
580+
inputText('foo');
581+
582+
const event = keyboardEventFor('keydown', 27, [], 'Escape');
583+
const keyDownSpy = sinon.spy(event, 'stopPropagation');
584+
input.dispatchEvent(event);
585+
expect(keyDownSpy.called).to.be.true;
586+
});
560587
});
561588
});

0 commit comments

Comments
 (0)