Skip to content

Commit 0fa129a

Browse files
committed
feat(dateinput): Arrow down from input
1 parent 50240be commit 0fa129a

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

src/components/DateInput.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
@blur="handleInputBlur"
4545
@click="handleInputClick"
4646
@focus="handleInputFocus"
47+
@keydown.down.prevent="handleKeydownDown"
4748
@keydown.enter.prevent="handleKeydownEnter"
4849
@keydown.escape.prevent="$emit('close')"
4950
@keydown.space="handleKeydownSpace($event)"
@@ -234,6 +235,20 @@ export default {
234235
235236
this.$emit('focus')
236237
},
238+
/**
239+
* Opens the calendar, or sets the focus to the next focusable element down
240+
*/
241+
handleKeydownDown() {
242+
if (!this.isOpen) {
243+
this.$emit('open')
244+
}
245+
246+
if (!this.typeable) {
247+
return
248+
}
249+
250+
this.$emit('set-focus', ['prev', 'up', 'next', 'tabbableCell'])
251+
},
237252
/**
238253
* Formats a typed date and closes the calendar
239254
*/

test/unit/specs/DateInput/typedDates.spec.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,36 @@ describe('Datepicker mounted to document body', () => {
260260
wrapper.destroy()
261261
})
262262

263+
it('arrows down from the input field to the previous button when the calendar is open', async () => {
264+
const input = wrapper.find('input')
265+
266+
await input.trigger('click')
267+
await input.trigger('keydown.down')
268+
269+
const prevButton = wrapper.find('button.prev')
270+
271+
expect(document.activeElement).toBe(prevButton.element)
272+
})
273+
274+
it('arrows down from the input field to the `tabbable-cell` when `show-header` is false', async () => {
275+
await wrapper.setProps({
276+
showHeader: false,
277+
})
278+
279+
const input = wrapper.find('input')
280+
281+
await input.trigger('click')
282+
await input.trigger('keydown.down')
283+
284+
const tabbableCell = wrapper.find('button.cell[data-test-tabbable-cell]')
285+
286+
expect(document.activeElement).toBe(tabbableCell.element)
287+
})
288+
263289
it('arrows up from the previous button to the input field', async () => {
264290
const input = wrapper.find('input')
265291

266292
await input.trigger('click')
267-
await wrapper.vm.$nextTick()
268293

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

278303
await input.trigger('click')
279-
await wrapper.vm.$nextTick()
280304

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

290314
await input.trigger('click')
291-
await wrapper.vm.$nextTick()
292315

293316
const upButton = wrapper.find('button.vdp-datepicker__up')
294317
await upButton.trigger('keydown.up')

test/unit/specs/Datepicker/Datepicker.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,37 @@ describe('Datepicker mounted to body', () => {
316316
})
317317
})
318318

319+
describe('Datepicker mounted to body with openDate', () => {
320+
let wrapper
321+
322+
beforeEach(() => {
323+
jest.useFakeTimers()
324+
325+
wrapper = mount(Datepicker, {
326+
attachTo: document.body,
327+
propsData: {
328+
openDate: new Date(2020, 0, 1),
329+
},
330+
})
331+
})
332+
333+
afterEach(() => {
334+
jest.clearAllTimers()
335+
wrapper.destroy()
336+
})
337+
338+
it('opens the calendar on pressing the `down` arrow when the input is focused', async () => {
339+
const input = wrapper.find('input')
340+
341+
await input.trigger('focus')
342+
await input.trigger('keydown.down')
343+
jest.advanceTimersByTime(250)
344+
345+
const openDateCell = wrapper.find('button.open')
346+
expect(document.activeElement).toBe(openDateCell.element)
347+
})
348+
})
349+
319350
describe('Datepicker shallowMounted', () => {
320351
let wrapper
321352
let date

0 commit comments

Comments
 (0)