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

fix(calendar): improve calendar types of custom date format #17273

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions packages/primeng/src/calendar/calendar.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ export interface LocaleSettings {
*/
weekHeader?: string;
}

/**
* Custom date format for a single day for the calendar display
* @group Interface
*/
export interface CalendarDay {
day: number;
month: number;
year: number;
selectable: boolean;
today?: boolean;
otherMonth?: boolean;
}

/**
* An array of days that contains date values in a custom format
* @group Types
*/
export type CalendarWeek = CalendarDay[];

/**
* Month interface.
* @group Interface
Expand All @@ -133,9 +153,9 @@ export interface Month {
*/
year?: number;
/**
* Array of dates.
* Array of weeks, each containing an array of days.
*/
dates?: Date[];
dates?: CalendarWeek[];
/**
* Array of week numbers.
*/
Expand Down
33 changes: 14 additions & 19 deletions packages/primeng/src/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { Ripple } from 'primeng/ripple';
import { Nullable, VoidListener } from 'primeng/ts-helpers';
import { ZIndexUtils } from 'primeng/utils';
import { Subscription } from 'rxjs';
import { CalendarMonthChangeEvent, CalendarResponsiveOptions, CalendarTypeView, CalendarYearChangeEvent, LocaleSettings, Month, NavigationState } from './calendar.interface';
import { CalendarDay, CalendarMonthChangeEvent, CalendarResponsiveOptions, CalendarTypeView, CalendarWeek, CalendarYearChangeEvent, LocaleSettings, Month, NavigationState } from './calendar.interface';
import { CalendarStyle } from './style/calendarstyle';

export const CALENDAR_VALUE_ACCESSOR: any = {
Expand Down Expand Up @@ -1488,7 +1488,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
}

createMonth(month: number, year: number): Month {
let dates = [];
let dates: CalendarWeek[] = [];
let firstDay = this.getFirstDayOfMonthIndex(month, year);
let daysLength = this.getDaysCountInMonth(month, year);
let prevMonthDaysLength = this.getDaysCountInPrevMonth(month, year);
Expand All @@ -1498,7 +1498,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
let monthRows = Math.ceil((daysLength + firstDay) / 7);

for (let i = 0; i < monthRows; i++) {
let week = [];
let week: CalendarDay[] = [];

if (i == 0) {
for (let j = prevMonthDaysLength - firstDay + 1; j <= prevMonthDaysLength; j++) {
Expand Down Expand Up @@ -1557,12 +1557,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
dates.push(week);
}

return {
month: month,
year: year,
dates: <any>dates,
weekNumbers: weekNumbers
};
return { month, year, dates, weekNumbers };
}

initTime(date: Date) {
Expand Down Expand Up @@ -1679,7 +1674,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
event.preventDefault();
}

onDateSelect(event: Event, dateMeta: any) {
onDateSelect(event: Event, dateMeta: CalendarDay) {
if (this.disabled || !dateMeta.selectable) {
event.preventDefault();
return;
Expand All @@ -1694,7 +1689,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
}
this.updateModel(this.value);
} else {
if (this.shouldSelectDate(dateMeta)) {
if (this.shouldSelectDate()) {
this.selectDate(dateMeta);
}
}
Expand All @@ -1716,7 +1711,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
event.preventDefault();
}

shouldSelectDate(dateMeta: any) {
shouldSelectDate() {
if (this.isMultipleSelection()) return this.maxDateCount != null ? this.maxDateCount > (this.value ? this.value.length : 0) : true;
else return true;
}
Expand Down Expand Up @@ -1796,7 +1791,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
return formattedValue;
}

formatDateMetaToDate(dateMeta: any): Date {
formatDateMetaToDate(dateMeta: CalendarDay): Date {
return new Date(dateMeta.year, dateMeta.month, dateMeta.day);
}

Expand All @@ -1823,7 +1818,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
this.alignOverlay();
}

selectDate(dateMeta: any) {
selectDate(dateMeta: CalendarDay) {
let date = this.formatDateMetaToDate(dateMeta);

if (this.showTime) {
Expand Down Expand Up @@ -1948,7 +1943,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
return firstDayOfWeek > 0 ? 7 - firstDayOfWeek : 0;
}

isSelected(dateMeta: any): boolean | undefined {
isSelected(dateMeta: CalendarDay): boolean | undefined {
if (this.value) {
if (this.isSingleSelection()) {
return this.isDateEquals(this.value, dateMeta);
Expand Down Expand Up @@ -2011,12 +2006,12 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
return false;
}

isDateEquals(value: any, dateMeta: any) {
isDateEquals(value: any, dateMeta: CalendarDay) {
if (value && isDate(value)) return value.getDate() === dateMeta.day && value.getMonth() === dateMeta.month && value.getFullYear() === dateMeta.year;
else return false;
}

isDateBetween(start: Date, end: Date, dateMeta: any) {
isDateBetween(start: Date, end: Date, dateMeta: CalendarDay) {
let between: boolean = false;
if (isDate(start) && isDate(end)) {
let date: Date = this.formatDateMetaToDate(dateMeta);
Expand Down Expand Up @@ -2236,7 +2231,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,
}
}

onDateCellKeydown(event: any, dateMeta: any, groupIndex: number) {
onDateCellKeydown(event: any, dateMeta: CalendarDay, groupIndex: number) {
const cellContent = event.currentTarget;
const cell = cellContent.parentElement;
const currentDate = this.formatDateMetaToDate(dateMeta);
Expand Down Expand Up @@ -3682,7 +3677,7 @@ export class Calendar extends BaseComponent implements OnInit, AfterContentInit,

onTodayButtonClick(event: any) {
const date: Date = new Date();
const dateMeta = {
const dateMeta: CalendarDay = {
day: date.getDate(),
month: date.getMonth(),
year: date.getFullYear(),
Expand Down
Loading