Skip to content

Commit

Permalink
Merge pull request #4405 from omarhoumz/feature/add-day-to-renderMont…
Browse files Browse the repository at this point in the history
…hContent

feature: Add day parameter to renderMonthContent function
  • Loading branch information
martijnrusschen authored Dec 7, 2023
2 parents f92c727 + 7105690 commit 5cabc76
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 4 additions & 2 deletions docs-site/src/examples/renderCustomMonth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
() => {
const renderMonthContent = (month, shortMonth, longMonth) => {
const tooltipText = `Tooltip for month: ${longMonth}`;
const renderMonthContent = (month, shortMonth, longMonth, day) => {
const fullYear = new Date(day).getFullYear();
const tooltipText = `Tooltip for month: ${longMonth} ${fullYear}`;

return <span title={tooltipText}>{shortMonth}</span>;
};
return (
Expand Down
9 changes: 5 additions & 4 deletions src/month.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,12 @@ export default class Month extends React.Component {
};

getMonthContent = (m) => {
const { showFullMonthYearPicker, renderMonthContent, locale } = this.props;
const { showFullMonthYearPicker, renderMonthContent, locale, day } =
this.props;
const shortMonthText = utils.getMonthShortInLocale(m, locale);
const fullMonthText = utils.getMonthInLocale(m, locale);
if (renderMonthContent) {
return renderMonthContent(m, shortMonthText, fullMonthText);
return renderMonthContent(m, shortMonthText, fullMonthText, day);
}
return showFullMonthYearPicker ? fullMonthText : shortMonthText;
};
Expand Down Expand Up @@ -753,8 +754,8 @@ export default class Month extends React.Component {
{showMonthYearPicker
? this.renderMonths()
: showQuarterYearPicker
? this.renderQuarters()
: this.renderWeeks()}
? this.renderQuarters()
: this.renderWeeks()}
</div>
);
}
Expand Down
14 changes: 9 additions & 5 deletions test/month_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,18 +826,22 @@ describe("Month", () => {

describe("custom renders", () => {
it("should render custom month content", () => {
function renderMonthContent() {
return <span>custom render</span>;
function renderMonthContent(_, __, ___, day) {
return <span data-day={day}>custom render</span>;
}
const day = utils.newDate();

const monthComponent = mount(
<Month
day={utils.newDate()}
day={day}
renderMonthContent={renderMonthContent}
showMonthYearPicker
/>,
);
const month = monthComponent.find(".react-datepicker__month-text").at(0);
expect(month.find("span").at(0).text()).toBe("custom render");
const span = month.find("span").at(0);
expect(span.text()).toBe("custom render");
expect(span.prop("data-day")).toBe(day);
});

it("should render custom quarter content", () => {
Expand Down Expand Up @@ -921,7 +925,7 @@ describe("Month", () => {
setPreSelection: setPreSelection,
preSelection: preSelected,
disabledKeyboardNavigation: true,
showQuarterYearPicker: true
showQuarterYearPicker: true,
});

expect(
Expand Down

0 comments on commit 5cabc76

Please sign in to comment.