Skip to content

Commit c63a968

Browse files
committed
feat(datepicker)!: Make from/to dates inclusive
1 parent d6689f0 commit c63a968

File tree

17 files changed

+104
-94
lines changed

17 files changed

+104
-94
lines changed

docs/guide/DisabledDates/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var state = {
2525
}
2626
```
2727

28-
All dates before 2016-01-05 are disabled.
28+
2016-01-05 and all earlier dates are disabled.
2929

3030
## Disable from a specific date
3131

@@ -37,7 +37,7 @@ var state = {
3737
}
3838
```
3939

40-
All dates after 2016-01-26 are disabled.
40+
2016-01-26 and all later dates are disabled.
4141

4242
## Disable specific days of the week
4343

@@ -102,7 +102,7 @@ var state = {
102102
}
103103
```
104104

105-
The dates from 2016-12-26 to 2016-12-29 (inclusive) and 2017-02-13 to 2017-03-24
105+
The dates from 2016-12-25 to 2016-12-30 (inclusive) and 2017-02-12 to 2017-03-25
106106
(inclusive) are disabled.
107107

108108
## Disable based on custom logic

docs/guide/HighlightedDates/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var state = {
2323
}
2424
```
2525

26-
Everything before 2016-01-05 is highlighted.
26+
2016-01-05 and all earlier dates are highlighted.
2727

2828
## Highlight from a specific date
2929

