Skip to content

Commit

Permalink
fix(Datepicker): Fix keyboard navigation inside Shadow DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-gendron committed Mar 26, 2021
1 parent e84f5cc commit a44ffc4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/react/src/components/datepicker/datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,6 @@ export const Datepicker = forwardRef(({
}
}, [currentLocale, firstDayOfWeek]);

function handleCalendarKeyDown(event: KeyboardEvent<HTMLDivElement>): void {
if (event.key === 'Escape') {
event.stopPropagation();
dateInputRef.current?.setOpen(false);
calendarButtonRef.current?.focus();
}
}

function handleCalendarSelect(): void {
calendarButtonRef.current?.focus();
}

function focusCalendarDate(): void {
setTimeout(() => {
const dateToFocus = calendarRef.current
Expand All @@ -405,6 +393,26 @@ export const Datepicker = forwardRef(({
}, 0);
}

function handleCalendarKeyDown(event: KeyboardEvent<HTMLDivElement>): void {
switch (event.key) {
case 'ArrowUp':
case 'ArrowDown':
case 'ArrowLeft':
case 'ArrowRight':
focusCalendarDate();
break;
case 'Escape':
event.stopPropagation();
dateInputRef.current?.setOpen(false);
calendarButtonRef.current?.focus();
break;
}
}

function handleCalendarSelect(): void {
calendarButtonRef.current?.focus();
}

function handleCalendarButtonMouseDown(event: MouseEvent<HTMLButtonElement>): void {
event.stopPropagation();
if (dateInputRef.current?.isCalendarOpen()) {
Expand Down
6 changes: 6 additions & 0 deletions packages/storybook/stories/datepicker.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Story } from '@storybook/react';
import React, { FormEvent, useRef } from 'react';
import styled from 'styled-components';
import { decorateWith } from './utils/decorator';
import { ShadowDomDecorator } from './utils/shadow-dom-decorator';

const Container = styled.div`
height: 400px;
Expand All @@ -22,6 +23,11 @@ export const Normal: Story = () => (
<Datepicker label="Date" hint="Hint" />
);

export const InsideShadowDom: Story = () => (
<Datepicker label="Date" hint="Hint" />
);
InsideShadowDom.decorators = [ShadowDomDecorator];

export const WithTodayButton: Story = () => (
<Datepicker label="Date" hasTodayButton />
);
Expand Down

0 comments on commit a44ffc4

Please sign in to comment.