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

Commit

Permalink
Continue or confirm popup on key down ENTER
Browse files Browse the repository at this point in the history
  • Loading branch information
mariomka committed Jan 19, 2018
1 parent 69edaaa commit ff281ca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/DatetimePopup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import DatetimeTimePicker from './DatetimeTimePicker'
import DatetimeYearPicker from './DatetimeYearPicker'
const KEY_TAB = 9
const KEY_ENTER = 13
const KEY_ESC = 27
export default {
Expand Down Expand Up @@ -206,8 +207,15 @@ export default {
}
},
onKeyDown (event) {
if (event.keyCode === KEY_ESC || event.keyCode === KEY_TAB) {
this.cancel()
switch (event.keyCode) {
case KEY_ESC:
case KEY_TAB:
this.cancel()
break
case KEY_ENTER:
this.nextStep()
break
}
}
}
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 @@ -315,6 +315,32 @@ describe('DatetimePopup.vue', function () {
})
})

it('should emit confirm event on key down ENTER', function (done) {
const vm = createVM(this,
`<DatetimePopup :datetime="datetime" @confirm="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 = 13
event.initEvent('keydown')
document.dispatchEvent(event)

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

it('should emit cancel event on cancel', function () {
const vm = createVM(this,
`<DatetimePopup :datetime="datetime" @cancel="spy"></DatetimePopup>`,
Expand Down

0 comments on commit ff281ca

Please sign in to comment.