Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit bc440af

Browse files
authored
Merge pull request #483 from hypeserver/custom-day-content-renderer
Add support for customising the DayCell
2 parents 249bf9a + 48e7070 commit bc440af

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/components/DateRangePicker/README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,70 @@ const [state, setState] = useState([
122122
nextButton: "next month button",
123123
}}
124124
/>;
125-
```
125+
```
126+
127+
#### Example: Custom Day Cell Content
128+
Show orange dot only for weekend
129+
130+
```jsx inside Markdown
131+
import { addDays, format, isWeekend } from 'date-fns';
132+
import { useState } from 'react';
133+
134+
const [state, setState] = useState([
135+
{
136+
startDate: addDays(new Date(), -6),
137+
endDate: new Date(),
138+
key: 'selection1'
139+
},
140+
{
141+
startDate: addDays(new Date(), 1),
142+
endDate: addDays(new Date(), 7),
143+
key: 'selection2'
144+
}
145+
]);
146+
147+
function customDayContent(day) {
148+
extraDot = null;
149+
if (isWeekend(day)) {
150+
extraDot = (
151+
<div
152+
style={{
153+
height: "5px",
154+
width: "5px",
155+
borderRadius: "100%",
156+
background: "orange",
157+
position: "absolute",
158+
top: 2,
159+
right: 2,
160+
}}
161+
/>
162+
)
163+
}
164+
return (
165+
<div>
166+
{extraDot}
167+
<span>{format(day, "d")}</span>
168+
</div>
169+
)
170+
}
171+
172+
<DateRangePicker
173+
onChange={item => setState([item.selection])}
174+
showSelectionPreview={true}
175+
moveRangeOnFirstSelection={false}
176+
months={2}
177+
ranges={state}
178+
direction="horizontal"
179+
dayContentRenderer={customDayContent}
180+
ariaLabels={{
181+
dateInput: {
182+
selection1: { startDate: "start date input of selction 1", endDate: "end date input of selction 1" },
183+
selection2: { startDate: "start date input of selction 2", endDate: "end date input of selction 2" }
184+
},
185+
monthPicker: "month picker",
186+
yearPicker: "year picker",
187+
prevButton: "previous month button",
188+
nextButton: "next month button",
189+
}}
190+
/>;
191+
```

src/components/DayCell/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ class DayCell extends Component {
149149
/>
150150
));
151151
};
152+
152153
render() {
154+
const { dayContentRenderer } = this.props;
153155
return (
154156
<button
155157
type="button"
@@ -168,7 +170,10 @@ class DayCell extends Component {
168170
{this.renderSelectionPlaceholders()}
169171
{this.renderPreviewPlaceholder()}
170172
<span className={this.props.styles.dayNumber}>
171-
<span>{format(this.props.day, this.props.dayDisplayFormat)}</span>
173+
{
174+
dayContentRenderer?.(this.props.day) ||
175+
<span>{format(this.props.day, this.props.dayDisplayFormat)}</span>
176+
}
172177
</span>
173178
</button>
174179
);
@@ -213,6 +218,7 @@ DayCell.propTypes = {
213218
onMouseDown: PropTypes.func,
214219
onMouseUp: PropTypes.func,
215220
onMouseEnter: PropTypes.func,
221+
dayContentRenderer: PropTypes.func,
216222
};
217223

218224
export default DayCell;

0 commit comments

Comments
 (0)