Skip to content

Commit

Permalink
feat(dateinput): Arrow down from input
Browse files Browse the repository at this point in the history
  • Loading branch information
mst101 committed Aug 18, 2021
1 parent 50240be commit 0fa129a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/components/DateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
@blur="handleInputBlur"
@click="handleInputClick"
@focus="handleInputFocus"
@keydown.down.prevent="handleKeydownDown"
@keydown.enter.prevent="handleKeydownEnter"
@keydown.escape.prevent="$emit('close')"
@keydown.space="handleKeydownSpace($event)"
Expand Down Expand Up @@ -234,6 +235,20 @@ export default {
this.$emit('focus')
},
/**
* Opens the calendar, or sets the focus to the next focusable element down
*/
handleKeydownDown() {
if (!this.isOpen) {
this.$emit('open')
}
if (!this.typeable) {
return
}
this.$emit('set-focus', ['prev', 'up', 'next', 'tabbableCell'])
},
/**
* Formats a typed date and closes the calendar
*/
Expand Down
29 changes: 26 additions & 3 deletions test/unit/specs/DateInput/typedDates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,36 @@ describe('Datepicker mounted to document body', () => {
wrapper.destroy()
})

it('arrows down from the input field to the previous button when the calendar is open', async () => {
const input = wrapper.find('input')

await input.trigger('click')
await input.trigger('keydown.down')

const prevButton = wrapper.find('button.prev')

expect(document.activeElement).toBe(prevButton.element)
})

it('arrows down from the input field to the `tabbable-cell` when `show-header` is false', async () => {
await wrapper.setProps({
showHeader: false,
})

const input = wrapper.find('input')

await input.trigger('click')
await input.trigger('keydown.down')

const tabbableCell = wrapper.find('button.cell[data-test-tabbable-cell]')

expect(document.activeElement).toBe(tabbableCell.element)
})

it('arrows up from the previous button to the input field', async () => {
const input = wrapper.find('input')

await input.trigger('click')
await wrapper.vm.$nextTick()

const prevButton = wrapper.find('button.prev')
await prevButton.trigger('keydown.up')
Expand All @@ -276,7 +301,6 @@ describe('Datepicker mounted to document body', () => {
const input = wrapper.find('input')

await input.trigger('click')
await wrapper.vm.$nextTick()

const nextButton = wrapper.find('button.next')
await nextButton.trigger('keydown.up')
Expand All @@ -288,7 +312,6 @@ describe('Datepicker mounted to document body', () => {
const input = wrapper.find('input')

await input.trigger('click')
await wrapper.vm.$nextTick()

const upButton = wrapper.find('button.vdp-datepicker__up')
await upButton.trigger('keydown.up')
Expand Down
31 changes: 31 additions & 0 deletions test/unit/specs/Datepicker/Datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,37 @@ describe('Datepicker mounted to body', () => {
})
})

describe('Datepicker mounted to body with openDate', () => {
let wrapper

beforeEach(() => {
jest.useFakeTimers()

wrapper = mount(Datepicker, {
attachTo: document.body,
propsData: {
openDate: new Date(2020, 0, 1),
},
})
})

afterEach(() => {
jest.clearAllTimers()
wrapper.destroy()
})

it('opens the calendar on pressing the `down` arrow when the input is focused', async () => {
const input = wrapper.find('input')

await input.trigger('focus')
await input.trigger('keydown.down')
jest.advanceTimersByTime(250)

const openDateCell = wrapper.find('button.open')
expect(document.activeElement).toBe(openDateCell.element)
})
})

describe('Datepicker shallowMounted', () => {
let wrapper
let date
Expand Down

0 comments on commit 0fa129a

Please sign in to comment.