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(CalendarMonth): does not skip a month on arrow click #9722

Merged
merged 5 commits into from
Oct 18, 2023
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
61 changes: 18 additions & 43 deletions packages/react-core/src/components/CalendarMonth/CalendarMonth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,13 @@
const [isSelectOpen, setIsSelectOpen] = React.useState(false);

const getInitialDate = () => {
const initDate = new Date(dateProp);
if (isValidDate(initDate)) {
return initDate;
} else {
return isValidDate(rangeStart) ? rangeStart : today;
if (isValidDate(dateProp)) {
return dateProp;
}
if (isValidDate(rangeStart)) {
return rangeStart;
}
return today;
};
const initialDate = getInitialDate();
const [focusedDate, setFocusedDate] = React.useState(initialDate);
Expand All @@ -176,14 +177,14 @@
} else if (!dateProp) {
setFocusedDate(today);
}
}, [dateProp]);

Check warning on line 180 in packages/react-core/src/components/CalendarMonth/CalendarMonth.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'focusedDate'. Either include it or remove the dependency array

useEffect(() => {
// Calendar month should not be focused on page load
if ((shouldFocus || isDateFocused) && focusedDateValidated && focusRef.current) {
focusRef.current.focus();
}
}, [focusedDate, isDateFocused, focusedDateValidated, focusRef]);

Check warning on line 187 in packages/react-core/src/components/CalendarMonth/CalendarMonth.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'shouldFocus'. Either include it or remove the dependency array

const onMonthClick = (ev: React.MouseEvent, newDate: Date) => {
setFocusedDate(newDate);
Expand Down Expand Up @@ -211,43 +212,25 @@
}
};

const changeMonth = (month: number) => {
const newDate = new Date(focusedDate);
const desiredDay = newDate.getDate();
const monthDays = new Date(newDate.getFullYear(), (month + 1) % 12, 0).getDate(); // Setting day 0 of the next month returns the last day of current month

if (monthDays < desiredDay) {
newDate.setDate(monthDays);
}

newDate.setMonth(month);

if (initialDate.getDate() > desiredDay && monthDays > desiredDay) {
newDate.setDate(initialDate.getDate());
}
const changeYear = (newYear: number) => changeMonth(focusedDate.getMonth(), newYear);

return newDate;
};
const changeMonth = (newMonth: number, newYear?: number) =>
new Date(newYear ?? focusedDate.getFullYear(), newMonth, 1);

const addMonth = (toAdd: -1 | 1) => {
let newMonth = new Date(focusedDate).getMonth() + toAdd;
let newMonth = focusedDate.getMonth() + toAdd;
let newYear = focusedDate.getFullYear();

if (newMonth === -1) {
newMonth = 11;
newYear--;
} else if (newMonth === 12) {
newMonth = 0;
newYear++;
}
const newDate = changeMonth(newMonth);
if (toAdd === 1 && newMonth === 0) {
newDate.setFullYear(newDate.getFullYear() + 1);
}
if (toAdd === -1 && newMonth === 11) {
newDate.setFullYear(newDate.getFullYear() - 1);
}
return newDate;
};

const yearHasFebruary29th = (year: number) => new Date(year, 1, 29).getMonth() === 1;
const dateIsFebruary29th = (date: Date) => date.getMonth() === 1 && date.getDate() === 29;
return changeMonth(newMonth, newYear);
};

const prevMonth = addMonth(-1);
const nextMonth = addMonth(1);
Expand Down Expand Up @@ -340,19 +323,11 @@
type="number"
value={yearFormatted}
onChange={(ev: React.FormEvent<HTMLInputElement>, year: string) => {
const newDate = new Date(focusedDate);
if (dateIsFebruary29th(newDate) && !yearHasFebruary29th(+year)) {
newDate.setDate(28);
newDate.setMonth(1);
}
if (dateIsFebruary29th(initialDate) && yearHasFebruary29th(+year)) {
newDate.setFullYear(+year);
newDate.setDate(29);
}
newDate.setFullYear(+year);
const newDate = changeYear(Number(year));
setFocusedDate(newDate);
setHoveredDate(newDate);
setShouldFocus(false);
focusRef.current?.blur(); // will unfocus a date when changing year via up/down arrows
onMonthChange(ev, newDate);
}}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/react-core/src/helpers/datetimeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* @param {Date} date - A date to check the validity of
*/
export const isValidDate = (date: Date) => Boolean(date && !isNaN(date as any));
export const isValidDate = (date?: Date) => Boolean(date && !isNaN(date as any));
Loading