Skip to content

Commit 1aacf8f

Browse files
committed
add focusVisibleWithin to table cell as well
1 parent 3112a73 commit 1aacf8f

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

packages/react-aria-components/src/Table.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,9 @@ export interface CellRenderProps {
661661
* Whether the cell is currently hovered with a mouse.
662662
* @selector [data-hovered]
663663
*/
664-
isHovered: boolean
664+
isHovered: boolean,
665+
/** Whether the cell's children have keyboard focus. */
666+
isFocusVisibleWithin: boolean
665667
}
666668

667669
export interface CellProps extends RenderProps<CellRenderProps> {
@@ -1020,6 +1022,8 @@ function TableRow<T>({item}: {item: GridNode<T>}) {
10201022
ref
10211023
);
10221024
let {isFocused, isFocusVisible, focusProps} = useFocusRing();
1025+
// TODO: would we also want to to check isFocusWithin as well?
1026+
// TODO: Should isFocusVisible be (isFocusVisible || isFocusVisibleWithin) or should they exist as separate states
10231027
let {
10241028
isFocusVisible: isFocusVisibleWithin,
10251029
focusProps: focusWithinProps
@@ -1161,6 +1165,10 @@ function TableCell<T>({cell}: {cell: GridNode<T>}) {
11611165
shouldSelectOnPressUp: !!dragState
11621166
}, state, ref);
11631167
let {isFocused, isFocusVisible, focusProps} = useFocusRing();
1168+
let {
1169+
isFocusVisible: isFocusVisibleWithin,
1170+
focusProps: focusWithinProps
1171+
} = useFocusRing({within: true});
11641172
let {hoverProps, isHovered} = useHover({});
11651173

11661174
let props: CellProps = cell.props;
@@ -1172,18 +1180,20 @@ function TableCell<T>({cell}: {cell: GridNode<T>}) {
11721180
isFocused,
11731181
isFocusVisible,
11741182
isPressed,
1175-
isHovered
1183+
isHovered,
1184+
isFocusVisibleWithin
11761185
}
11771186
});
11781187

11791188
return (
11801189
<td
1181-
{...mergeProps(filterDOMProps(props as any), gridCellProps, focusProps, hoverProps)}
1190+
{...mergeProps(filterDOMProps(props as any), gridCellProps, focusProps, hoverProps, focusWithinProps)}
11821191
{...renderProps}
11831192
ref={ref}
11841193
data-focused={isFocused || undefined}
11851194
data-focus-visible={isFocusVisible || undefined}
1186-
data-pressed={isPressed || undefined}>
1195+
data-pressed={isPressed || undefined}
1196+
data-focus-visible-within={isFocusVisibleWithin || undefined}>
11871197
{renderProps.children}
11881198
</td>
11891199
);

packages/react-aria-components/test/Table.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,8 @@ describe('Table', () => {
769769
expect(onScroll).toHaveBeenCalled();
770770
});
771771

772-
it('should support data-focus-visible-within', async () => {
773-
let {getAllByRole} = renderTable();
772+
it('should support data-focus-visible-within for rows and cell', async () => {
773+
let {getAllByRole} = renderTable({tableProps: {selectionMode: 'multiple'}});
774774
let items = getAllByRole('row');
775775
expect(items[1]).not.toHaveAttribute('data-focus-visible-within', 'true');
776776

@@ -779,9 +779,18 @@ describe('Table', () => {
779779
expect(items[1]).toHaveAttribute('data-focus-visible-within', 'true');
780780
await user.keyboard('{ArrowRight}');
781781

782-
let cell = within(items[1]).getAllByRole('rowheader')[0];
783-
expect(document.activeElement).toBe(cell);
784-
expect(cell).toHaveAttribute('data-focus-visible', 'true');
782+
let checkboxCell = within(items[1]).getAllByRole('gridcell')[0];
783+
let checkbox = within(items[1]).getByRole('checkbox');
784+
expect(document.activeElement).toBe(checkbox);
785+
expect(checkboxCell).not.toHaveAttribute('data-focus-visible', 'true');
786+
expect(checkboxCell).toHaveAttribute('data-focus-visible-within', 'true');
787+
expect(items[1]).toHaveAttribute('data-focus-visible-within', 'true');
788+
await user.keyboard('{ArrowRight}');
789+
790+
let tableCell = within(items[1]).getAllByRole('rowheader')[0];
791+
expect(document.activeElement).toBe(tableCell);
792+
expect(tableCell).toHaveAttribute('data-focus-visible', 'true');
793+
expect(tableCell).toHaveAttribute('data-focus-visible-within', 'true');
785794
expect(items[1]).toHaveAttribute('data-focus-visible-within', 'true');
786795

787796
await user.keyboard('{ArrowDown}');

0 commit comments

Comments
 (0)