-
Notifications
You must be signed in to change notification settings - Fork 50.6k
[react-interactions] Add allowModifiers flag to FocusList + FocusTable #16971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ type FocusListProps = {| | |
| portrait: boolean, | ||
| wrap?: boolean, | ||
| tabScope?: ReactScope, | ||
| allowModifiers?: boolean, | ||
| |}; | ||
|
|
||
| const {useRef} = React; | ||
|
|
@@ -82,6 +83,13 @@ function getListProps(currentCell: ReactScopeMethods): Object { | |
| return {}; | ||
| } | ||
|
|
||
| function hasModifierKey(event: KeyboardEvent): boolean { | ||
| const {altKey, ctrlKey, metaKey, shiftKey} = event; | ||
| return ( | ||
| altKey === true || ctrlKey === true || metaKey === true || shiftKey === true | ||
| ); | ||
| } | ||
|
|
||
| export function createFocusList(scope: ReactScope): Array<React.Component> { | ||
| const TableScope = React.unstable_createScope(scope.fn); | ||
|
|
||
|
|
@@ -90,14 +98,16 @@ export function createFocusList(scope: ReactScope): Array<React.Component> { | |
| portrait, | ||
| wrap, | ||
| tabScope: TabScope, | ||
| allowModifiers, | ||
| }): FocusListProps { | ||
| const tabScopeRef = useRef(null); | ||
| return ( | ||
| <TableScope | ||
| type="list" | ||
| portrait={portrait} | ||
| wrap={wrap} | ||
| tabScopeRef={tabScopeRef}> | ||
| tabScopeRef={tabScopeRef} | ||
| allowModifiers={allowModifiers}> | ||
| {TabScope ? ( | ||
| <TabScope ref={tabScopeRef}>{children}</TabScope> | ||
| ) : ( | ||
|
|
@@ -117,25 +127,36 @@ export function createFocusList(scope: ReactScope): Array<React.Component> { | |
| const listProps = list && list.getProps(); | ||
| if (list !== null && listProps.type === 'list') { | ||
| const portrait = listProps.portrait; | ||
| switch (event.key) { | ||
| case 'Tab': { | ||
| const tabScope = getListProps(currentItem).tabScopeRef.current; | ||
| if (tabScope) { | ||
| const activeNode = document.activeElement; | ||
| const nodes = tabScope.getScopedNodes(); | ||
| for (let i = 0; i < nodes.length; i++) { | ||
| const node = nodes[i]; | ||
| if (node !== activeNode) { | ||
| setElementCanTab(node, false); | ||
| } else { | ||
| setElementCanTab(node, true); | ||
| } | ||
| const key = event.key; | ||
|
|
||
| if (key === 'Tab') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that "shift" is a modifier, and it impacts "Tab" (reverses the direction) it seems odd that we check for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The modifiers logic is only for the keyboard arrow keys, not for Tab.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. That wasn't super clear to me just looking at the code and going off of the name of the prop. Maybe some inline comment clarifying the intent could be helpful? |
||
| const tabScope = getListProps(currentItem).tabScopeRef.current; | ||
| if (tabScope) { | ||
| const activeNode = document.activeElement; | ||
| const nodes = tabScope.getScopedNodes(); | ||
| for (let i = 0; i < nodes.length; i++) { | ||
| const node = nodes[i]; | ||
| if (node !== activeNode) { | ||
| setElementCanTab(node, false); | ||
| } else { | ||
| setElementCanTab(node, true); | ||
| } | ||
| return; | ||
| } | ||
| return; | ||
| } | ||
| event.continuePropagation(); | ||
| return; | ||
| } | ||
| // Using modifier keys with keyboard arrow events should be no-ops | ||
| // unless an explicit allowModifiers flag is set on the FocusList. | ||
| if (hasModifierKey(event)) { | ||
| const allowModifiers = getListProps(currentItem).allowModifiers; | ||
| if (!allowModifiers) { | ||
| event.continuePropagation(); | ||
| return; | ||
| } | ||
| } | ||
| switch (key) { | ||
| case 'ArrowUp': { | ||
| if (portrait) { | ||
| const previousListItem = getPreviousListItem( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @emails react-core | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import {createEventTarget} from 'react-interactions/events/src/dom/testing-library'; | ||
|
|
||
| // This function is used by the a11y modules for testing | ||
| export function emulateBrowserTab(backwards) { | ||
| const activeElement = document.activeElement; | ||
| const focusedElem = createEventTarget(activeElement); | ||
| let defaultPrevented = false; | ||
| focusedElem.keydown({ | ||
| key: 'Tab', | ||
| shiftKey: backwards, | ||
| preventDefault() { | ||
| defaultPrevented = true; | ||
| }, | ||
| }); | ||
| if (!defaultPrevented) { | ||
| // This is not a full spec compliant version, but should be suffice for this test | ||
| const focusableElems = Array.from( | ||
| document.querySelectorAll( | ||
| 'input, button, select, textarea, a[href], [tabindex], [contenteditable], iframe, object, embed', | ||
| ), | ||
| ).filter( | ||
| elem => elem.tabIndex > -1 && !elem.disabled && !elem.contentEditable, | ||
| ); | ||
| const idx = focusableElems.indexOf(activeElement); | ||
| if (idx !== -1) { | ||
| focusableElems[backwards ? idx - 1 : idx + 1].focus(); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is already in the 'shared' module
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally used that thinking the exact same thing, but it's actually slightly different as it expects a different event object (that it reads
nativeEventfrom). I'm not too bothered about duplication here, we can tidy up once we're happy with the moving parts. :)