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 17, 2024
1 parent 8ef2eef commit f1c23ac
Show file tree
Hide file tree
Showing 3 changed files with 112 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
94 changes: 94 additions & 0 deletions test/datepicker_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,100 @@ describe("DatePicker", () => {
});
});

describe("multiple MonthYearPicker", () => {
const selected = utils.newDate("2023-05-15");

it("should contain a different year all headers.", () => {
let instance;
// 2 Years
const { rerender } = render(
<DatePicker
ref={(node) => {
instance = node;
}}
monthsShown={2}
selected={selected}
showMonthYearPicker
/>,
);
fireEvent.click(instance.input);
const headers = instance.calendar.componentNode.querySelectorAll(
".react-datepicker__header",
);
expect(headers).toHaveLength(2);
expect(headers[0].textContent).toBe("2023");
expect(headers[1].textContent).toBe("2024");

// 4 Years
rerender(
<DatePicker
ref={(node) => {
instance = node;
}}
monthsShown={4}
selected={selected}
showMonthYearPicker
/>,
);
fireEvent.click(instance.input);
const headersMore = instance.calendar.componentNode.querySelectorAll(
".react-datepicker__header",
);
expect(headersMore).toHaveLength(4);
expect(headersMore[0].textContent).toBe("2023");
expect(headersMore[1].textContent).toBe("2024");
expect(headersMore[2].textContent).toBe("2025");
expect(headersMore[3].textContent).toBe("2026");
});
});

describe("multiple QuarterYearPicker", () => {
const selected = utils.newDate("2023-05-15");

it("should contain a different year all headers.", () => {
let instance;
// 2 Years
const { rerender } = render(
<DatePicker
ref={(node) => {
instance = node;
}}
monthsShown={2}
selected={selected}
showQuarterYearPicker
/>,
);
fireEvent.click(instance.input);
const headers = instance.calendar.componentNode.querySelectorAll(
".react-datepicker__header",
);
expect(headers).toHaveLength(2);
expect(headers[0].textContent).toBe("2023");
expect(headers[1].textContent).toBe("2024");

// 4 Years
rerender(
<DatePicker
ref={(node) => {
instance = node;
}}
monthsShown={4}
selected={selected}
showQuarterYearPicker
/>,
);
fireEvent.click(instance.input);
const headersMore = instance.calendar.componentNode.querySelectorAll(
".react-datepicker__header",
);
expect(headersMore).toHaveLength(4);
expect(headersMore[0].textContent).toBe("2023");
expect(headersMore[1].textContent).toBe("2024");
expect(headersMore[2].textContent).toBe("2025");
expect(headersMore[3].textContent).toBe("2026");
});
});

describe("shouldFocusDayInline state", () => {
const dateFormat = "yyyy-MM-dd";

Expand Down

0 comments on commit f1c23ac

Please sign in to comment.