@@ -35,7 +35,7 @@ var state = {
3535
}
3636
```
3737

38-
Everything after 2016-01-26 is highlighted.
38+
2016-01-26 and all later dates are highlighted.
3939

4040
## Highlight specific days of the week
4141

docs/guide/Migration/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Migration
22

3+
## 5.x.x to 6.x.x
4+
5+
- disabled and highlighted `to`/`from` dates are now **inclusive** e.g. `<DatePicker :disabled-dates="{ from: new Date(2023, 6, 15) }" />` now disables 15th July 2023 and all dates beyond.
6+
37
## 4.x.x to 5.x.x
48

9+
- the datepicker now supports Vue 3. Please use version 4.x.x for Vue 2 compatibility.
510
- the `selected` event has been deprecated in favour of `input`. You should therefore listen to `input` events on the datepicker, or simply bind your date via v-model: `<DatePicker v-model="myDate" />`
611
- a `typeable` datepicker no longer selects the date each time the input string can be parsed to a date. Instead, a typed date is only selected - and an `input` event fired - when the input field is focused and the `enter` key is pressed, or when the datepicker loses focus entirely.
712
- a new `changed` event is emitted whenever the selected date deviates from its previous value.

src/components/PickerDay.vue

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,7 @@ export default {
204204
return false
205205
}
206206
207-
const { from } = this.disabledConfig
208-
const disabledFromMonth = this.utils.monthYearDate(from.year, from.month)
209-
const pageMonth = this.utils.monthYearDate(this.pageYear, this.pageMonth)
210-
211-
return disabledFromMonth <= pageMonth
207+
return this.latestPossibleDate < this.firstOfNextMonth
212208
},
213209
/**
214210
* Is the previous month disabled?
@@ -219,11 +215,15 @@ export default {
219215
return false
220216
}
221217
222-
const { to } = this.disabledConfig
223-
const disabledToMonth = this.utils.monthYearDate(to.year, to.month)
224-
const pageMonth = this.utils.monthYearDate(this.pageYear, this.pageMonth)
225-
226-
return disabledToMonth >= pageMonth
218+
return this.earliestPossibleDate > this.lastOfPreviousMonth
219+
},
220+
/**
221+
* The first day of the next page's month.
222+
* @return {Date}
223+
*/
224+
lastOfPreviousMonth() {
225+
const d = new Date(this.pageDate)
226+
return new Date(this.utils.setDate(d, 0))
227227
},
228228
/**
229229
* Returns the current page's month as an integer.

src/components/PickerMonth.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default {
103103
if (!this.disabledConfig.has.from) {
104104
return false
105105
}
106-
return this.disabledConfig.from.year <= this.pageYear
106+
return this.latestPossibleDate <= new Date(this.pageYear, 11, 31)
107107
},
108108
/**
109109
* Is the previous year disabled?
@@ -113,7 +113,7 @@ export default {
113113
if (!this.disabledConfig.has.to) {
114114
return false
115115
}
116-
return this.disabledConfig.to.year >= this.pageYear
116+
return this.earliestPossibleDate >= new Date(this.pageYear, 0, 1)
117117
},
118118
/**
119119
* Display the current page's year as the title.

src/components/PickerYear.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export default {
113113
if (!this.disabledConfig.has.from) {
114114
return false
115115
}
116-
return this.disabledConfig.from.year <= this.pageDecadeEnd
116+
const firstDayOfNextDecade = new Date(this.pageDecadeEnd + 1, 0, 1)
117+
return this.latestPossibleDate < firstDayOfNextDecade
117118
},
118119
/**
119120
* Is the previous decade disabled?
@@ -123,7 +124,8 @@ export default {
123124
if (!this.disabledConfig.has.to) {
124125
return false
125126
}
126-
return this.disabledConfig.to.year >= this.pageDecadeStart
127+
const lastDayOfPreviousDecade = new Date(this.pageDecadeStart - 1, 11, 31)
128+
return this.earliestPossibleDate > lastDayOfPreviousDecade
127129
},
128130
/**
129131
* The year at which the current yearRange starts

src/utils/DateUtils.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,6 @@ const utils = {
390390
)
391391
},
392392

393-
/**
394-
* Create a date object from a month and year, using UTC or not
395-
* @param {Number} year
396-
* @param {Number} monthIndex
397-
* @return {Date}
398-
*/
399-
monthYearDate(year, monthIndex) {
400-
return this.useUtc
401-
? new Date(Date.UTC(year, monthIndex, 1))
402-
: new Date(year, monthIndex, 1)
403-
},
404-
405393
/**
406394
* Return a new date object with hours/minutes/seconds/milliseconds removed.
407395
* Defaults to today's date, if no parameter is provided

src/utils/DisabledDate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ export default class DisabledDate {
4242

4343
return {
4444
to: () => {
45-
return has.to && date < disabledDates.to
45+
return has.to && date <= disabledDates.to
4646
},
4747
from: () => {
48-
return has.from && date > disabledDates.from
48+
return has.from && date >= disabledDates.from
4949
},
5050
range: () => {
5151
if (!has.ranges) return false
@@ -58,7 +58,7 @@ export default class DisabledDate {
5858
const hasTo = u.isDefined(thisRange, 'to')
5959

6060
return (
61-
hasFrom && hasTo && date < thisRange.to && date > thisRange.from
61+
hasFrom && hasTo && date <= thisRange.to && date >= thisRange.from
6262
)
6363
})
6464
},

src/utils/HighlightedDate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ export default class HighlightedDate {
5050

5151
return {
5252
to: () => {
53-
return has.to && date < highlightedDates.to
53+
return has.to && date <= highlightedDates.to
5454
},
5555
from: () => {
56-
return has.from && date > highlightedDates.from
56+
return has.from && date >= highlightedDates.from
5757
},
5858
range: () => {
5959
if (!has.ranges) return false

test/e2e/specs/CellNavigation.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ Feature: Cell Navigation
4343

4444
Examples:
4545
| # | openDate | direction | view | toOrFrom | disabled | page | focusedCell |
46-
| 1 | 2020-01-01 | up | day | to | 2019-12-26 | previous | 26 |
47-
| 2 | 2020-01-31 | down | day | from | 2020-02-05 | next | 5 |
46+
| 1 | 2020-01-01 | up | day | to | 2019-12-25 | previous | 26 |
47+
| 2 | 2020-01-31 | down | day | from | 2020-02-06 | next | 5 |
4848
| 3 | 2020-01-01 | up | month | to | 2019-12-01 | previous | December |
4949
| 4 | 2019-12-31 | down | month | from | 2020-01-31 | next | January |
5050
| 5 | 2022-01-01 | up | year | to | 2019-01-01 | previous | 2019 |
@@ -59,8 +59,8 @@ Feature: Cell Navigation
5959

6060
Examples:
6161
| # | openDate | direction | view | toOrFrom | disabled | focusedCell |
62-
| 1 | 2020-01-15 | left | day | to | 2020-01-14 | 14 |
63-
| 2 | 2020-01-15 | right | day | from | 2020-01-16 | 16 |
62+
| 1 | 2020-01-15 | left | day | to | 2020-01-13 | 14 |
63+
| 2 | 2020-01-15 | right | day | from | 2020-01-17 | 16 |
6464
| 3 | 2020-06-01 | left | month | to | 2020-05-15 | May |
6565
| 4 | 2020-06-01 | right | month | from | 2020-07-15 | July |
6666
| 5 | 2023-01-01 | left | year | to | 2022-06-01 | 2022 |

0 commit comments

Comments
 (0)