From d6062bf40a2f36559c608dd99ad255556c08a75b Mon Sep 17 00:00:00 2001 From: Charlie Kassel Date: Thu, 3 May 2018 07:09:12 +0100 Subject: [PATCH] close the calendar on enter or escape key --- src/components/Datepicker.vue | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/components/Datepicker.vue b/src/components/Datepicker.vue index 85b66b20..8f37d9f2 100644 --- a/src/components/Datepicker.vue +++ b/src/components/Datepicker.vue @@ -417,6 +417,7 @@ export default { if (!this.isInline) { setTimeout(() => { document.addEventListener('click', this.clickOutside, false) + document.addEventListener('keyup', this.escapeCalendar, false) }, 100) } }, @@ -428,17 +429,32 @@ export default { if (this.$el && !this.$el.contains(event.target)) { this.resetDefaultPageDate() this.close(true) - document.removeEventListener('click', this.clickOutside, false) + } + }, + /** + * close the calendar if escape or enter are pressed + * @param {Event} + */ + escapeCalendar (event) { + if ([ + 27, // escape + 13 // enter + ].includes(event.keyCode)) { + this.close(true) } }, /** * Close all calendar layers + * @param {Boolean} full - emit close event */ close (full) { this.showDayView = this.showMonthView = this.showYearView = false if (!this.isInline) { - if (full) this.$emit('closed') + if (full) { + this.$emit('closed') + } document.removeEventListener('click', this.clickOutside, false) + document.addEventListener('keyup', this.escapeCalendar, false) } }, /**