Skip to content

Commit 0ccf0e0

Browse files
committed
Close the picker on ESC #128
1 parent 2386e39 commit 0ccf0e0

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

lib/js/__tests__/index-test.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,34 @@ test("opening the picker ads backdrop and picker elements to the dom", () => {
1616

1717
const $backdrop = $(".c-scrim");
1818
const $picker = $(".c-datepicker");
19-
19+
2020
expect($picker).not.toBeNull();
2121
expect($backdrop).not.toBeNull();
2222
});
2323

2424
test("closing the picker removes elements from the dom", async () => {
25-
const picker = await openPicker()
25+
const picker = await openPicker();
2626
picker.close();
2727
await delay(300);
28-
28+
2929
const $backdrop = $(".c-scrim");
3030
const $picker = $(".c-datepicker");
3131

3232
expect($picker).toBeNull();
3333
expect($backdrop).toBeNull();
3434
});
3535

36+
test.only("the picker closes when the escape key is pressed", async () => {
37+
const picker = await openPicker();
38+
39+
const event = new window.KeyboardEvent('keydown', { which: 27, keyCode: 27 });
40+
window.dispatchEvent(event);
41+
42+
await delay(300);
43+
const $picker = $(".c-datepicker")
44+
expect($picker).toBeNull();
45+
});
46+
3647
test("opening the picker with a default time", async () => {
3748
// time divides 5 minutes; set exactly
3849
const time = "2017-02-01T17:30:00.000Z";
@@ -126,4 +137,4 @@ test("picker#set quanitizes to the nearest 5 minutes", async () => {
126137
// test round up
127138
picker.set("2017-02-01T18:35:04.941Z");
128139
expect(picker.data().toISOString()).toEqual( "2017-02-01T18:35:00.000Z");
129-
})
140+
})

lib/js/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Events from './events';
77

88
import '../scss/material-datetime-picker.scss';
99

10+
const ESC_KEY = 27;
11+
1012
const prefix = 'c-datepicker';
1113
const defaults = () => ({
1214
default: moment().startOf('hour'),
@@ -98,10 +100,13 @@ class DateTimePicker extends Events {
98100
}
99101

100102
this.initializeRome(this.$(`.${this.options.styles.container}`), this.options.dateValidator);
103+
this._listenForCloseEvents();
104+
101105
this._show();
102106
}
103107

104108
close() {
109+
this._stopListeningForCloseEvents();
105110
this._hide();
106111
}
107112

@@ -127,6 +132,21 @@ class DateTimePicker extends Events {
127132
return this;
128133
}
129134

135+
_listenForCloseEvents() {
136+
this._onWindowKeypress = (e) => {
137+
if (e.which === ESC_KEY) {
138+
this.close();
139+
}
140+
};
141+
142+
window.addEventListener("keydown", this._onWindowKeypress);
143+
}
144+
145+
_stopListeningForCloseEvents() {
146+
window.removeEventListener("keydown", this._onWindowKeypress);
147+
this._closeHandler = null;
148+
}
149+
130150
delegateEvents() {
131151
this.$('.js-cancel')
132152
.addEventListener('click', () => this.clickCancel(), false);

0 commit comments

Comments
 (0)