fix(calendar): normalize dates for min/max date comparison
This commit is contained in:
parent
fe72ebd055
commit
afddc0ec3c
3 changed files with 28 additions and 4 deletions
|
|
@ -331,7 +331,7 @@ export class LionCalendar extends LocalizeMixin(LitElement) {
|
||||||
|
|
||||||
__coreDayPreprocessor(_day, { currentMonth = false } = {}) {
|
__coreDayPreprocessor(_day, { currentMonth = false } = {}) {
|
||||||
const day = createDay(new Date(_day.date), _day);
|
const day = createDay(new Date(_day.date), _day);
|
||||||
const today = new Date();
|
const today = normalizeDateTime(new Date());
|
||||||
day.central = isSameDate(day.date, this.centralDate);
|
day.central = isSameDate(day.date, this.centralDate);
|
||||||
day.previousMonth = currentMonth && day.date.getMonth() < currentMonth.getMonth();
|
day.previousMonth = currentMonth && day.date.getMonth() < currentMonth.getMonth();
|
||||||
day.currentMonth = currentMonth && day.date.getMonth() === currentMonth.getMonth();
|
day.currentMonth = currentMonth && day.date.getMonth() === currentMonth.getMonth();
|
||||||
|
|
@ -342,11 +342,11 @@ export class LionCalendar extends LocalizeMixin(LitElement) {
|
||||||
day.future = day.date > today;
|
day.future = day.date > today;
|
||||||
day.disabled = this.disableDates(day.date);
|
day.disabled = this.disableDates(day.date);
|
||||||
|
|
||||||
if (this.minDate && day.date < this.minDate) {
|
if (this.minDate && normalizeDateTime(day.date) < normalizeDateTime(this.minDate)) {
|
||||||
day.disabled = true;
|
day.disabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.maxDate && day.date > this.maxDate) {
|
if (this.maxDate && normalizeDateTime(day.date) > normalizeDateTime(this.maxDate)) {
|
||||||
day.disabled = true;
|
day.disabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @desc Makes suitable for date comparisons
|
* @desc Makes suitable for date comparisons
|
||||||
* @param {Date} d
|
* @param {Date} d
|
||||||
|
|
|
||||||
|
|
@ -354,6 +354,31 @@ describe('<lion-calendar>', () => {
|
||||||
// dates in the month view will be derived from.
|
// dates in the month view will be derived from.
|
||||||
expect(isNormalizedDate(el.centralDate)).to.be.true;
|
expect(isNormalizedDate(el.centralDate)).to.be.true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('normalizes dates in date comparisons', async () => {
|
||||||
|
const selectedDate = new Date('2000-11-04T03:00:00');
|
||||||
|
// without normalization, selectedDate > maxDate would wrongfully be disabled
|
||||||
|
const maxDate = new Date('2000-11-29T02:00:00');
|
||||||
|
// without normalization, selectedDate < minDate would wrongfully be disabled
|
||||||
|
const minDate = new Date('2000-11-02T04:00:00');
|
||||||
|
|
||||||
|
const el = await fixture(
|
||||||
|
html`
|
||||||
|
<lion-calendar
|
||||||
|
.selectedDate="${selectedDate}"
|
||||||
|
.minDate="${minDate}"
|
||||||
|
.maxDate="${maxDate}"
|
||||||
|
></lion-calendar>
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
const elObj = new CalendarObject(el);
|
||||||
|
|
||||||
|
expect(elObj.getDayObj(29).isDisabled).to.equal(false);
|
||||||
|
expect(elObj.getDayObj(30).isDisabled).to.equal(true);
|
||||||
|
|
||||||
|
expect(elObj.getDayObj(2).isDisabled).to.equal(false);
|
||||||
|
expect(elObj.getDayObj(1).isDisabled).to.equal(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue