Skip to content

Commit b5b930f

Browse files
authored
Fix table keyboard navigation bugs (#1993)
* fixing Tab/shift tab behavior for table * fixing option/alt+tabbing in table * updating comment * fixing lint * adding test for option + tab focus movement * preventing option + tab from moving focus in Table * simplifying fix for shift tabbing into TableView seems to work if we switch to scrollToItem from TableView instead of always using Virtualizer.scrolltoItem * adding aria labels
1 parent d6772b6 commit b5b930f

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

packages/@react-aria/selection/src/useSelectableCollection.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ export function useSelectableCollection(options: SelectableCollectionOptions): S
104104
let {direction} = useLocale();
105105

106106
let onKeyDown = (e: KeyboardEvent) => {
107+
// Prevent option + tab from doing anything since it doesn't move focus to the cells, only buttons/checkboxes
108+
if (e.altKey && e.key === 'Tab') {
109+
e.preventDefault();
110+
}
111+
107112
// Let child element (e.g. menu button) handle the event if the Alt key is pressed.
108113
// Keyboard events bubble through portals. Don't handle keyboard events
109114
// for elements outside the collection (e.g. menus).

packages/@react-aria/virtualizer/src/Virtualizer.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,16 @@ export function useVirtualizer<T extends object, V, W>(props: VirtualizerOptions
141141
let onFocus = useCallback((e: FocusEvent) => {
142142
// If the focused item is scrolled out of view and is not in the DOM, the collection
143143
// will have tabIndex={0}. When tabbing in from outside, scroll the focused item into view.
144-
// We only want to do this if the collection itself is receiving focus, not a child
145-
// element, and we aren't moving focus to the collection from within (see below).
146-
if (e.target === ref.current && !isFocusWithin.current) {
147-
virtualizer.scrollToItem(focusedKey, {duration: 0});
144+
if (!isFocusWithin.current) {
145+
if (scrollToItem) {
146+
scrollToItem(focusedKey);
147+
} else {
148+
virtualizer.scrollToItem(focusedKey, {duration: 0});
149+
}
148150
}
149151

150152
isFocusWithin.current = e.target !== ref.current;
151-
}, [ref, virtualizer, focusedKey]);
153+
}, [ref, virtualizer, focusedKey, scrollToItem]);
152154

153155
let onBlur = useCallback((e: FocusEvent) => {
154156
isFocusWithin.current = ref.current.contains(e.relatedTarget as Element);

packages/@react-spectrum/table/stories/Table.stories.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,20 +339,24 @@ storiesOf('TableView', module)
339339
.add(
340340
'many columns and rows',
341341
() => (
342-
<TableView aria-label="TableView with many columns and rows" selectionMode="multiple" width={700} height={500} onSelectionChange={s => onSelectionChange([...s])}>
343-
<TableHeader columns={manyColunns}>
344-
{column =>
345-
<Column minWidth={100}>{column.name}</Column>
346-
}
347-
</TableHeader>
348-
<TableBody items={manyRows}>
349-
{item =>
350-
(<Row key={item.foo}>
351-
{key => <Cell>{item[key]}</Cell>}
352-
</Row>)
353-
}
354-
</TableBody>
355-
</TableView>
342+
<>
343+
<input aria-label="Focusable before" placeholder="Focusable before" />
344+
<TableView aria-label="TableView with many columns and rows" selectionMode="multiple" width={700} height={500} onSelectionChange={s => onSelectionChange([...s])}>
345+
<TableHeader columns={manyColunns}>
346+
{column =>
347+
<Column minWidth={100}>{column.name}</Column>
348+
}
349+
</TableHeader>
350+
<TableBody items={manyRows}>
351+
{item =>
352+
(<Row key={item.foo}>
353+
{key => <Cell>{item[key]}</Cell>}
354+
</Row>)
355+
}
356+
</TableBody>
357+
</TableView>
358+
<input aria-label="Focusable after" placeholder="Focusable after" />
359+
</>
356360
),
357361
{chromatic: {disable: true}}
358362
)

0 commit comments

Comments
 (0)