Skip to content

Commit

Permalink
close the calendar on enter or escape key
Browse files Browse the repository at this point in the history
  • Loading branch information
Charlie Kassel committed May 3, 2018
1 parent ab94b55 commit d6062bf
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/components/Datepicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ export default {
if (!this.isInline) {
setTimeout(() => {
document.addEventListener('click', this.clickOutside, false)
document.addEventListener('keyup', this.escapeCalendar, false)
}, 100)
}
},
Expand All @@ -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)
}
},
/**
Expand Down

0 comments on commit d6062bf

Please sign in to comment.