Skip to content

Commit 3478953

Browse files
committed
feat(navMixin): Update tabbableCell with latestValidTypedDate
1 parent 9d227db commit 3478953

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/mixins/navMixin.vue

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ export default {
4848
},
4949
},
5050
methods: {
51+
/**
52+
* Converts a date to first in month for `month` view or first in year for `year` view
53+
* @param {Date} date The date to convert
54+
* @returns {Date}
55+
*/
56+
getCellDate(date) {
57+
switch (this.view) {
58+
case 'month':
59+
return new Date(this.utils.setDate(date, 1))
60+
case 'year':
61+
return new Date(
62+
this.utils.setMonth(new Date(this.utils.setDate(date, 1)), 0),
63+
)
64+
default:
65+
return date
66+
}
67+
},
5168
/**
5269
* Returns true, unless tabbing should be focus-trapped
5370
* @return {Boolean}
@@ -104,6 +121,27 @@ export default {
104121
105122
return null
106123
},
124+
/**
125+
* Returns the `cellId` for a given a date
126+
* @param {Date} date The date for which we need the cellId
127+
* @returns {Number|null}
128+
*/
129+
getCellId(date) {
130+
if (!date || !this.$refs.picker.$refs.cells) {
131+
return null
132+
}
133+
134+
const cellDate = this.getCellDate(date)
135+
const { cells } = this.$refs.picker.$refs.cells
136+
137+
for (let i = 0; i < cells.length; i += 1) {
138+
if (cells[i].timestamp === cellDate.valueOf()) {
139+
return i
140+
}
141+
}
142+
143+
return null
144+
},
107145
/**
108146
* Finds an element by its `ref` attribute
109147
* @param {string} ref The `ref` name of the wanted element
@@ -205,6 +243,18 @@ export default {
205243
206244
return this.$refs.dateInput.$refs[this.refName]
207245
},
246+
/**
247+
* Used for a typeable datepicker: returns the cell element that corresponds to latestValidTypedDate...
248+
*/
249+
getTypedCell() {
250+
if (!this.typeable) {
251+
return null
252+
}
253+
254+
const cellId = this.getCellId(this.latestValidTypedDate)
255+
256+
return cellId ? this.$refs.picker.$refs.cells.$el.children[cellId] : null
257+
},
208258
/**
209259
* Sets `datepickerId` (as a global) and keeps track of focusable elements
210260
*/
@@ -426,6 +476,7 @@ export default {
426476
427477
this.tabbableCell =
428478
this.getActiveCell() ||
479+
this.getTypedCell() ||
429480
pickerCells.querySelector('button.selected:not(.muted):enabled') ||
430481
pickerCells.querySelector('button.open:not(.muted):enabled') ||
431482
pickerCells.querySelector('button.today:not(.muted):enabled') ||

0 commit comments

Comments
 (0)