File tree Expand file tree Collapse file tree 4 files changed +43
-3
lines changed
test/unit/specs/Datepicker Expand file tree Collapse file tree 4 files changed +43
-3
lines changed Original file line number Diff line number Diff line change 4
4
<!-- Calendar Button -->
5
5
<button
6
6
v-if =" calendarButton"
7
+ ref =" calendarButton"
7
8
class =" vdp-datepicker__calendar-button"
8
9
:class =" { 'btn input-group-prepend': bootstrapStyling }"
9
10
data-test-calendar-button
10
11
:disabled =" disabled"
11
12
type =" button"
12
- @click =" toggle"
13
+ @click =" toggle('calendarButton') "
13
14
@focus =" handleButtonFocus"
14
15
>
15
16
<span :class =" { 'input-group-text': bootstrapStyling }" >
@@ -271,7 +272,11 @@ export default {
271
272
/**
272
273
* Opens or closes the calendar
273
274
*/
274
- toggle () {
275
+ toggle (calendarButton ) {
276
+ if (this .isOpen ) {
277
+ this .$emit (' set-focus' , [calendarButton || ' input' ])
278
+ }
279
+
275
280
this .$emit (this .isOpen ? ' close' : ' open' )
276
281
},
277
282
},
Original file line number Diff line number Diff line change 41
41
@close =" close"
42
42
@focus =" handleInputFocus"
43
43
@open =" open"
44
+ @set-focus =" setFocus($event)"
44
45
@typed-date =" handleTypedDate"
45
46
>
46
47
<slot slot =" beforeDateInput" name =" beforeDateInput" />
@@ -392,6 +393,8 @@ export default {
392
393
if (this .showCalendarOnFocus || ! this .focus .refs .length ) {
393
394
this .$refs .dateInput .shouldToggleOnClick = true
394
395
document .body .focus ()
396
+ } else {
397
+ this .reviewFocus ()
395
398
}
396
399
397
400
this .$emit (' closed' )
Original file line number Diff line number Diff line change @@ -52,14 +52,17 @@ export default {
52
52
* @param {string} ref The `ref` name of the wanted element
53
53
* @returns {HTMLElement|Vue} A Vue element
54
54
*/
55
- // eslint-disable-next-line complexity
55
+ // eslint-disable-next-line complexity,max-statements
56
56
getElementByRef (ref ) {
57
57
if (ref === ' tabbableCell' ) {
58
58
return this .tabbableCell
59
59
}
60
60
if (ref === ' input' ) {
61
61
return this .$refs .dateInput && this .$refs .dateInput .$refs [this .refName ]
62
62
}
63
+ if (ref === ' calendarButton' ) {
64
+ return this .$refs .dateInput .$refs .calendarButton
65
+ }
63
66
if (this .showHeader ) {
64
67
if (ref === ' up' ) {
65
68
return this .$refs .picker && this .$refs .picker .$refs .up .$el
@@ -163,6 +166,14 @@ export default {
163
166
setAllElements () {
164
167
this .allElements = this .getFocusableElements (this .$refs .datepicker )
165
168
},
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
+ },
166
177
/**
167
178
* Determines which elements in datepicker should be focus-trapped
168
179
*/
Original file line number Diff line number Diff line change @@ -252,6 +252,27 @@ describe('Datepicker mounted to body', () => {
252
252
expect ( wrapper . vm . isOpen ) . toBeFalsy ( )
253
253
} )
254
254
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
+
255
276
it ( 'does not arrow up from the previous button to the input field' , async ( ) => {
256
277
const input = wrapper . find ( 'input' )
257
278
You can’t perform that action at this time.
0 commit comments