Skip to content

Commit e979e11

Browse files
committed
Add custom week classname support
1 parent ec4ce2c commit e979e11

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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: 6 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,9 @@ 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 <div className={clsx(weekNumberClasses, customWeekClassName)}>{this.renderDays()}</div>;
196201
}
197202
}

0 commit comments

Comments
 (0)