Skip to content

Commit 04c8ff5

Browse files
authored
refactor: update date-picker Escape logic to stop propagation (#10477)
1 parent 066574a commit 04c8ff5

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

packages/date-picker/src/vaadin-date-picker-mixin.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,11 @@ export const DatePickerMixin = (subclass) =>
889889
}
890890

891891
/** @protected */
892-
_onOverlayEscapePress() {
892+
_onOverlayEscapePress(event) {
893+
event.stopPropagation();
893894
this._focusedDate = this._selectedDate;
894-
this._closedByEscape = true;
895+
this._applyInputValue(this._selectedDate);
895896
this._close();
896-
this._closedByEscape = false;
897897
}
898898

899899
/** @protected */
@@ -984,9 +984,6 @@ export const DatePickerMixin = (subclass) =>
984984
}
985985
window.removeEventListener('scroll', this._boundOnScroll, true);
986986

987-
if (this._closedByEscape) {
988-
this._applyInputValue(this._selectedDate);
989-
}
990987
this.__commitParsedOrFocusedDate();
991988

992989
if (this.inputElement && this.inputElement.selectionStart) {
@@ -1163,8 +1160,8 @@ export const DatePickerMixin = (subclass) =>
11631160
* @override
11641161
*/
11651162
_onEscape(event) {
1166-
// Closing overlay is handled in vaadin-overlay-escape-press event listener.
11671163
if (this.opened) {
1164+
this._onOverlayEscapePress(event);
11681165
return;
11691166
}
11701167

packages/date-picker/src/vaadin-date-picker.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ class DatePicker extends DatePickerMixin(
227227
theme="${ifDefined(this._theme)}"
228228
.opened="${this.opened}"
229229
@opened-changed="${this._onOpenedChanged}"
230-
@vaadin-overlay-escape-press="${this._onOverlayEscapePress}"
231230
@vaadin-overlay-open="${this._onOverlayOpened}"
232231
@vaadin-overlay-close="${this._onVaadinOverlayClose}"
233232
@vaadin-overlay-closing="${this._onOverlayClosed}"

packages/date-picker/test/keyboard-input.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { sendKeys } from '@vaadin/test-runner-commands';
3-
import { aTimeout, enter, fixtureSync, nextRender, tap } from '@vaadin/testing-helpers';
3+
import { aTimeout, enter, fixtureSync, keyboardEventFor, nextRender, tap } from '@vaadin/testing-helpers';
44
import sinon from 'sinon';
55
import '../src/vaadin-date-picker.js';
66
import { formatISODate, getAdjustedYear, parseDate } from '../src/vaadin-date-picker-helper.js';
@@ -334,6 +334,13 @@ describe('keyboard', () => {
334334
await sendKeys({ press: 'Escape' });
335335
expect(document.activeElement).to.equal(input);
336336
});
337+
338+
it('should stop propagation on Esc when closing overlay', () => {
339+
const event = keyboardEventFor('keydown', 27, [], 'Escape');
340+
const spy = sinon.spy(event, 'stopPropagation');
341+
datePicker.inputElement.dispatchEvent(event);
342+
expect(spy.calledOnce).to.be.true;
343+
});
337344
});
338345
});
339346

0 commit comments

Comments
 (0)