Skip to content

Commit 8471108

Browse files
FrediWatomivirkki
andauthored
feat: add past and future parts to cells in date picker calendar (#7691)
* remove unit tests * address feedback * remove redundant normalizeDate * Update packages/date-picker/test/dom/month-calendar.test.js Co-authored-by: Tomi Virkki <tomivirkki@users.noreply.github.com> --------- Co-authored-by: Tomi Virkki <tomivirkki@users.noreply.github.com>
1 parent dae5efe commit 8471108

File tree

5 files changed

+163
-131
lines changed

5 files changed

+163
-131
lines changed

packages/date-picker/src/vaadin-date-picker-helper.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ export function getISOWeekNumber(date) {
3636
return Math.floor(daysSinceFirstOfJanuary / 7 + 1);
3737
}
3838

39+
/**
40+
* Creates a new object with the same date, but sets the hours, minutes, seconds and milliseconds to 0.
41+
*
42+
* @param {Date} date
43+
* @return {Date} The same date time elements set to 0.
44+
*/
45+
export function normalizeDate(date) {
46+
const normalizedDate = new Date(date);
47+
normalizedDate.setHours(0, 0, 0, 0);
48+
return normalizedDate;
49+
}
50+
3951
/**
4052
* Check if two dates are equal.
4153
*
@@ -45,11 +57,7 @@ export function getISOWeekNumber(date) {
4557
*/
4658
export function dateEquals(date1, date2) {
4759
return (
48-
date1 instanceof Date &&
49-
date2 instanceof Date &&
50-
date1.getFullYear() === date2.getFullYear() &&
51-
date1.getMonth() === date2.getMonth() &&
52-
date1.getDate() === date2.getDate()
60+
date1 instanceof Date && date2 instanceof Date && normalizeDate(date1).getTime() === normalizeDate(date2).getTime()
5361
);
5462
}
5563

packages/date-picker/src/vaadin-lit-month-calendar.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { html, LitElement } from 'lit';
77
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
88
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
99
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
10-
import { dateAllowed, dateEquals } from './vaadin-date-picker-helper.js';
10+
import { dateAllowed, dateEquals, normalizeDate } from './vaadin-date-picker-helper.js';
1111
import { MonthCalendarMixin } from './vaadin-month-calendar-mixin.js';
1212
import { monthCalendarStyles } from './vaadin-month-calendar-styles.js';
1313

@@ -62,13 +62,17 @@ class MonthCalendar extends MonthCalendarMixin(ThemableMixin(PolylitMixin(LitEle
6262
const isFocused = dateEquals(date, this.focusedDate);
6363
const isSelected = dateEquals(date, this.selectedDate);
6464
const isDisabled = !dateAllowed(date, this.minDate, this.maxDate, this.isDateDisabled);
65+
const greaterThanToday = date > normalizeDate(new Date());
66+
const lessThanToday = date < normalizeDate(new Date());
6567
6668
const parts = [
6769
'date',
6870
isDisabled && 'disabled',
6971
isFocused && 'focused',
7072
isSelected && 'selected',
7173
this._isToday(date) && 'today',
74+
greaterThanToday && 'future',
75+
lessThanToday && 'past',
7276
].filter(Boolean);
7377
7478
return html`

packages/date-picker/src/vaadin-month-calendar.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import '@polymer/polymer/lib/elements/dom-repeat.js';
77
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
88
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
99
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
10-
import { dateAllowed, dateEquals } from './vaadin-date-picker-helper.js';
10+
import { dateAllowed, dateEquals, normalizeDate } from './vaadin-date-picker-helper.js';
1111
import { MonthCalendarMixin } from './vaadin-month-calendar-mixin.js';
1212
import { monthCalendarStyles } from './vaadin-month-calendar-styles.js';
1313

@@ -110,6 +110,8 @@ class MonthCalendar extends MonthCalendarMixin(ThemableMixin(PolymerElement)) {
110110
// eslint-disable-next-line @typescript-eslint/max-params
111111
__getDatePart(date, focusedDate, selectedDate, minDate, maxDate, isDateDisabled) {
112112
const result = ['date'];
113+
const greaterThanToday = date > normalizeDate(new Date());
114+
const lessThanToday = date < normalizeDate(new Date());
113115

114116
if (this.__isDayDisabled(date, minDate, maxDate, isDateDisabled)) {
115117
result.push('disabled');
@@ -127,6 +129,14 @@ class MonthCalendar extends MonthCalendarMixin(ThemableMixin(PolymerElement)) {
127129
result.push('today');
128130
}
129131

132+
if (lessThanToday) {
133+
result.push('past');
134+
}
135+
136+
if (greaterThanToday) {
137+
result.push('future');
138+
}
139+
130140
return result.join(' ');
131141
}
132142

0 commit comments

Comments
 (0)