Skip to content

Commit ffd8d30

Browse files
authored
fix(DatePicker): link invalid/warn text via aria-describedby (#22070)
* fix(DatePicker): link invalid/warn text via aria-describedby * test(DatePicker): add a11y test for helper/invalid/warn messaging
1 parent 5f977c9 commit ffd8d30

2 files changed

Lines changed: 61 additions & 1 deletion

File tree

packages/react/src/components/DatePicker/DatePicker-test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,56 @@ describe('DatePicker', () => {
180180
spy.mockRestore();
181181
});
182182

183+
describe('accessibility', () => {
184+
it('associates helper text with the input using aria-describedby', () => {
185+
render(
186+
<DatePickerInput
187+
id="date-picker-input-helper"
188+
labelText="Date Picker label"
189+
helperText="Helpful text"
190+
/>
191+
);
192+
193+
const input = screen.getByLabelText('Date Picker label');
194+
const helperText = screen.getByText('Helpful text');
195+
196+
expect(input).toHaveAttribute('aria-describedby', helperText.id);
197+
});
198+
199+
it('associates invalid text with the input and marks input as invalid', () => {
200+
render(
201+
<DatePickerInput
202+
id="date-picker-input-invalid"
203+
labelText="Date Picker label"
204+
invalid
205+
invalidText="Invalid date"
206+
/>
207+
);
208+
209+
const input = screen.getByLabelText('Date Picker label');
210+
const invalidText = screen.getByText('Invalid date');
211+
212+
expect(input).toHaveAttribute('aria-describedby', invalidText.id);
213+
expect(input).toHaveAttribute('aria-invalid', 'true');
214+
});
215+
216+
it('associates warning text with the input using aria-describedby', () => {
217+
render(
218+
<DatePickerInput
219+
id="date-picker-input-warn"
220+
labelText="Date Picker label"
221+
warn
222+
warnText="Warning message"
223+
/>
224+
);
225+
226+
const input = screen.getByLabelText('Date Picker label');
227+
const warnText = screen.getByText('Warning message');
228+
229+
expect(input).toHaveAttribute('aria-describedby', warnText.id);
230+
});
231+
});
232+
183233
it('should respect parseDate prop', async () => {
184234
const parseDate = jest.fn();
185235
parseDate.mockReturnValueOnce(new Date('1989/01/20'));

packages/react/src/components/DatePickerInput/DatePickerInput.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,27 @@ const DatePickerInput = frFn((props, ref) => {
243243
? undefined
244244
: `datepicker-input-helper-text-${datePickerInputInstanceId}`;
245245

246+
let ariaDescribedBy: string | undefined;
247+
if (normalizedProps.invalid) {
248+
ariaDescribedBy = normalizedProps.invalidId;
249+
} else if (normalizedProps.warn) {
250+
ariaDescribedBy = normalizedProps.warnId;
251+
} else {
252+
ariaDescribedBy = helperText ? datePickerInputHelperId : undefined;
253+
}
254+
246255
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- https://github.com/carbon-design-system/carbon/issues/20452
247256
const inputProps: any = {
248257
...rest,
249258
...datePickerInputProps,
250259
className: inputClasses,
251260
disabled: normalizedProps.disabled,
252261
ref,
253-
['aria-describedby']: helperText ? datePickerInputHelperId : undefined,
262+
['aria-describedby']: ariaDescribedBy,
254263
};
255264
if (normalizedProps.invalid) {
256265
inputProps['data-invalid'] = true;
266+
inputProps['aria-invalid'] = true;
257267
}
258268
const input = <input {...inputProps} />;
259269

0 commit comments

Comments
 (0)