Skip to content

Commit 7f154c5

Browse files
showyduizendnegen
authored andcommitted
Enforce that the current date is between min/max date (adopted-ember-addons#121)
* The component can now react if the current date value is below the minDate or above the maxDate. - New component property enforceDateIntervals is used to activate this behavior - When `enforceDateIntervals` is `true` the component will set the pikaday date to minDate or maxDate (currentDate < minDate or currentDate > maxDate) and call `onSelection` with the new date. * Added documentation for enfoceDateIntervals option and behavior * Fixed documentation about enforceDateIntervals * Only call onSelection when enforceDateIntervals is true. We don't need to call pikaday.setDate * Updated documentation * Testing againts day instead of dates in tests for enforceDateIntervals * When enforceDateIntervals is active we call onSelection on the next sync queue to prevent reassigments when rendering * Removed unused code in the dummy application * Removed everything related to enforceDateIntervals. The component by default reacts to minDate and maxDate changes, and change the pikaday current date accordingly * Modified test names * Instead of calling onSelection to force the date to be between minDate and maxDate we call pikaday.setDate directly * Removed traces of enforceDateIntervals property used in some tests * Moved the complete logic of checking for minDate and maxDates outside interval to the queued function
1 parent 47decf7 commit 7f154c5

File tree

7 files changed

+69
-8
lines changed

7 files changed

+69
-8
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ env:
1414
- EMBER_TRY_SCENARIO=ember-release
1515
- EMBER_TRY_SCENARIO=ember-beta
1616
- EMBER_TRY_SCENARIO=ember-canary
17+
- EMBER_TRY_SCENARIO=ember-2.8.3
1718
- EMBER_TRY_SCENARIO=ember-2.4.3
1819
- EMBER_TRY_SCENARIO=ember-2.3.1
1920
- EMBER_TRY_SCENARIO=ember-2.2.2

addon/mixins/pikaday.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import moment from 'moment';
55
const {
66
isPresent,
77
run,
8+
getProperties
89
} = Ember;
10+
911
const assign = Ember.assign || Ember.merge;
1012

1113
export default Ember.Mixin.create({
14+
1215
_options: Ember.computed('options', 'i18n', {
1316
get() {
1417
let options = this._defaultOptions();
@@ -99,17 +102,35 @@ export default Ember.Mixin.create({
99102
},
100103

101104
setMinDate: function() {
102-
if (this.get('minDate')) {
105+
const { pikaday, minDate, value } = getProperties(this, [ 'pikaday', 'minDate', 'value' ]);
106+
107+
if (minDate) {
103108
run.later(() => {
104-
this.get('pikaday').setMinDate(this.get('minDate'));
109+
pikaday.setMinDate(minDate);
110+
});
111+
112+
// If the current date is lower than minDate we set date to minDate
113+
run.schedule('sync', () => {
114+
if (value < minDate) {
115+
pikaday.setDate(minDate);
116+
}
105117
});
106118
}
107119
},
108120

109121
setMaxDate: function() {
110-
if (this.get('maxDate')) {
122+
const { pikaday, maxDate, value } = getProperties(this, [ 'pikaday', 'maxDate', 'value' ]);
123+
124+
if (maxDate) {
111125
run.later(() => {
112-
this.get('pikaday').setMaxDate(this.get('maxDate'));
126+
pikaday.setMaxDate(maxDate);
127+
});
128+
129+
// If the current date is greater than maxDate we set date to maxDate
130+
run.schedule('sync', () => {
131+
if (value > maxDate) {
132+
pikaday.setDate(maxDate);
133+
}
113134
});
114135
}
115136
},

config/ember-try.js

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ module.exports = {
3939
}
4040
}
4141
},
42+
{
43+
name: 'ember-2.8.3',
44+
bower: {
45+
dependencies: {
46+
'ember': '2.8.3'
47+
},
48+
resolutions: {
49+
'ember': '2.8.3'
50+
}
51+
}
52+
},
4253
{
4354
name: 'ember-2.4.3',
4455
bower: {

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module.exports = {
55
name: 'ember-pikaday',
6-
6+
77
options: {
88
nodeAssets: {
99
pikaday: {

tests/dummy/app/controllers/application.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export default Ember.Controller.extend({
99
doSomethingWithSelectedValue(value) {
1010
console.log(value);
1111
}
12-
}
13-
});
12+
},
13+
});

tests/dummy/app/templates/application.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Datepicker already disabled before it's rendered:
2121

2222
Inputless datepicker:
2323
{{pikaday-inputless onSelection=(action (mut someDate))}}
24-
{{someDate}}
24+
{{someDate}}

tests/integration/components/pikaday-input-test.js

+28
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,31 @@ test('if updates pikaday config if options hash is changed', function(assert) {
388388
openDatepicker(this.$('input'));
389389
assert.notOk($(`td[data-day=${weekendDay}]`).hasClass('is-disabled'));
390390
});
391+
392+
test('if minDate is greater than value we we set pikaday\'s current date to minDate' ,function(assert) {
393+
assert.expect(1);
394+
395+
let today = new Date();
396+
let tomorrow = new Date( Date.now() + (60 * 60 * 24 * 1000));
397+
398+
this.set('currentDate', today);
399+
this.set('minDate', today);
400+
this.render(hbs`{{pikaday-input minDate=minDate value=currentDate onSelection=(action (mut currentDate)) }}`);
401+
402+
this.set('minDate', tomorrow);
403+
assert.equal(this.get('currentDate').getDate(), tomorrow.getDate(), 'value should change');
404+
});
405+
406+
test('if maxDate is lower than value we set pikaday\'s current date to maxDate', function(assert) {
407+
assert.expect(1);
408+
409+
let today = new Date();
410+
let tomorrow = new Date( Date.now() + (60 * 60 * 24 * 1000));
411+
412+
this.set('currentDate', tomorrow);
413+
this.set('maxDate', tomorrow);
414+
this.render(hbs`{{pikaday-input maxDate=maxDate value=currentDate onSelection=(action (mut currentDate)) }}`);
415+
416+
this.set('maxDate', today);
417+
assert.equal(this.get('currentDate').getDate(), today.getDate(), 'value should change');
418+
});

0 commit comments

Comments
 (0)