Skip to content

Commit

Permalink
fix: #4401 monthsShown with MonthYearPicker or QuarterYearPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki0410-dev committed Mar 16, 2024
1 parent 8ef2eef commit 6ca7ea7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -843,13 +843,15 @@ export default class Calendar extends React.Component {
);
};

renderYearHeader = () => {
const { date } = this.state;
renderYearHeader = ({ monthDate }) => {
const { showYearPicker, yearItemNumber } = this.props;
const { startPeriod, endPeriod } = getYearsPeriod(date, yearItemNumber);
const { startPeriod, endPeriod } = getYearsPeriod(
monthDate,
yearItemNumber,
);
return (
<div className="react-datepicker__header react-datepicker-year-header">
{showYearPicker ? `${startPeriod} - ${endPeriod}` : getYear(date)}
{showYearPicker ? `${startPeriod} - ${endPeriod}` : getYear(monthDate)}
</div>
);
};
Expand All @@ -876,11 +878,17 @@ export default class Calendar extends React.Component {
const monthsToSubtract = this.props.showPreviousMonths
? this.props.monthsShown - 1
: 0;
const fromMonthDate = subMonths(this.state.date, monthsToSubtract);
const fromMonthDate =
this.props.showMonthYearPicker || this.props.showQuarterYearPicker
? addYears(this.state.date, monthsToSubtract)
: subMonths(this.state.date, monthsToSubtract);
const monthSelectedIn = this.props.monthSelectedIn ?? monthsToSubtract;
for (let i = 0; i < this.props.monthsShown; ++i) {
const monthsToAdd = i - monthSelectedIn + monthsToSubtract;
const monthDate = addMonths(fromMonthDate, monthsToAdd);
const monthDate =
this.props.showMonthYearPicker || this.props.showQuarterYearPicker
? addYears(fromMonthDate, monthsToAdd)
: addMonths(fromMonthDate, monthsToAdd);
const monthKey = `month-${i}`;
const monthShowsDuplicateDaysEnd = i < this.props.monthsShown - 1;
const monthShowsDuplicateDaysStart = i > 0;
Expand Down Expand Up @@ -975,7 +983,7 @@ export default class Calendar extends React.Component {
if (this.props.showYearPicker) {
return (
<div className="react-datepicker__year--container">
{this.renderHeader()}
{this.renderHeader({ monthDate: this.state.date })}
<Year
onDayClick={this.handleDayClick}
selectingDate={this.state.selectingDate}
Expand Down
5 changes: 3 additions & 2 deletions src/month.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export default class Month extends React.Component {
),
"react-datepicker__month-text--keyboard-selected":
!this.props.disabledKeyboardNavigation &&
utils.getMonth(preSelection) === m,
this.isSelectedMonth(day, m, preSelection),
"react-datepicker__month-text--in-selecting-range":
this.isInSelectingRangeMonth(m),
"react-datepicker__month-text--in-range": utils.isMonthInRange(
Expand Down Expand Up @@ -643,7 +643,8 @@ export default class Month extends React.Component {
selected,
),
"react-datepicker__quarter-text--keyboard-selected":
!disabledKeyboardNavigation && utils.getQuarter(preSelection) === q,
!disabledKeyboardNavigation &&
this.isSelectedQuarter(day, q, preSelection),
"react-datepicker__quarter-text--in-selecting-range":
this.isInSelectingRangeQuarter(q),
"react-datepicker__quarter-text--in-range": utils.isQuarterInRange(
Expand Down

0 comments on commit 6ca7ea7

Please sign in to comment.