Skip to content

Commit 8807c91

Browse files
committed
feat(datepicker): Focus correct element on close
1 parent 566e188 commit 8807c91

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/components/DateInput.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
<!-- Calendar Button -->
55
<button
66
v-if="calendarButton"
7+
ref="calendarButton"
78
class="vdp-datepicker__calendar-button"
89
:class="{ 'btn input-group-prepend': bootstrapStyling }"
910
data-test-calendar-button
1011
:disabled="disabled"
1112
type="button"
12-
@click="toggle"
13+
@click="toggle('calendarButton')"
1314
@focus="handleButtonFocus"
1415
>
1516
<span :class="{ 'input-group-text': bootstrapStyling }">
@@ -271,7 +272,11 @@ export default {
271272
/**
272273
* Opens or closes the calendar
273274
*/
274-
toggle() {
275+
toggle(calendarButton) {
276+
if (this.isOpen) {
277+
this.$emit('set-focus', [calendarButton || 'input'])
278+
}
279+
275280
this.$emit(this.isOpen ? 'close' : 'open')
276281
},
277282
},

src/components/Datepicker.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@close="close"
4242
@focus="handleInputFocus"
4343
@open="open"
44+
@set-focus="setFocus($event)"
4445
@typed-date="handleTypedDate"
4546
>
4647
<slot slot="beforeDateInput" name="beforeDateInput" />
@@ -392,6 +393,8 @@ export default {
392393
if (this.showCalendarOnFocus || !this.focus.refs.length) {
393394
this.$refs.dateInput.shouldToggleOnClick = true
394395
document.body.focus()
396+
} else {
397+
this.reviewFocus()
395398
}
396399
397400
this.$emit('closed')

src/mixins/navMixin.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ export default {
5252
* @param {string} ref The `ref` name of the wanted element
5353
* @returns {HTMLElement|Vue} A Vue element
5454
*/
55-
// eslint-disable-next-line complexity
55+
// eslint-disable-next-line complexity,max-statements
5656
getElementByRef(ref) {
5757
if (ref === 'tabbableCell') {
5858
return this.tabbableCell
5959
}
6060
if (ref === 'input') {
6161
return this.$refs.dateInput && this.$refs.dateInput.$refs[this.refName]
6262
}
63+
if (ref === 'calendarButton') {
64+
return this.$refs.dateInput.$refs.calendarButton
65+
}
6366
if (this.showHeader) {
6467
if (ref === 'up') {
6568
return this.$refs.picker && this.$refs.picker.$refs.up.$el
@@ -163,6 +166,14 @@ export default {
163166
setAllElements() {
164167
this.allElements = this.getFocusableElements(this.$refs.datepicker)
165168
},
169+
/**
170+
* Set the focus
171+
* @param {Array} refs An array of `refs` to focus (in order of preference)
172+
*/
173+
setFocus(refs) {
174+
this.focus.refs = refs
175+
this.applyFocus()
176+
},
166177
/**
167178
* Determines which elements in datepicker should be focus-trapped
168179
*/

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,27 @@ describe('Datepicker mounted to body', () => {
252252
expect(wrapper.vm.isOpen).toBeFalsy()
253253
})
254254

255+
it('focuses the calendar button when closed via the calendar button', async () => {
256+
await wrapper.setProps({
257+
calendarButton: true,
258+
})
259+
260+
const calendarButton = wrapper.find('button[data-test-calendar-button]')
261+
await calendarButton.trigger('click')
262+
jest.advanceTimersByTime(250)
263+
264+
expect(wrapper.vm.isOpen).toBeTruthy()
265+
266+
const openDateCell = wrapper.find('button.open')
267+
expect(document.activeElement).toStrictEqual(openDateCell.element)
268+
269+
await calendarButton.trigger('click')
270+
jest.advanceTimersByTime(250)
271+
272+
expect(wrapper.vm.isOpen).toBeFalsy()
273+
expect(document.activeElement).toBe(calendarButton.element)
274+
})
275+
255276
it('does not arrow up from the previous button to the input field', async () => {
256277
const input = wrapper.find('input')
257278

0 commit comments

Comments
 (0)