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