Skip to content

Commit

Permalink
feat: add past and future parts to cells in date picker calendar (#7691)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
FrediWa and tomivirkki authored Aug 29, 2024
1 parent dae5efe commit 8471108
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 131 deletions.
18 changes: 13 additions & 5 deletions packages/date-picker/src/vaadin-date-picker-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ export function getISOWeekNumber(date) {
return Math.floor(daysSinceFirstOfJanuary / 7 + 1);
}

/**
* Creates a new object with the same date, but sets the hours, minutes, seconds and milliseconds to 0.
*
* @param {Date} date
* @return {Date} The same date time elements set to 0.
*/
export function normalizeDate(date) {
const normalizedDate = new Date(date);
normalizedDate.setHours(0, 0, 0, 0);
return normalizedDate;
}

/**
* Check if two dates are equal.
*
Expand All @@ -45,11 +57,7 @@ export function getISOWeekNumber(date) {
*/
export function dateEquals(date1, date2) {
return (
date1 instanceof Date &&
date2 instanceof Date &&
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
date1 instanceof Date && date2 instanceof Date && normalizeDate(date1).getTime() === normalizeDate(date2).getTime()
);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/date-picker/src/vaadin-lit-month-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { html, LitElement } from 'lit';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { dateAllowed, dateEquals } from './vaadin-date-picker-helper.js';
import { dateAllowed, dateEquals, normalizeDate } from './vaadin-date-picker-helper.js';
import { MonthCalendarMixin } from './vaadin-month-calendar-mixin.js';
import { monthCalendarStyles } from './vaadin-month-calendar-styles.js';

Expand Down Expand Up @@ -62,13 +62,17 @@ class MonthCalendar extends MonthCalendarMixin(ThemableMixin(PolylitMixin(LitEle
const isFocused = dateEquals(date, this.focusedDate);
const isSelected = dateEquals(date, this.selectedDate);
const isDisabled = !dateAllowed(date, this.minDate, this.maxDate, this.isDateDisabled);
const greaterThanToday = date > normalizeDate(new Date());
const lessThanToday = date < normalizeDate(new Date());
const parts = [
'date',
isDisabled && 'disabled',
isFocused && 'focused',
isSelected && 'selected',
this._isToday(date) && 'today',
greaterThanToday && 'future',
lessThanToday && 'past',
].filter(Boolean);
return html`
Expand Down
12 changes: 11 additions & 1 deletion packages/date-picker/src/vaadin-month-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import '@polymer/polymer/lib/elements/dom-repeat.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { dateAllowed, dateEquals } from './vaadin-date-picker-helper.js';
import { dateAllowed, dateEquals, normalizeDate } from './vaadin-date-picker-helper.js';
import { MonthCalendarMixin } from './vaadin-month-calendar-mixin.js';
import { monthCalendarStyles } from './vaadin-month-calendar-styles.js';

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

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

if (lessThanToday) {
result.push('past');
}

if (greaterThanToday) {
result.push('future');
}

return result.join(' ');
}

Expand Down
Loading

0 comments on commit 8471108

Please sign in to comment.