Skip to content

Commit

Permalink
Merge pull request #33355 from ishpaul777/fix/33006
Browse files Browse the repository at this point in the history
Fix/33006
  • Loading branch information
danieldoglas authored Jan 8, 2024
2 parents 206ba4c + f46f2b7 commit b055d6f
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/components/DatePicker/CalendarPicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,44 @@ class CalendarPicker extends React.PureComponent {
* Handles the user pressing the previous month arrow of the calendar picker.
*/
moveToPrevMonth() {
this.setState((prev) => ({currentDateView: subMonths(new Date(prev.currentDateView), 1)}));
this.setState((prev) => {
const prevMonth = subMonths(new Date(prev.currentDateView), 1);
// if year is subtracted, we need to update the years list
let newYears = prev.years;
if (prevMonth.getFullYear() < prev.currentDateView.getFullYear()) {
newYears = _.map(prev.years, (item) => ({
...item,
isSelected: item.value === prevMonth.getFullYear(),
}));
}

return {
currentDateView: prevMonth,
years: newYears,
};
});
}

/**
* Handles the user pressing the next month arrow of the calendar picker.
*/
moveToNextMonth() {
this.setState((prev) => ({currentDateView: addMonths(new Date(prev.currentDateView), 1)}));
this.setState((prev) => {
const nextMonth = addMonths(new Date(prev.currentDateView), 1);
// if year is added, we need to update the years list
let newYears = prev.years;
if (nextMonth.getFullYear() > prev.currentDateView.getFullYear()) {
newYears = _.map(prev.years, (item) => ({
...item,
isSelected: item.value === nextMonth.getFullYear(),
}));
}

return {
currentDateView: nextMonth,
years: newYears,
};
});
}

render() {
Expand Down

0 comments on commit b055d6f

Please sign in to comment.