( props: GridCellProps, state: GridState<T, C>, ref: RefObject<FocusableElement | null> )
| 81 | * @param state - State of the parent grid, as returned by `useGridState`. |
| 82 | */ |
| 83 | export function useGridCell<T, C extends GridCollection<T>>( |
| 84 | props: GridCellProps, |
| 85 | state: GridState<T, C>, |
| 86 | ref: RefObject<FocusableElement | null> |
| 87 | ): GridCellAria { |
| 88 | let { |
| 89 | node, |
| 90 | isVirtualized, |
| 91 | focusMode: focusModeProp, |
| 92 | allowsArrowNavigation, |
| 93 | shouldSelectOnPressUp, |
| 94 | onAction |
| 95 | } = props; |
| 96 | |
| 97 | let {direction} = useLocale(); |
| 98 | let { |
| 99 | keyboardDelegate, |
| 100 | actions: {onCellAction}, |
| 101 | keyboardNavigationBehavior |
| 102 | } = gridMap.get(state)!; |
| 103 | let focusMode = focusModeProp ?? (keyboardNavigationBehavior === 'tab' ? 'cell' : 'child'); |
| 104 | |
| 105 | // We need to track the key of the item at the time it was last focused so that we force |
| 106 | // focus to go to the item when the DOM node is reused for a different item in a virtualizer. |
| 107 | let keyWhenFocused = useRef<Key | null>(null); |
| 108 | |
| 109 | // Handles focusing the cell. If there is a focusable child, |
| 110 | // it is focused, otherwise the cell itself is focused. |
| 111 | let focus = () => { |
| 112 | if (ref.current) { |
| 113 | let treeWalker = getFocusableTreeWalker(ref.current); |
| 114 | if (focusMode === 'child') { |
| 115 | // If focus is already on a focusable child within the cell, early return so we don't shift focus |
| 116 | if ( |
| 117 | isFocusWithin(ref.current) && |
| 118 | ref.current !== getActiveElement(getOwnerDocument(ref.current)) |
| 119 | ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | let focusable = |
| 124 | state.selectionManager.childFocusStrategy === 'last' |
| 125 | ? last(treeWalker) |
| 126 | : (treeWalker.firstChild() as FocusableElement); |
| 127 | if (focusable) { |
| 128 | focusSafely(focusable); |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if ( |
| 134 | (keyWhenFocused.current != null && node.key !== keyWhenFocused.current) || |
| 135 | !isFocusWithin(ref.current) |
| 136 | ) { |
| 137 | focusSafely(ref.current); |
| 138 | } |
| 139 | } |
| 140 | }; |
no test coverage detected