Skip to content

Commit 075e023

Browse files
sandydooduizendnegen
authored andcommitted
Set minDate and maxDate using a copy to avoid modifying original date (adopted-ember-addons#145)
Fixes adopted-ember-addons#138
1 parent e2b363a commit 075e023

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

addon/mixins/pikaday.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ export default Ember.Mixin.create({
106106

107107
if (minDate) {
108108
run.later(() => {
109-
pikaday.setMinDate(minDate);
109+
const _minDate = new Date(minDate.getTime());
110+
pikaday.setMinDate(_minDate);
110111
});
111112

112113
// If the current date is lower than minDate we set date to minDate
@@ -123,7 +124,8 @@ export default Ember.Mixin.create({
123124

124125
if (maxDate) {
125126
run.later(() => {
126-
pikaday.setMaxDate(maxDate);
127+
const _maxDate = new Date(maxDate.getTime());
128+
pikaday.setMaxDate(_maxDate);
127129
});
128130

129131
// If the current date is greater than maxDate we set date to maxDate

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ test('if updates pikaday config if options hash is changed', function(assert) {
399399
assert.notOk($(`td[data-day=${weekendDay}]`).hasClass('is-disabled'));
400400
});
401401

402-
test('if minDate is greater than value we we set pikaday\'s current date to minDate' ,function(assert) {
402+
test('if minDate is greater than value we set pikaday\'s current date to minDate', function(assert) {
403403
assert.expect(1);
404404

405405
let today = new Date();
@@ -440,3 +440,20 @@ test('if value is null we don\'t enforce minDate or maxDate', function(assert) {
440440
run(() => this.set('minDate', today));
441441
assert.equal(this.get('currentDate'), null, 'value should be null');
442442
});
443+
444+
test('the original date passed to minDate or maxDate is not modified by pikaday', function(assert) {
445+
assert.expect(2);
446+
447+
let today = new Date();
448+
let todayCopy = new Date(today);
449+
let tomorrow = new Date(Date.now() + (60 * 60 * 24 * 1000));
450+
let tomorrowCopy = new Date(tomorrow);
451+
452+
this.render(hbs`{{pikaday-input minDate=minDate maxDate=maxDate value=today}}`);
453+
454+
run(() => this.set('minDate', today));
455+
run(() => this.set('maxDate', tomorrow));
456+
457+
assert.equal(today.toISOString(), todayCopy.toISOString(), 'value should not change');
458+
assert.equal(tomorrow.toISOString(), tomorrowCopy.toISOString(), 'value should not change');
459+
});

0 commit comments

Comments
 (0)