Skip to content

Commit 9beb8c5

Browse files
Merge pull request #5745 from marcelltoth/week-classname-support
Week classname support
2 parents 9e182d7 + 0c82008 commit 9beb8c5

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

docs-site/src/components/Examples/examples.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
color: #f00;
134134
}
135135

136+
.react-datepicker__week.highlighted {
137+
background-color: #ddffdd;
138+
border-radius: 5px;
139+
}
140+
136141
.example-custom-input {
137142
cursor: pointer;
138143
padding: 5px 15px;

docs-site/src/components/Examples/index.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import CustomDateFormat from "../../examples/customDateFormat?raw";
1818
import CustomClassName from "../../examples/customClassName?raw";
1919
import CustomCalendarClassName from "../../examples/customCalendarClassName?raw";
2020
import CustomDayClassName from "../../examples/customDayClassName?raw";
21+
import CustomWeekClassName from "../../examples/customWeekClassName?raw";
2122
import CustomTimeClassName from "../../examples/customTimeClassName?raw";
2223
import Today from "../../examples/today?raw";
2324
import PlaceholderText from "../../examples/placeholderText?raw";
@@ -223,6 +224,10 @@ export default class exampleComponents extends React.Component {
223224
title: "Custom day class name",
224225
component: CustomDayClassName,
225226
},
227+
{
228+
title: "Custom week class name",
229+
component: CustomWeekClassName,
230+
},
226231
{
227232
title: "Custom date format",
228233
component: CustomDateFormat,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
() => {
2+
const [selectedDate, setSelectedDate] = useState(new Date());
3+
return (
4+
<DatePicker
5+
selected={selectedDate}
6+
onChange={(date) => setSelectedDate(date)}
7+
weekClassName={(date) =>
8+
getDate(date) % 2 === 0 ? "highlighted" : undefined
9+
}
10+
/>
11+
);
12+
};

src/test/week_test.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ describe("Week", () => {
2323
expect(container.querySelector(".react-datepicker__week")).not.toBeNull();
2424
});
2525

26+
it("should apply className returned from passed weekClassName prop function", () => {
27+
const className = "customClassNameWeek";
28+
const monthClassNameFunc = () => className;
29+
const { container } = render(
30+
<Week
31+
day={newDate()}
32+
month={getMonth(newDate())}
33+
weekClassName={monthClassNameFunc}
34+
/>,
35+
);
36+
expect(
37+
container
38+
.querySelector(".react-datepicker__week")
39+
?.classList.contains(className),
40+
).toBe(true);
41+
});
42+
2643
it("should render the days of the week", () => {
2744
const weekStart = getStartOfWeek(newDate("2015-12-20"));
2845
const { container } = render(

src/week.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Day from "./day";
1212
import WeekNumber from "./week_number";
1313

1414
interface DayProps extends React.ComponentPropsWithoutRef<typeof Day> {}
15+
1516
interface WeekNumberProps
1617
extends React.ComponentPropsWithoutRef<typeof WeekNumber> {}
1718

@@ -38,6 +39,7 @@ interface WeekProps
3839
weekNumber: number,
3940
event: React.MouseEvent<HTMLDivElement>,
4041
) => void;
42+
weekClassName?: (date: Date) => string;
4143
}
4244

4345
export default class Week extends Component<WeekProps> {
@@ -192,6 +194,13 @@ export default class Week extends Component<WeekProps> {
192194
),
193195
"react-datepicker__week--keyboard-selected": this.isKeyboardSelected(),
194196
};
195-
return <div className={clsx(weekNumberClasses)}>{this.renderDays()}</div>;
197+
const customWeekClassName = this.props.weekClassName
198+
? this.props.weekClassName(this.startOfWeek())
199+
: undefined;
200+
return (
201+
<div className={clsx(weekNumberClasses, customWeekClassName)}>
202+
{this.renderDays()}
203+
</div>
204+
);
196205
}
197206
}

0 commit comments

Comments
 (0)