Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ export type DatePickerProps = OmitUnion<
tabIndex?: number;
ariaDescribedBy?: string;
ariaInvalid?: string;
ariaLabel?: string;
ariaLabelledBy?: string;
ariaRequired?: string;
"aria-describedby"?: string;
"aria-invalid"?: string;
"aria-label"?: string;
"aria-labelledby"?: string;
"aria-required"?: string;
rangeSeparator?: string;
Expand Down Expand Up @@ -1584,13 +1586,15 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
const ariaDescribedBy =
this.props["aria-describedby"] ?? this.props.ariaDescribedBy;
const ariaInvalid = this.props["aria-invalid"] ?? this.props.ariaInvalid;
const ariaLabel = this.props["aria-label"] ?? this.props.ariaLabel;
const ariaLabelledBy =
this.props["aria-labelledby"] ?? this.props.ariaLabelledBy;
const ariaRequired = this.props["aria-required"] ?? this.props.ariaRequired;

if (ariaDescribedBy != null)
ariaProps["aria-describedby"] = ariaDescribedBy;
if (ariaInvalid != null) ariaProps["aria-invalid"] = ariaInvalid;
if (ariaLabel != null) ariaProps["aria-label"] = ariaLabel;
if (ariaLabelledBy != null) ariaProps["aria-labelledby"] = ariaLabelledBy;
if (ariaRequired != null) ariaProps["aria-required"] = ariaRequired;

Expand Down
30 changes: 30 additions & 0 deletions src/test/datepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4350,20 +4350,50 @@ describe("DatePicker", () => {
expect(input.getAttribute("aria-required")).toBe("true");
});

it("should pass aria-label to the input using standard HTML attribute name", () => {
const { container } = render(
<DatePicker selected={newDate()} aria-label="Select a date" />,
);
const input = safeQuerySelector(container, "input");
expect(input.getAttribute("aria-label")).toBe("Select a date");
});

it("should pass aria-label to the input using camelCase prop name", () => {
const { container } = render(
<DatePicker selected={newDate()} ariaLabel="Select a date" />,
);
const input = safeQuerySelector(container, "input");
expect(input.getAttribute("aria-label")).toBe("Select a date");
});

it("should prefer standard HTML attribute name over camelCase for aria-label", () => {
const { container } = render(
<DatePicker
selected={newDate()}
aria-label="standard-label"
ariaLabel="camelcase-label"
/>,
);
const input = safeQuerySelector(container, "input");
expect(input.getAttribute("aria-label")).toBe("standard-label");
});

it("should pass aria attributes to custom input using standard HTML attribute names", () => {
const { container } = render(
<DatePicker
selected={newDate()}
customInput={<CustomInput />}
aria-describedby="desc-id"
aria-invalid="true"
aria-label="date-label"
aria-labelledby="label-id"
aria-required="true"
/>,
);
const input = safeQuerySelector(container, "input");
expect(input.getAttribute("aria-describedby")).toBe("desc-id");
expect(input.getAttribute("aria-invalid")).toBe("true");
expect(input.getAttribute("aria-label")).toBe("date-label");
expect(input.getAttribute("aria-labelledby")).toBe("label-id");
expect(input.getAttribute("aria-required")).toBe("true");
});
Expand Down
Loading