Skip to content

Commit b0c470b

Browse files
authored
Merge pull request #593 from teamanvyl/fix/do-not-automatically-restrict-values-to-min-or-max-dates
fix: Prevent ember-pikaday from automatically updating values to within a min/max date bounds
2 parents f6e8637 + 696c65f commit b0c470b

File tree

3 files changed

+14
-78
lines changed

3 files changed

+14
-78
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ All other named arguments are passed directly to Pikaday, so see [Pikaday's conf
5959

6060
The only behaviors this modifier adds to the stock Pikaday are:
6161

62-
- if you set `minDate` or `maxDate` and that causes `value` to be outside the legal range, we adjust `value` and fire `onSelect` to inform you of the change
6362
- if you set your `<input>` element's `disabled` attribute we will close Pikaday if it had been open.
6463

6564
### &lt;PikadayInput&gt; Component

src/modifiers/pikaday.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,9 @@ export default class PikadayModifier extends Modifier {
5454
this.#observer.observe(element, { attributes: true });
5555
register?.(this.#pikaday);
5656
} else {
57-
let { value, minDate, maxDate } = named;
58-
let valueAltered = false;
59-
this.#pikaday.setMinDate(copyDate(minDate));
60-
if (minDate && value && value < minDate) {
61-
value = minDate;
62-
valueAltered = true;
63-
}
64-
65-
this.#pikaday.setMaxDate(copyDate(maxDate));
66-
if (maxDate && value && value > maxDate) {
67-
value = maxDate;
68-
valueAltered = true;
69-
}
57+
let { value } = named;
7058

71-
this.#pikaday.setDate(value, !valueAltered);
59+
this.#pikaday.setDate(value, true);
7260
this.#pikaday.config(pikadayOptions);
7361
}
7462
}
@@ -79,12 +67,3 @@ export default class PikadayModifier extends Modifier {
7967
}
8068
}
8169
}
82-
83-
// apparently Pikaday mutates Dates that you pass it for minDate and maxDate. <sigh>
84-
function copyDate(date) {
85-
if (date) {
86-
return new Date(date.getTime());
87-
} else {
88-
return date;
89-
}
90-
}

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

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,12 @@ module('Integration | Component | pikaday-input', function (hooks) {
253253
this.set('tomorrow', new Date(2010, 7, 9));
254254
await settled();
255255

256-
assert.dom('input').hasValue('09.08.2010');
256+
assert
257+
.dom('input')
258+
.hasValue(
259+
'10.08.2010',
260+
'we do not manipulate the value even if it violates minDate'
261+
);
257262
});
258263

259264
test('set new date value with a new max date', async function (assert) {
@@ -267,7 +272,12 @@ module('Integration | Component | pikaday-input', function (hooks) {
267272
this.set('tomorrow', new Date(2010, 7, 11));
268273
await settled();
269274

270-
assert.dom('input').hasValue('11.08.2010');
275+
assert
276+
.dom('input')
277+
.hasValue(
278+
'10.08.2010',
279+
'we do not manipulate the value, even if it violates the maxDate'
280+
);
271281
});
272282

273283
test('theme option adds theme as CSS class to DOM element', async function (assert) {
@@ -592,58 +602,6 @@ module('Integration | Component | pikaday-input', function (hooks) {
592602
assert.dom(disabledWeekendCell).doesNotHaveClass('is-disabled');
593603
});
594604

595-
test("if minDate is greater than value we set pikaday's current date to minDate", async function (assert) {
596-
assert.expect(1);
597-
598-
const today = new Date();
599-
const tomorrow = new Date(Date.now() + 60 * 60 * 24 * 1000);
600-
601-
this.set('currentDate', today);
602-
this.set('minDate', today);
603-
this.set('updateCurrentDate', (date) => {
604-
this.set('currentDate', date);
605-
});
606-
607-
await render(hbs`
608-
<PikadayInput @minDate={{this.minDate}} @value={{this.currentDate}} @onSelection={{this.updateCurrentDate}}/>
609-
`);
610-
611-
this.set('minDate', tomorrow);
612-
await settled();
613-
614-
assert.equal(
615-
this.currentDate.getDate(),
616-
tomorrow.getDate(),
617-
'value should change'
618-
);
619-
});
620-
621-
test("if maxDate is lower than value we set pikaday's current date to maxDate", async function (assert) {
622-
assert.expect(1);
623-
624-
const today = new Date();
625-
const tomorrow = new Date(Date.now() + 60 * 60 * 24 * 1000);
626-
627-
this.set('currentDate', tomorrow);
628-
this.set('maxDate', tomorrow);
629-
this.set('updateCurrentDate', (date) => {
630-
this.set('currentDate', date);
631-
});
632-
633-
await render(hbs`
634-
<PikadayInput @maxDate={{this.maxDate}} @value={{this.currentDate}} @onSelection={{this.updateCurrentDate}}/>
635-
`);
636-
637-
this.set('maxDate', today);
638-
await settled();
639-
640-
assert.equal(
641-
this.currentDate.getDate(),
642-
today.getDate(),
643-
'value should change'
644-
);
645-
});
646-
647605
test("if value is null we don't enforce minDate or maxDate", async function (assert) {
648606
assert.expect(1);
649607

0 commit comments

Comments
 (0)