Skip to content

Commit

Permalink
Merge pull request #4319 from qburst/issue-4259/feature/aria-disabled…
Browse files Browse the repository at this point in the history
…-for-Time-component

Fix #4259: Add the aria-disabled on the Time component's disabled time units
  • Loading branch information
martijnrusschen authored Oct 15, 2023
2 parents fa6d0c2 + fde2732 commit 733fc27
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
18 changes: 10 additions & 8 deletions src/time.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export default class Time extends React.Component {
isSelectedTime = (time) =>
this.props.selected && isSameMinute(this.props.selected, time);

isDisabledTime = (time) =>
((this.props.minTime || this.props.maxTime) &&
isTimeInDisabledRange(time, this.props)) ||
((this.props.excludeTimes ||
this.props.includeTimes ||
this.props.filterTime) &&
isTimeDisabled(time, this.props));

liClasses = (time) => {
let classes = [
"react-datepicker__time-list-item",
Expand All @@ -110,14 +118,7 @@ export default class Time extends React.Component {
classes.push("react-datepicker__time-list-item--selected");
}

if (
((this.props.minTime || this.props.maxTime) &&
isTimeInDisabledRange(time, this.props)) ||
((this.props.excludeTimes ||
this.props.includeTimes ||
this.props.filterTime) &&
isTimeDisabled(time, this.props))
) {
if (this.isDisabledTime(time)) {
classes.push("react-datepicker__time-list-item--disabled");
}
if (
Expand Down Expand Up @@ -216,6 +217,7 @@ export default class Time extends React.Component {
tabIndex={time === timeToFocus ? 0 : -1}
role="option"
aria-selected={this.isSelectedTime(time) ? "true" : undefined}
aria-disabled={this.isDisabledTime(time) ? "true" : undefined}
>
{formatDate(time, format, this.props.locale)}
</li>
Expand Down
5 changes: 5 additions & 0 deletions test/exclude_times_test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ describe("DatePicker", () => {
".react-datepicker__time-list-item--disabled",
);
expect(disabledTimeItems.length).toBe(4);

const allDisabledTimeItemsHaveAriaDisabled = Array.from(
disabledTimeItems,
).every((time) => time.getAttribute("aria-disabled") === "true");
expect(allDisabledTimeItemsHaveAriaDisabled).toBe(true);
});
});
16 changes: 11 additions & 5 deletions test/filter_times_test.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import React from "react";
import { mount } from "enzyme";
import { render } from "@testing-library/react";
import { getHours } from "../src/date_utils";
import TimeComponent from "../src/time";

describe("TimeComponent", () => {
it("should disable times matched by filterTime prop", () => {
const timeComponent = mount(
const { container: timeComponent } = render(
<TimeComponent filterTime={(time) => getHours(time) !== 17} />,
);

expect(
timeComponent.find(".react-datepicker__time-list-item--disabled"),
).toHaveLength(2);
const disabledTimeItems = timeComponent.querySelectorAll(
".react-datepicker__time-list-item--disabled",
);
expect(disabledTimeItems.length).toBe(2);

const allDisabledTimeItemsHaveAriaDisabled = Array.from(
disabledTimeItems,
).every((time) => time.getAttribute("aria-disabled") === "true");
expect(allDisabledTimeItemsHaveAriaDisabled).toBe(true);
});
});
13 changes: 9 additions & 4 deletions test/include_times_test.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from "react";
import { mount } from "enzyme";
import { render } from "@testing-library/react";
import * as utils from "../src/date_utils";
import TimeComponent from "../src/time";

describe("TimeComponent", () => {
it("should only enable times specified in includeTimes props", () => {
const today = utils.getStartOfDay(utils.newDate());
const timeComponent = mount(
const { container: timeComponent } = render(
<TimeComponent
includeTimes={[
utils.addMinutes(today, 60),
Expand All @@ -16,9 +16,14 @@ describe("TimeComponent", () => {
/>,
);

const disabledItems = timeComponent.find(
const disabledTimeItems = timeComponent.querySelectorAll(
".react-datepicker__time-list-item--disabled",
);
expect(disabledItems).toHaveLength(45);
expect(disabledTimeItems.length).toBe(45);

const allDisabledTimeItemsHaveAriaDisabled = Array.from(
disabledTimeItems,
).every((time) => time.getAttribute("aria-disabled") === "true");
expect(allDisabledTimeItemsHaveAriaDisabled).toBe(true);
});
});

0 comments on commit 733fc27

Please sign in to comment.