Skip to content

Commit bd3ab11

Browse files
Merge pull request #6057 from Hacker0x01/feature/aria-label-support
feat: add aria-label prop support for DatePicker
2 parents 5d228d3 + dbfd2d3 commit bd3ab11

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,12 @@ export type DatePickerProps = OmitUnion<
192192
tabIndex?: number;
193193
ariaDescribedBy?: string;
194194
ariaInvalid?: string;
195+
ariaLabel?: string;
195196
ariaLabelledBy?: string;
196197
ariaRequired?: string;
197198
"aria-describedby"?: string;
198199
"aria-invalid"?: string;
200+
"aria-label"?: string;
199201
"aria-labelledby"?: string;
200202
"aria-required"?: string;
201203
rangeSeparator?: string;
@@ -1584,13 +1586,15 @@ export class DatePicker extends Component<DatePickerProps, DatePickerState> {
15841586
const ariaDescribedBy =
15851587
this.props["aria-describedby"] ?? this.props.ariaDescribedBy;
15861588
const ariaInvalid = this.props["aria-invalid"] ?? this.props.ariaInvalid;
1589+
const ariaLabel = this.props["aria-label"] ?? this.props.ariaLabel;
15871590
const ariaLabelledBy =
15881591
this.props["aria-labelledby"] ?? this.props.ariaLabelledBy;
15891592
const ariaRequired = this.props["aria-required"] ?? this.props.ariaRequired;
15901593

15911594
if (ariaDescribedBy != null)
15921595
ariaProps["aria-describedby"] = ariaDescribedBy;
15931596
if (ariaInvalid != null) ariaProps["aria-invalid"] = ariaInvalid;
1597+
if (ariaLabel != null) ariaProps["aria-label"] = ariaLabel;
15941598
if (ariaLabelledBy != null) ariaProps["aria-labelledby"] = ariaLabelledBy;
15951599
if (ariaRequired != null) ariaProps["aria-required"] = ariaRequired;
15961600

src/test/datepicker_test.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4350,20 +4350,50 @@ describe("DatePicker", () => {
43504350
expect(input.getAttribute("aria-required")).toBe("true");
43514351
});
43524352

4353+
it("should pass aria-label to the input using standard HTML attribute name", () => {
4354+
const { container } = render(
4355+
<DatePicker selected={newDate()} aria-label="Select a date" />,
4356+
);
4357+
const input = safeQuerySelector(container, "input");
4358+
expect(input.getAttribute("aria-label")).toBe("Select a date");
4359+
});
4360+
4361+
it("should pass aria-label to the input using camelCase prop name", () => {
4362+
const { container } = render(
4363+
<DatePicker selected={newDate()} ariaLabel="Select a date" />,
4364+
);
4365+
const input = safeQuerySelector(container, "input");
4366+
expect(input.getAttribute("aria-label")).toBe("Select a date");
4367+
});
4368+
4369+
it("should prefer standard HTML attribute name over camelCase for aria-label", () => {
4370+
const { container } = render(
4371+
<DatePicker
4372+
selected={newDate()}
4373+
aria-label="standard-label"
4374+
ariaLabel="camelcase-label"
4375+
/>,
4376+
);
4377+
const input = safeQuerySelector(container, "input");
4378+
expect(input.getAttribute("aria-label")).toBe("standard-label");
4379+
});
4380+
43534381
it("should pass aria attributes to custom input using standard HTML attribute names", () => {
43544382
const { container } = render(
43554383
<DatePicker
43564384
selected={newDate()}
43574385
customInput={<CustomInput />}
43584386
aria-describedby="desc-id"
43594387
aria-invalid="true"
4388+
aria-label="date-label"
43604389
aria-labelledby="label-id"
43614390
aria-required="true"
43624391
/>,
43634392
);
43644393
const input = safeQuerySelector(container, "input");
43654394
expect(input.getAttribute("aria-describedby")).toBe("desc-id");
43664395
expect(input.getAttribute("aria-invalid")).toBe("true");
4396+
expect(input.getAttribute("aria-label")).toBe("date-label");
43674397
expect(input.getAttribute("aria-labelledby")).toBe("label-id");
43684398
expect(input.getAttribute("aria-required")).toBe("true");
43694399
});

0 commit comments

Comments
 (0)