Skip to content

Commit 7230d88

Browse files
committed
fix(time-picker): preserve aria-describedby and added tests
1 parent 0cd84a3 commit 7230d88

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ describe('TimePicker', () => {
313313
);
314314
});
315315

316-
it('should apply invalid aria attributes over conflicting consumer props', () => {
316+
it('should merge invalid aria attributes with consumer-provided aria-describedby', () => {
317317
render(
318318
<TimePicker
319319
id="time-picker"
@@ -326,12 +326,29 @@ describe('TimePicker', () => {
326326
const invalidText = screen.getByText('Invalid time');
327327
expect(screen.getByRole('textbox')).toHaveAttribute(
328328
'aria-describedby',
329-
invalidText.id
329+
`${invalidText.id} custom-hint`
330330
);
331331
expect(screen.getByRole('textbox')).toHaveAttribute(
332332
'aria-invalid',
333333
'true'
334334
);
335335
});
336+
337+
it('should merge warning aria attributes with consumer-provided aria-describedby', () => {
338+
render(
339+
<TimePicker
340+
id="time-picker"
341+
warning
342+
warningText="Warning message"
343+
aria-describedby="custom-hint"
344+
/>
345+
);
346+
const warningText = screen.getByText('Warning message');
347+
expect(screen.getByRole('textbox')).toHaveAttribute(
348+
'aria-describedby',
349+
`${warningText.id} custom-hint`
350+
);
351+
expect(screen.getByRole('textbox')).not.toHaveAttribute('aria-invalid');
352+
});
336353
});
337354
});

packages/react/src/components/TimePicker/TimePicker.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,17 @@ const TimePicker = frFn((props, ref) => {
269269
const readOnlyProps = {
270270
readOnly: readOnly,
271271
};
272-
const describedBy = normalizedProps.invalid
273-
? normalizedProps.invalidId
274-
: normalizedProps.warn
275-
? normalizedProps.warnId
276-
: ariaDescribedBy;
272+
const describedBy =
273+
[
274+
normalizedProps.invalid
275+
? normalizedProps.invalidId
276+
: normalizedProps.warn
277+
? normalizedProps.warnId
278+
: null,
279+
ariaDescribedBy,
280+
]
281+
.filter(Boolean)
282+
.join(' ') || undefined;
277283

278284
return (
279285
<div className={cx(`${prefix}--form-item`, className)}>

0 commit comments

Comments
 (0)