Skip to content

Commit d4efe50

Browse files
committed
fix(datepicker): Set dates to 1st January in year view
1 parent bf68b64 commit d4efe50

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

src/components/Datepicker.vue

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,14 @@ export default {
266266
},
267267
data() {
268268
const utils = makeDateUtils(this.useUtc)
269-
const startDate = utils.getNewDateObject(this.openDate || null)
270-
const pageTimestamp = utils.setDate(startDate, 1)
269+
const initialView = this.initialView || this.minimumView
270+
const openDate = utils.getOpenDate(
271+
this.openDate,
272+
this.selectedDate,
273+
initialView,
274+
)
275+
276+
const pageTimestamp = utils.setDate(openDate, 1)
271277
272278
return {
273279
calendarHeight: 0,
@@ -299,14 +305,11 @@ export default {
299305
return this.initialView || this.minimumView
300306
},
301307
computedOpenDate() {
302-
const parsedOpenDate = this.parseValue(this.openDate)
303-
const openDateOrToday = this.utils.getNewDateObject(parsedOpenDate)
304-
const openDate = this.selectedDate || openDateOrToday
305-
306-
// If the `minimum-view` is `month` or `year`, convert `openDate` accordingly
307-
return this.minimumView === 'day'
308-
? openDate
309-
: new Date(this.utils.setDate(openDate, 1))
308+
return this.utils.getOpenDate(
309+
this.openDate,
310+
this.selectedDate,
311+
this.minimumView,
312+
)
310313
},
311314
datepickerId() {
312315
return `vdp-${Math.random().toString(36).slice(-10)}`
@@ -376,7 +379,7 @@ export default {
376379
// eslint-disable-next-line complexity
377380
handler() {
378381
const selectedDate =
379-
this.selectedDate || this.parseValue(this.modelValue)
382+
this.selectedDate || this.utils.parseAsDate(this.modelValue)
380383
381384
if (!selectedDate) {
382385
return
@@ -412,8 +415,8 @@ export default {
412415
},
413416
modelValue: {
414417
handler(newValue, oldValue) {
415-
let parsedValue = this.parseValue(newValue)
416-
const oldParsedValue = this.parseValue(oldValue)
418+
let parsedValue = this.utils.parseAsDate(newValue)
419+
const oldParsedValue = this.utils.parseAsDate(oldValue)
417420
418421
if (!this.utils.compareDates(parsedValue, oldParsedValue)) {
419422
const isDateDisabled = parsedValue && this.isDateDisabled(parsedValue)
@@ -430,6 +433,9 @@ export default {
430433
this.setPageDate()
431434
},
432435
view(newView, oldView) {
436+
if (oldView === '') {
437+
this.setPageDate(this.utils.adjustDateToView(this.computedOpenDate))
438+
}
433439
this.handleViewChange(newView, oldView)
434440
},
435441
},
@@ -703,18 +709,6 @@ export default {
703709
704710
this.$emit('opened')
705711
},
706-
/**
707-
* Parse a datepicker modelValue from string/number to date
708-
* @param {Date|String|Number|undefined} date
709-
* @returns {Date|null}
710-
*/
711-
parseValue(date) {
712-
if (typeof date === 'string' || typeof date === 'number') {
713-
const parsed = new Date(date)
714-
return this.utils.isValidDate(parsed) ? parsed : null
715-
}
716-
return this.utils.isValidDate(date) ? date : null
717-
},
718712
/**
719713
* Focus the open date, or close the calendar if already focused
720714
*/

src/utils/DateUtils.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ const utils = {
360360
return new Date(parsedDate)
361361
},
362362

363+
/**
364+
* Parses a string/number to a date, or returns null
365+
* @param {Date|String|Number|undefined} date
366+
* @returns {Date|null}
367+
*/
368+
parseAsDate(date) {
369+
if (typeof date === 'string' || typeof date === 'number') {
370+
const parsed = new Date(date)
371+
return this.isValidDate(parsed) ? parsed : null
372+
}
373+
return this.isValidDate(date) ? date : null
374+
},
375+
363376
getTime() {
364377
const time = 'T00:00:00'
365378

@@ -401,6 +414,20 @@ const utils = {
401414
: this.resetDateTime(new Date())
402415
},
403416

417+
/**
418+
* Returns the `open date` at a given view
419+
* @param {Date|null} openDate the date on which the datepicker should open
420+
* @param {View} view Either `day`, `month`, or `year`
421+
* @return {Date|null}
422+
*/
423+
getOpenDate(openDate, selectedDate, view) {
424+
const parsedOpenDate = this.parseAsDate(openDate)
425+
const openDateOrToday = this.getNewDateObject(parsedOpenDate)
426+
const newOpenDate = selectedDate || openDateOrToday
427+
428+
return this.adjustDateToView(newOpenDate, view)
429+
},
430+
404431
/**
405432
* Converts a date according to a given view
406433
* e.g. '2025-05-15' becomes '2025-05-01' at `month view and

test/e2e/specs/CellSelection.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Feature: Cell Selection
44
So that I pick a date
55

66

7-
# N.B. Until native events are supported by Cypress, we are simulating `enter` and `space bar` key presses
8-
# on buttons by using a `click` event.
9-
# See https://github.com/cypress-io/cypress/issues/311
10-
# and https://github.com/cypress-io/cypress/issues/8267
7+
# N.B. Until native events are supported by Cypress, we are simulating `enter` and `space bar` key presses
8+
# on buttons by using a `click` event.
9+
# See https://github.com/cypress-io/cypress/issues/311
10+
# and https://github.com/cypress-io/cypress/issues/8267
1111

1212
@id-1
1313
Scenario Outline: Select a cell: minimum view: <minimumView>
@@ -20,7 +20,7 @@ Feature: Cell Selection
2020
| # | minimumView | date |
2121
| 1 | day | 15 Mar 2020 |
2222
| 2 | month | 01 Mar 2020 |
23-
| 3 | year | 01 Mar 2020 |
23+
| 3 | year | 01 Jan 2020 |
2424

2525

2626
@id-2

test/e2e/specs/CellSelection/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ describe('Cell selection', () => {
1010
createCalendar({
1111
openDate: new Date(2020, 2, 15),
1212
minimumView,
13-
initialView: minimumView,
1413
})
1514

1615
clickThe('input')

0 commit comments

Comments
 (0)