Skip to content

Commit 1af80de

Browse files
Merge pull request charliekassel#322 from cybrilla/master
Adds options to disable and highlight dates using fuctions
2 parents 4b085f4 + d4a1080 commit 1af80de

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,17 @@ var state = {
162162
}, {
163163
from: new Date(2017, 1, 12),
164164
to: new Date(2017, 2, 25)
165-
}]
165+
}],
166+
// a custom function that returns true if the date is disabled
167+
// this can be used for wiring you own logic to disable a date if none
168+
// of the above conditions serve your purpose
169+
// this function should accept a date and return true if is disabled
170+
customPredictor: function(date) {
171+
// disables the date if it is a multiple of 5
172+
if(date.getDate() % 5 == 0){
173+
return true
174+
}
175+
}
166176
}
167177
}
168178
</script>
@@ -185,7 +195,17 @@ var state = {
185195
new Date(2016, 9, 16),
186196
new Date(2016, 9, 17),
187197
new Date(2016, 9, 18)
188-
]
198+
],
199+
// a custom function that returns true of the date is highlighted
200+
// this can be used for wiring you own logic to highlight a date if none
201+
// of the above conditions serve your purpose
202+
// this function should accept a date and return true if is highlighted
203+
customPredictor: function(date) {
204+
// highlights the date if it is a multiple of 4
205+
if(date.getDate() % 4 == 0){
206+
return true
207+
}
208+
}
189209
}
190210
}
191211
</script>

src/Demo.vue

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,55 @@
7070
</div>
7171

7272
<div class="example">
73-
<h3>Highlighting Dates</h3>
73+
<div class="settings">
74+
<h5>Settings</h5>
75+
<div class="form-group">
76+
<label>Disabled Function:</label>
77+
</div>
78+
<pre>
79+
disabled: {
80+
customPredictor: function (date) {
81+
// disables every day of a month which is a multiple of 3
82+
if (date.getDate() % 3 === 0) {
83+
return true
84+
}
85+
}
86+
}
87+
</pre>
88+
<h5>Resulting Date picker</h5>
89+
<datepicker :disabled="disabledFn"></datepicker>
90+
</div>
91+
</div>
92+
93+
<div class="example">
94+
<h3>Highlighting Dates Matching Given Function</h3>
7495
<datepicker :disabled="disabled"></datepicker>
7596
<code>
7697
&lt;datepicker :highlighted="highlighted"&gt;&lt;/datepicker&gt;
7798
</code>
99+
<div class="settings">
100+
<h5>Settings</h5>
101+
<pre>
102+
highlighted: {
103+
customPredictor: function (date) {
104+
// highlights every day of a month which is a multiple of 4
105+
if (date.getDate() % 4 === 0) {
106+
return true
107+
}
108+
}
109+
}
110+
</pre>
111+
112+
<h5>Resulting Date picker</h5>
113+
<datepicker :highlighted="highlightedFn"></datepicker>
114+
</div>
115+
</div>
116+
117+
<div class="example">
118+
<h3>Highlighting Dates</h3>
119+
<code>
120+
&lt;datepicker :highlighted="highlighted"&gt;&lt;/datepicker&gt;
121+
</code>
78122
<div class="settings">
79123
<h5>Settings</h5>
80124
<div class="form-group">
@@ -163,6 +207,20 @@ export default {
163207
return {
164208
format: 'd MMMM yyyy',
165209
disabled: {},
210+
disabledFn: {
211+
customPredictor (date) {
212+
if (date.getDate() % 3 === 0) {
213+
return true
214+
}
215+
}
216+
},
217+
highlightedFn: {
218+
customPredictor (date) {
219+
if (date.getDate() % 4 === 0) {
220+
return true
221+
}
222+
}
223+
},
166224
highlighted: {},
167225
eventMsg: null,
168226
state: state,

src/components/Datepicker.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,9 @@ export default {
566566
if (typeof this.disabled.daysOfMonth !== 'undefined' && this.disabled.daysOfMonth.indexOf(date.getDate()) !== -1) {
567567
disabled = true
568568
}
569+
if (typeof this.disabled.customPredictor === 'function' && this.disabled.customPredictor(date)) {
570+
disabled = true
571+
}
569572
return disabled
570573
},
571574
/**
@@ -605,6 +608,10 @@ export default {
605608
highlighted = true
606609
}
607610
611+
if (typeof this.highlighted.customPredictor === 'function' && this.highlighted.customPredictor(date)) {
612+
highlighted = true
613+
}
614+
608615
return highlighted
609616
},
610617
/**

test/unit/specs/Datepicker.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,22 @@ describe('Datepicker has disabled dates but can change dates', () => {
445445
expect(vm.isDisabledDate(new Date(2016, 10, 30))).to.equal(true)
446446
expect(vm.isDisabledDate(new Date(2016, 9, 11))).to.equal(false)
447447
})
448+
449+
it('can accept a customPredictor to check if the date is disabled', () => {
450+
vm = getViewModel(Datepicker, {
451+
disabled: {
452+
customPredictor (date) {
453+
if (date.getDate() % 4 === 0) {
454+
return true
455+
}
456+
}
457+
}
458+
})
459+
expect(vm.isDisabledDate(new Date(2016, 8, 29))).to.equal(false)
460+
expect(vm.isDisabledDate(new Date(2016, 9, 28))).to.equal(true)
461+
expect(vm.isDisabledDate(new Date(2016, 10, 24))).to.equal(true)
462+
expect(vm.isDisabledDate(new Date(2016, 9, 11))).to.equal(false)
463+
})
448464
})
449465

450466
describe('Datepicker highlight date', () => {
@@ -524,6 +540,22 @@ describe('Datepicker highlight date', () => {
524540
expect(vm.isHighlightedDate(new Date(2016, 7, 20))).to.equal(false)
525541
})
526542

543+
it('can accept a customPredictor to check if the date is highlighted', () => {
544+
vm = getViewModel(Datepicker, {
545+
highlighted: {
546+
customPredictor (date) {
547+
if (date.getDate() % 5 === 0) {
548+
return true
549+
}
550+
}
551+
}
552+
})
553+
expect(vm.isHighlightedDate(new Date(2016, 8, 30))).to.equal(true)
554+
expect(vm.isHighlightedDate(new Date(2016, 9, 28))).to.equal(false)
555+
expect(vm.isHighlightedDate(new Date(2016, 10, 20))).to.equal(true)
556+
expect(vm.isHighlightedDate(new Date(2016, 9, 11))).to.equal(false)
557+
})
558+
527559
it('should detect the first date of the highlighted dates', () => {
528560
expect(vm.isHighlightStart(new Date(2016, 12, 4))).to.equal(true)
529561
expect(vm.isHighlightStart(new Date(2016, 12, 3))).to.equal(false)

0 commit comments

Comments
 (0)