Skip to content

Commit 81165c4

Browse files
committed
fix(datepicker): Allow multiple highlighted ranges
1 parent f801b8b commit 81165c4

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/utils/HighlightedDate.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class HighlightedDate {
2323
daysOfMonth: utils.hasArray(highlightedDates, 'daysOfMonth'),
2424
daysOfWeek: utils.hasArray(highlightedDates, 'days'),
2525
from: utils.hasDate(highlightedDates, 'from'),
26+
ranges: utils.hasArray(highlightedDates, 'ranges'),
2627
specificDates: utils.hasArray(highlightedDates, 'dates'),
2728
to: utils.hasDate(highlightedDates, 'to'),
2829
includeDisabled:
@@ -53,10 +54,25 @@ export default class HighlightedDate {
5354

5455
return {
5556
to: () => {
56-
return has.to && date <= highlightedDates.to
57+
return has.to && date < highlightedDates.to
5758
},
5859
from: () => {
59-
return has.from && date >= highlightedDates.from
60+
return has.from && date > highlightedDates.from
61+
},
62+
range: () => {
63+
if (!has.ranges) return false
64+
65+
const { ranges } = highlightedDates
66+
const u = makeCellUtils(this._utils)
67+
68+
return ranges.some((thisRange) => {
69+
const hasFrom = u.isDefined(thisRange, 'from')
70+
const hasTo = u.isDefined(thisRange, 'to')
71+
72+
return (
73+
hasFrom && hasTo && date <= thisRange.to && date >= thisRange.from
74+
)
75+
})
6076
},
6177
customPredictor: () => {
6278
return has.customPredictor && highlightedDates.customPredictor(date)

test/unit/specs/PickerDay/highlightedDates.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ describe('PickerDay mounted', () => {
5555
expect(wrapper.vm.isHighlightedDate(new Date(2016, 9, 3))).toEqual(false)
5656
})
5757

58+
it('accepts an array of highlighted dates in a range', async () => {
59+
await wrapper.setProps({
60+
highlighted: {
61+
ranges: [
62+
{
63+
from: new Date(2005, 6, 5),
64+
to: new Date(2016, 9, 4),
65+
},
66+
{
67+
from: new Date(2016, 9, 26),
68+
to: new Date(2030, 12, 25),
69+
},
70+
],
71+
},
72+
})
73+
74+
expect(wrapper.vm.isHighlightedDate(new Date(2006, 9, 2))).toEqual(true)
75+
expect(wrapper.vm.isHighlightedDate(new Date(2026, 9, 2))).toEqual(true)
76+
})
77+
5878
it('accepts an array of highlighted days of the week', async () => {
5979
await wrapper.setProps({
6080
highlighted: {

0 commit comments

Comments
 (0)