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

♿️ Fix #5280: Disable the WeekNumber if all the corresponding week days are disabled #5291

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/week_number.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
| `showWeekNumber` | | | |
| `showWeekPicker` | | | |
| `weekNumber` (required) | | | |
| `isWeekDisabled` | | | |
38 changes: 38 additions & 0 deletions src/test/week_number_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,44 @@ describe("WeekNumber", () => {
).toBe(true);
});

it("shouldn't have the class 'react-datepicker__week-number--clickable' if isWeekDisabled is true", () => {
const { container } = render(
<WeekNumber
weekNumber={1}
date={new Date()}
onClick={() => {}}
isWeekDisabled
/>,
);
const weekNumber = container.querySelector(
".react-datepicker__week-number",
);
expect(
weekNumber?.classList.contains(
"react-datepicker__week-number--clickable",
),
).toBe(false);
});

it("should have the class 'react-datepicker__week-number--clickable' if isWeekDisabled is false and onClick is defined", () => {
const { container } = render(
<WeekNumber
weekNumber={1}
date={new Date()}
onClick={() => {}}
isWeekDisabled={false}
/>,
);
const weekNumber = container.querySelector(
".react-datepicker__week-number",
);
expect(
weekNumber?.classList.contains(
"react-datepicker__week-number--clickable",
),
).toBe(true);
});

it("should have the class 'react-datepicker__week-number--selected' if selected is current week and preselected is also current week and has the onClick Props", () => {
const currentWeekNumber = newDate("2023-10-22T13:09:53+02:00");
const { container } = render(
Expand Down
15 changes: 15 additions & 0 deletions src/week.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ export default class Week extends Component<WeekProps> {
return getWeek(date);
};

isWeekDisabled = (): boolean => {
const startOfWeek = this.startOfWeek();
const endOfWeek = addDays(startOfWeek, 6);

let processingDate = new Date(startOfWeek);
while (processingDate <= endOfWeek) {
if (!this.isDisabled(processingDate)) return false;

processingDate = addDays(processingDate, 1);
}

return true;
};

renderDays = () => {
const startOfWeek = this.startOfWeek();
const days = [];
Expand All @@ -128,6 +142,7 @@ export default class Week extends Component<WeekProps> {
{...Week.defaultProps}
{...this.props}
weekNumber={weekNumber}
isWeekDisabled={this.isWeekDisabled()}
date={startOfWeek}
onClick={onClickAction}
/>,
Expand Down
4 changes: 3 additions & 1 deletion src/week_number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface WeekNumberProps {
handleOnKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
containerRef?: React.RefObject<HTMLDivElement>;
isInputFocused?: boolean;
isWeekDisabled?: boolean;
}

export default class WeekNumber extends Component<WeekNumberProps> {
Expand Down Expand Up @@ -111,13 +112,14 @@ export default class WeekNumber extends Component<WeekNumberProps> {
render(): JSX.Element {
const {
weekNumber,
isWeekDisabled,
ariaLabelPrefix = WeekNumber.defaultProps.ariaLabelPrefix,
onClick,
} = this.props;

const weekNumberClasses = {
"react-datepicker__week-number": true,
"react-datepicker__week-number--clickable": !!onClick,
"react-datepicker__week-number--clickable": !!onClick && !isWeekDisabled,
"react-datepicker__week-number--selected":
!!onClick && isSameDay(this.props.date, this.props.selected),
};
Expand Down
Loading