MCPcopy Create free account
hub / github.com/adobe/react-spectrum / onKeyDownCapture

Function onKeyDownCapture

packages/react-aria/src/grid/useGridCell.ts:153–273  ·  view source on GitHub ↗
(e: ReactKeyboardEvent)

Source from the content-addressed store, hash-verified

151 });
152
153 let onKeyDownCapture = (e: ReactKeyboardEvent) => {
154 let activeElement = getActiveElement(getOwnerDocument(ref.current));
155 if (
156 !nodeContains(e.currentTarget, getEventTarget(e) as Element) ||
157 state.isKeyboardNavigationDisabled ||
158 !ref.current ||
159 !activeElement
160 ) {
161 return;
162 }
163
164 let walker = getFocusableTreeWalker(ref.current);
165 walker.currentNode = activeElement;
166
167 switch (e.key) {
168 case 'ArrowLeft': {
169 // Find the next focusable element within the cell.
170 let focusable: FocusableElement | null =
171 direction === 'rtl'
172 ? (walker.nextNode() as FocusableElement)
173 : (walker.previousNode() as FocusableElement);
174
175 // Don't focus the cell itself if focusMode is "child"
176 if (focusMode === 'child' && focusable === ref.current) {
177 focusable = null;
178 }
179
180 e.preventDefault();
181 e.stopPropagation();
182 if (focusable) {
183 focusSafely(focusable);
184 scrollIntoViewport(focusable, {containingElement: getScrollParent(ref.current)});
185 } else {
186 // If there is no next focusable child, then move to the next cell to the left of this one.
187 // This will be handled by useSelectableCollection. However, if there is no cell to the left
188 // of this one, only one column, and the grid doesn't focus rows, then the next key will be the
189 // same as this one. In that case we need to handle focusing either the cell or the first/last
190 // child, depending on the focus mode.
191 let prev = keyboardDelegate.getKeyLeftOf?.(node.key);
192 if (prev !== node.key) {
193 // We prevent the capturing event from reaching children of the cell, e.g. pickers.
194 // We want arrow keys to navigate to the next cell instead. We need to re-dispatch
195 // the event from a higher parent so it still bubbles and gets handled by useSelectableCollection.
196 ref.current.parentElement?.dispatchEvent(
197 new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
198 );
199 break;
200 }
201
202 if (focusMode === 'cell' && direction === 'rtl') {
203 focusSafely(ref.current);
204 scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
205 } else {
206 walker.currentNode = ref.current;
207 focusable =
208 direction === 'rtl' ? (walker.firstChild() as FocusableElement) : last(walker);
209 if (focusable) {
210 focusSafely(focusable);

Callers

nothing calls this directly

Calls 14

getActiveElementFunction · 0.90
getOwnerDocumentFunction · 0.90
nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
getFocusableTreeWalkerFunction · 0.90
focusSafelyFunction · 0.90
scrollIntoViewportFunction · 0.90
getScrollParentFunction · 0.90
nextNodeMethod · 0.80
previousNodeMethod · 0.80
lastFunction · 0.70
getKeyLeftOfMethod · 0.65

Tested by

no test coverage detected