GeezerVison is showing github.com — [react-interactions] Adds more experimental Scope API methods (#17042) · react/react@b71ab61
File treeExpand file treeCollapse file tree Open diff view settings react-interactions/accessibility react-native-renderer/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-art/src/ReactARTHostConfig.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -461,3 +461,7 @@ export function updateFundamentalComponent(fundamentalInstance) { | 461 | 461 | export function unmountFundamentalComponent(fundamentalInstance) { | 462 | 462 | throw new Error('Not yet implemented.'); | 463 | 463 | } | | 464 | + | | 465 | +export function getInstanceFromNode(node) { | | 466 | + throw new Error('Not yet implemented.'); | | 467 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMHostConfig.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| 10 | | -import {precacheFiberNode, updateFiberProps} from './ReactDOMComponentTree'; | | 10 | +import { | | 11 | + precacheFiberNode, | | 12 | + updateFiberProps, | | 13 | + getClosestInstanceFromNode, | | 14 | +} from './ReactDOMComponentTree'; | 11 | 15 | import { | 12 | 16 | createElement, | 13 | 17 | createTextNode, | @@ -976,3 +980,7 @@ export function unmountFundamentalComponent( | 976 | 980 | } | 977 | 981 | } | 978 | 982 | } | | 983 | + | | 984 | +export function getInstanceFromNode(node: HTMLElement): null | Object { | | 985 | + return getClosestInstanceFromNode(node) || null; | | 986 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/README.md +14-4Lines changed: 14 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -42,8 +42,8 @@ function MyComponent(props) { | 42 | 42 | ); | 43 | 43 | } | 44 | 44 |
| 45 | | -// Using the ref, we can get the host nodes via getScopedNodes() | 46 | | -const divs = divOnlyScope.current.getScopedNodes(); | | 45 | +// Using the ref, we can get the host nodes via getAllNodes() | | 46 | +const divs = divOnlyScope.current.getAllNodes(); | 47 | 47 |
| 48 | 48 | // [<div>DIV 1</div>, <div>DIV 2</div>, <div>DIV 3</div>] | 49 | 49 | console.log(divs); | @@ -72,7 +72,17 @@ Returns the parent `ReactScopeInterface` of the scope node or `null` if none exi | 72 | 72 |
| 73 | 73 | Returns the current `props` object of the scope node. | 74 | 74 |
| 75 | | -### getScopedNodes: () => null | Array<HTMLElement> | | 75 | +### getAllNodes: () => null | Array<HTMLElement> | 76 | 76 |
| 77 | 77 | Returns an array of all child host nodes that successfully match when queried using the | 78 | | -query function passed to the scope. Returns `null` if there are no matching host nodes. | | 78 | +query function passed to the scope. Returns `null` if there are no matching host nodes. | | 79 | + | | 80 | +### getFirstNode: () => null | HTMLElement | | 81 | + | | 82 | +Returns the first child host node that successfully matches when queried using the | | 83 | +query function passed to the scope. Returns `null` if there is no matching host node. | | 84 | + | | 85 | +### containsNode: (node: HTMLElement) => boolean | | 86 | + | | 87 | +Returns `true` or `false` depending on if the given `HTMLElement` is a descendant | | 88 | +of the scope's sub-tree. |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/docs/TabbableScope.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -15,7 +15,7 @@ function FocusableNodeCollector(props) { | 15 | 15 | const scope = scopeRef.current; | 16 | 16 |
| 17 | 17 | if (scope) { | 18 | | - const tabFocusableNodes = scope.getScopedNodes(); | | 18 | + const tabFocusableNodes = scope.getAllNodes(); | 19 | 19 | if (tabFocusableNodes && props.onFocusableNodes) { | 20 | 20 | props.onFocusableNodes(tabFocusableNodes); | 21 | 21 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/FocusContain.js +8-4Lines changed: 8 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,10 +66,14 @@ export default function FocusContain({ | 66 | 66 | useLayoutEffect( | 67 | 67 | () => { | 68 | 68 | const scope = scopeRef.current; | 69 | | - if (scope !== null && disabled !== true) { | 70 | | - const elems = scope.getScopedNodes(); | 71 | | - if (elems && elems.indexOf(document.activeElement) === -1) { | 72 | | - elems[0].focus(); | | 69 | + if ( | | 70 | + scope !== null && | | 71 | + disabled !== true && | | 72 | + !scope.containsNode(document.activeElement) | | 73 | + ) { | | 74 | + const fistElem = scope.getFirstNode(); | | 75 | + if (fistElem !== null) { | | 76 | + fistElem.focus(); | 73 | 77 | } | 74 | 78 | } | 75 | 79 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/FocusGroup.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,9 +30,9 @@ type FocusGroupProps = {| | 30 | 30 | const {useRef} = React; | 31 | 31 |
| 32 | 32 | function focusGroupItem(cell: ReactScopeMethods, event: KeyboardEvent): void { | 33 | | - const tabbableNodes = cell.getScopedNodes(); | 34 | | - if (tabbableNodes !== null && tabbableNodes.length > 0) { | 35 | | - tabbableNodes[0].focus(); | | 33 | + const firstScopedNode = cell.getFirstNode(); | | 34 | + if (firstScopedNode !== null) { | | 35 | + firstScopedNode.focus(); | 36 | 36 | event.preventDefault(); | 37 | 37 | } | 38 | 38 | } | @@ -135,7 +135,7 @@ export function createFocusGroup( | 135 | 135 | const tabScope = getGroupProps(currentItem).tabScopeRef.current; | 136 | 136 | if (tabScope) { | 137 | 137 | const activeNode = document.activeElement; | 138 | | - const nodes = tabScope.getScopedNodes(); | | 138 | + const nodes = tabScope.getAllNodes(); | 139 | 139 | for (let i = 0; i < nodes.length; i++) { | 140 | 140 | const node = nodes[i]; | 141 | 141 | if (node !== activeNode) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/FocusTable.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,9 +39,9 @@ type FocusTableProps = {| | 39 | 39 | const {useRef} = React; | 40 | 40 |
| 41 | 41 | function focusScope(cell: ReactScopeMethods, event?: KeyboardEvent): void { | 42 | | - const tabbableNodes = cell.getScopedNodes(); | 43 | | - if (tabbableNodes !== null && tabbableNodes.length > 0) { | 44 | | - tabbableNodes[0].focus(); | | 42 | + const firstScopedNode = cell.getFirstNode(); | | 43 | + if (firstScopedNode !== null) { | | 44 | + firstScopedNode.focus(); | 45 | 45 | if (event) { | 46 | 46 | event.preventDefault(); | 47 | 47 | } | @@ -209,7 +209,7 @@ export function createFocusTable( | 209 | 209 | const tabScope = getTableProps(currentCell).tabScopeRef.current; | 210 | 210 | if (tabScope) { | 211 | 211 | const activeNode = document.activeElement; | 212 | | - const nodes = tabScope.getScopedNodes(); | | 212 | + const nodes = tabScope.getAllNodes(); | 213 | 213 | for (let i = 0; i < nodes.length; i++) { | 214 | 214 | const node = nodes[i]; | 215 | 215 | if (node !== activeNode) { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/__tests__/TabbableScope-test.internal.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,7 @@ describe('TabbableScope', () => { | 35 | 35 | container = null; | 36 | 36 | }); | 37 | 37 |
| 38 | | - it('getScopedNodes() works as intended', () => { | | 38 | + it('getAllNodes() works as intended', () => { | 39 | 39 | const scopeRef = React.createRef(); | 40 | 40 | const nodeRefA = React.createRef(); | 41 | 41 | const nodeRefB = React.createRef(); | @@ -58,7 +58,7 @@ describe('TabbableScope', () => { | 58 | 58 | } | 59 | 59 |
| 60 | 60 | ReactDOM.render(<Test />, container); | 61 | | - let nodes = scopeRef.current.getScopedNodes(); | | 61 | + let nodes = scopeRef.current.getAllNodes(); | 62 | 62 | expect(nodes).toEqual([ | 63 | 63 | nodeRefA.current, | 64 | 64 | nodeRefB.current, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/accessibility/src/shared/getTabbableNodes.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,7 +18,7 @@ export default function getTabbableNodes( | 18 | 18 | number, | 19 | 19 | null | HTMLElement, | 20 | 20 | ] { | 21 | | - const tabbableNodes = scope.getScopedNodes(); | | 21 | + const tabbableNodes = scope.getAllNodes(); | 22 | 22 | if (tabbableNodes === null || tabbableNodes.length === 0) { | 23 | 23 | return [null, null, null, 0, null]; | 24 | 24 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactFabricHostConfig.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -491,3 +491,7 @@ export function unmountFundamentalComponent(fundamentalInstance) { | 491 | 491 | export function cloneFundamentalInstance(fundamentalInstance) { | 492 | 492 | throw new Error('Not yet implemented.'); | 493 | 493 | } | | 494 | + | | 495 | +export function getInstanceFromNode(node) { | | 496 | + throw new Error('Not yet implemented.'); | | 497 | +} |
|