Skip to content

Commit 3941f44

Browse files
- add logic to generate more days per month if displayHiddenDays flag is true;
- change datetime component to use this attribute when in theme: ionic; - document changes;
1 parent f6ce023 commit 3941f44

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

core/src/components/datetime/datetime.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ export class Datetime implements ComponentInterface {
21932193
</div>
21942194
);
21952195
}
2196-
private renderMonth(month: number, year: number) {
2196+
private renderMonth(month: number, year: number, theme: Theme) {
21972197
const { disabled, readonly } = this;
21982198

21992199
const yearAllowed = this.parsedYearValues === undefined || this.parsedYearValues.includes(year);
@@ -2234,10 +2234,35 @@ export class Datetime implements ComponentInterface {
22342234
}}
22352235
>
22362236
<div class="calendar-month-grid">
2237-
{getDaysOfMonth(month, year, this.firstDayOfWeek % 7).map((dateObject, index) => {
2238-
const { day, dayOfWeek } = dateObject;
2237+
{getDaysOfMonth(month, year, this.firstDayOfWeek % 7, theme === 'ionic').map((dateObject, index) => {
2238+
const { day, dayOfWeek, hiddenDay } = dateObject;
22392239
const { el, highlightedDates, isDateEnabled, multiple } = this;
2240-
const referenceParts = { month, day, year };
2240+
let _month = month;
2241+
let _year = year;
2242+
2243+
if(theme === 'ionic'){
2244+
if(hiddenDay && day !== null && day > 20) {
2245+
// Leading with the hidden day from the previous month
2246+
// if its a hidden day and is higher than '20' (last week even in feb)
2247+
if(month === 1) {
2248+
_year = year - 1;
2249+
_month = 12;
2250+
}else{
2251+
_month = month-1;
2252+
}
2253+
} else if(hiddenDay && day !== null && day < 15) {
2254+
// Leading with the hidden day from the next month
2255+
// if its a hidden day and is lower than '15' (first two weeks)
2256+
if(month === 12) {
2257+
_year = year + 1;
2258+
_month = 1;
2259+
} else {
2260+
_month = month + 1;
2261+
}
2262+
}
2263+
}
2264+
2265+
const referenceParts = { month:_month, day, year:_year };
22412266
const isCalendarPadding = day === null;
22422267
const {
22432268
isActive,
@@ -2258,7 +2283,7 @@ export class Datetime implements ComponentInterface {
22582283

22592284
const dateIsoString = convertDataToISO(referenceParts);
22602285

2261-
let isCalDayDisabled = isCalMonthDisabled || isDayDisabled;
2286+
let isCalDayDisabled = isCalMonthDisabled || isDayDisabled || hiddenDay;
22622287

22632288
if (!isCalDayDisabled && isDateEnabled !== undefined) {
22642289
try {
@@ -2292,7 +2317,7 @@ export class Datetime implements ComponentInterface {
22922317
* Custom highlight styles should not override the style for selected dates,
22932318
* nor apply to "filler days" at the start of the grid.
22942319
*/
2295-
if (highlightedDates !== undefined && !isActive && day !== null) {
2320+
if (highlightedDates !== undefined && !isActive && day !== null && !hiddenDay) {
22962321
dateStyle = getHighlightStyles(highlightedDates, dateIsoString, el);
22972322
}
22982323

@@ -2327,8 +2352,8 @@ export class Datetime implements ComponentInterface {
23272352
}}
23282353
tabindex="-1"
23292354
data-day={day}
2330-
data-month={month}
2331-
data-year={year}
2355+
data-month={_month}
2356+
data-year={_year}
23322357
data-index={index}
23332358
data-day-of-week={dayOfWeek}
23342359
disabled={isButtonDisabled}
@@ -2384,11 +2409,11 @@ export class Datetime implements ComponentInterface {
23842409
</div>
23852410
);
23862411
}
2387-
private renderCalendarBody() {
2412+
private renderCalendarBody(theme: Theme) {
23882413
return (
23892414
<div class="calendar-body ion-focusable" ref={(el) => (this.calendarBodyRef = el)} tabindex="0">
23902415
{generateMonths(this.workingParts, this.forceRenderDate).map(({ month, year }) => {
2391-
return this.renderMonth(month, year);
2416+
return this.renderMonth(month, year, theme);
23922417
})}
23932418
</div>
23942419
);
@@ -2397,7 +2422,7 @@ export class Datetime implements ComponentInterface {
23972422
return (
23982423
<div class="datetime-calendar" key="datetime-calendar">
23992424
{this.renderCalendarHeader(theme)}
2400-
{this.renderCalendarBody()}
2425+
{this.renderCalendarBody(theme)}
24012426
</div>
24022427
);
24032428
}

core/src/components/datetime/utils/data.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ export const getDaysOfWeek = (locale: string, theme: Theme, firstDayOfWeek = 0)
102102
* the firstDayOfWeek value (Sunday by default)
103103
* using null values.
104104
*/
105-
export const getDaysOfMonth = (month: number, year: number, firstDayOfWeek: number) => {
105+
export const getDaysOfMonth = (month: number, year: number, firstDayOfWeek: number, displayHiddenDays = false) => {
106106
const numDays = getNumDaysInMonth(month, year);
107+
let previousNumDays: number; //previous month number of days
108+
if (month === 1) { //If january the previous month should be january and the last year
109+
previousNumDays = getNumDaysInMonth(12, year-1);
110+
} else { //If not the previous month should be month -1 and the current year
111+
previousNumDays = getNumDaysInMonth(month - 1, year);
112+
}
113+
107114
const firstOfMonth = new Date(`${month}/1/${year}`).getDay();
108115

109116
/**
@@ -127,14 +134,38 @@ export const getDaysOfMonth = (month: number, year: number, firstDayOfWeek: numb
127134
*/
128135
const offset =
129136
firstOfMonth >= firstDayOfWeek ? firstOfMonth - (firstDayOfWeek + 1) : 6 - (firstDayOfWeek - firstOfMonth);
137+
let days: ({
138+
day: number;
139+
dayOfWeek: number;
140+
hiddenDay: boolean;
141+
} | {
142+
day: null;
143+
dayOfWeek: null;
144+
hiddenDay: boolean;
145+
})[] = [];
146+
for (let i = 1; i <= numDays; i++) {
147+
days.push({ day: i, dayOfWeek: (offset + i) % 7, hiddenDay: false });
148+
}
149+
150+
if (displayHiddenDays) {
151+
for (let i = 0; i <= offset; i++) {
152+
// Using offset create previous month hidden day, starting from last day
153+
days = [{ day: previousNumDays-i, dayOfWeek: (previousNumDays - i) % 7, hiddenDay:true }, ...days];
154+
}
130155

131-
let days = [];
132-
for (let i = 1; i <= numDays; i++) {
133-
days.push({ day: i, dayOfWeek: (offset + i) % 7 });
134-
}
156+
// Calculate positiveOffset
157+
// The calendar will display 42 days (6 rows of 7 columns)
158+
// Knowing this the offset is 41 (we start at index 0)
159+
// minus (the previous offset + the current month days)
160+
const positiveOffset = 41 - (numDays + offset);
161+
for (let i = 0; i < positiveOffset; i++) {
162+
days.push( {day:i+1, dayOfWeek:((numDays + offset) + i) % 7, hiddenDay: true} )
163+
}
135164

136-
for (let i = 0; i <= offset; i++) {
137-
days = [{ day: null, dayOfWeek: null }, ...days];
165+
} else {
166+
for (let i = 0; i <= offset; i++) {
167+
days = [{ day: null, dayOfWeek: null, hiddenDay:true }, ...days];
168+
}
138169
}
139170

140171
return days;

0 commit comments

Comments
 (0)