Skip to content

Commit b49f0eb

Browse files
authored
fix: preserve DatePicker tab flow in readOnly mode (#22010)
1 parent f96472c commit b49f0eb

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,32 @@ describe('Single date picker', () => {
709709
expect(document.body).toHaveFocus();
710710
expect(onClose).toHaveBeenCalledTimes(2);
711711
});
712+
713+
it('should keep native tab navigation when `readOnly` is true', async () => {
714+
render(
715+
<>
716+
<DatePicker datePickerType="single" readOnly>
717+
<DatePickerInput id="readonly-input-id" labelText="Read only input" />
718+
</DatePicker>
719+
<button data-testid="next-focus-target" type="button">
720+
Next focus target
721+
</button>
722+
</>
723+
);
724+
725+
const dateInput = screen.getByLabelText('Read only input');
726+
const nextFocusTarget = screen.getByTestId('next-focus-target');
727+
const calendar = screen.getByRole('application');
728+
729+
expect(calendar).not.toHaveClass('open');
730+
expect(document.body).toHaveFocus();
731+
await userEvent.tab();
732+
expect(dateInput).toHaveFocus();
733+
expect(calendar).not.toHaveClass('open');
734+
await userEvent.tab();
735+
expect(nextFocusTarget).toHaveFocus();
736+
expect(calendar).not.toHaveClass('open');
737+
});
712738
});
713739

714740
describe('Range date picker', () => {

packages/react/src/components/DatePicker/DatePicker.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,8 @@ const DatePicker = forwardRef<HTMLDivElement, DatePickerProps>((props, ref) => {
654654
calendarRef.current = calendar;
655655

656656
const handleInputFieldKeyDown = (event: KeyboardEvent) => {
657+
if (readOnly && match(event, keys.Tab)) return;
658+
657659
const {
658660
calendarContainer,
659661
selectedDateElem: fpSelectedDateElem,

0 commit comments

Comments
 (0)