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

feature: Add day parameter to renderMonthContent function #4405

Merged
merged 3 commits into from
Dec 7, 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
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
Loading