Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Close popup on press ESC key
Browse files Browse the repository at this point in the history
  • Loading branch information
mariomka committed Jan 18, 2018
1 parent 09ae40e commit 072063f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Old features not yet implemented:

New features:

- Close on press `ESC` key
- Auto-detect the first day of week

## Installation
Expand Down
13 changes: 13 additions & 0 deletions src/DatetimePopup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export default {
}
},
created () {
document.addEventListener('keyup', this.onKeyup)
},
beforeDestroy () {
document.removeEventListener('keyup', this.onKeyup)
},
computed: {
year () {
return this.newDatetime.year
Expand Down Expand Up @@ -181,6 +189,11 @@ export default {
}
this.timeTouched = true
},
onKeyup (event) {
if (event.keyCode === 27) {
this.cancel()
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions test/specs/DatetimePopup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,32 @@ describe('DatetimePopup.vue', function () {
vm.$('.vdatetime-popup__actions__button--cancel').click()
expect(vm.spy).to.have.been.calledOnce
})

it('should emit cancel event on key up ESC', function (done) {
const vm = createVM(this,
`<DatetimePopup :datetime="datetime" @cancel="spy"></DatetimePopup>`,
{
components: { DatetimePopup },
data () {
return {
datetime: LuxonDatetime.local(),
spy: sinon.spy()
}
}
})

expect(vm.spy).to.have.not.been.called

const event = document.createEvent('Event')
event.keyCode = 27
event.initEvent('keyup')
document.dispatchEvent(event)

vm.$nextTick(() => {
expect(vm.spy).to.have.been.calledOnce
done()
})
})
})

describe('auto', function () {
Expand Down

0 comments on commit 072063f

Please sign in to comment.