Skip to content

Commit f1c6069

Browse files
mkosirmartijnrusschen
authored andcommitted
fix/render-custom-day (Hacker0x01#1637)
* fix/render-custom-day * updated test * update day to date * add default day render * code coverage * code cov Hacker0x01#2 * backwards compatible
1 parent 2ccc715 commit f1c6069

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

docs-site/src/example_components.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import IncludeTimes from "./examples/include_times";
5252
import InjectTimes from "./examples/inject_times";
5353
import DontCloseOnSelect from "./examples/dont_close_onSelect";
5454
import RenderCustomHeader from "./examples/render_custom_header";
55+
import RenderCustomDay from "./examples/render_custom_day";
5556
import TimeInput from "./examples/timeInput";
5657
import StrictParsing from "./examples/strict_parsing";
5758
import "react-datepicker/dist/react-datepicker.css";
@@ -263,6 +264,10 @@ export default class exampleComponents extends React.Component {
263264
title: "Custom header",
264265
component: <RenderCustomHeader />
265266
},
267+
{
268+
title: "Custom Day",
269+
component: <RenderCustomDay />
270+
},
266271
{
267272
title: "Input time",
268273
component: <TimeInput />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react";
2+
import DatePicker from "react-datepicker";
3+
import getDate from "date-fns/getDate";
4+
5+
export default class RenderCustomDay extends React.Component {
6+
constructor(props) {
7+
super(props);
8+
this.state = {
9+
startDate: new Date()
10+
};
11+
}
12+
13+
handleChange = date => {
14+
this.setState({
15+
startDate: date
16+
});
17+
};
18+
19+
renderDayContents = (day, date) => {
20+
const tooltipText = `Tooltip for date: ${date}`;
21+
return <span title={tooltipText}>{getDate(date)}</span>;
22+
};
23+
24+
render() {
25+
return (
26+
<div className="row">
27+
<pre className="column example__code">
28+
<code className="jsx">
29+
{`
30+
<DatePicker
31+
selected={this.state.startDate}
32+
onChange={this.handleChange}
33+
renderDayContents={this.renderDayContents}
34+
/>
35+
`}
36+
</code>
37+
</pre>
38+
<div className="column">
39+
<DatePicker
40+
selected={this.state.startDate}
41+
onChange={this.handleChange}
42+
renderDayContents={this.renderDayContents}
43+
/>
44+
</div>
45+
</div>
46+
);
47+
}
48+
}

src/day.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,10 @@ export default class Day extends React.Component {
203203
role="option"
204204
>
205205
{this.props.renderDayContents
206-
? this.props.renderDayContents(getDate(this.props.day))
206+
? this.props.renderDayContents(
207+
getDate(this.props.day),
208+
this.props.day
209+
)
207210
: getDate(this.props.day)}
208211
</div>
209212
);

test/day_test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ describe("Day", () => {
3737

3838
it("should render custom day contents", () => {
3939
const day = newDate();
40-
function renderDayContents(day) {
41-
return <span>{day}</span>;
40+
function renderDayContents(day, date) {
41+
const tooltipText = `Tooltip for date: ${date}`;
42+
return <span title={tooltipText}>{getDate(date)}</span>;
4243
}
4344
const shallowDay = renderDay(day, { renderDayContents });
4445
expect(shallowDay.find("span"));

0 commit comments

Comments
 (0)