-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a focus trap to DatePickerE using the react-focus-trap library. (This means that the focus stays within the picker when it is open and the tab key is pressed.) This also replaces the existing document click logic as this is handled by react-focus-trap (and focus-trap). The `isOpen` prop was also changed to affect the first render only and renamed to `initialIsOpen` (matching `SidebarE`). This is because it doesn't make much sense to have it affect subsequent renders – you never know whether the prop or the internal state is correct at any point in time. Finally the `usehooks-ts` library was added to gain access to some utility hooks it provides.
- Loading branch information
Showing
8 changed files
with
132 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
packages/react-component-library/src/components/DatePickerE/useCloseOnEscape.ts
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
packages/react-component-library/src/components/DatePickerE/useDatePickerEOpenClose.ts
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
packages/react-component-library/src/components/DatePickerE/useFocusTrapOptions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Options as FocusTrapOptions } from 'focus-trap' | ||
import React, { useMemo } from 'react' | ||
|
||
function isEventTargetDescendantOf( | ||
event: Event, | ||
refs: React.RefObject<Element>[] | ||
): boolean { | ||
return ( | ||
event.target instanceof Node && | ||
refs.some((ref) => ref.current?.contains(event.target as Node)) | ||
) | ||
} | ||
|
||
export function useFocusTrapOptions( | ||
close: () => void, | ||
clickAllowedElementRefs: React.RefObject<Element>[] | ||
): FocusTrapOptions { | ||
return useMemo( | ||
() => ({ | ||
allowOutsideClick: (event) => | ||
isEventTargetDescendantOf(event, clickAllowedElementRefs), | ||
clickOutsideDeactivates: (event) => | ||
!isEventTargetDescendantOf(event, clickAllowedElementRefs), | ||
// Temporary workaround until we update to react-day-picker v8, which has a way | ||
// to set the initial focus to the selected date (or today if no date selected) | ||
initialFocus: '[role="button"]', | ||
onDeactivate: close, | ||
}), | ||
[clickAllowedElementRefs, close] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters