Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add past and future parts to cells in date picker calendar #7691

Merged
merged 4 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading