Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions src/hooks/useRovingCellRef.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import { useRef, useState } from 'react';
import { useLayoutEffect } from './useLayoutEffect';
import { useCallback, useState } from 'react';

// https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_roving_tabindex
export function useRovingCellRef(isSelected: boolean) {
const ref = useRef<HTMLDivElement>(null);
// https://www.w3.org/TR/wai-aria-practices-1.1/#gridNav_focus
const isChildFocused = useRef(false);
const [, forceRender] = useState<unknown>({});
const [isChildFocused, setIsChildFocused] = useState(false);

useLayoutEffect(() => {
if (!isSelected) {
isChildFocused.current = false;
return;
}
if (isChildFocused && !isSelected) {
setIsChildFocused(false);
}

if (isChildFocused.current) {
// When the child is focused, we need to rerender
// the cell again so tabIndex is updated to -1
forceRender({});
return;
}
ref.current?.focus({ preventScroll: true });
}, [isSelected]);
const ref = useCallback((cell: HTMLDivElement | null) => {
if (cell === null || cell.contains(document.activeElement)) return;

cell.focus({ preventScroll: true });
}, []);

function onFocus(event: React.FocusEvent<HTMLDivElement>) {
if (event.target !== ref.current) {
isChildFocused.current = true;
if (event.target !== event.currentTarget) {
setIsChildFocused(true);
}
}

const isFocused = isSelected && !isChildFocused.current;
const isFocused = isSelected && !isChildFocused;

return {
ref,
ref: isSelected ? ref : undefined,
tabIndex: isFocused ? 0 : -1,
onFocus
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onFocus: isSelected ? onFocus: undefined Would this work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but then it does not catch programmatically focusing elements within a cell.
Then again we don't change the cell selection when we programmatically focus something within cells.

};
Expand Down
19 changes: 11 additions & 8 deletions test/components.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { StrictMode } from 'react';
import { forwardRef, StrictMode } from 'react';
import { render, screen } from '@testing-library/react';

import DataGrid, { DataGridDefaultComponentsProvider, SelectColumn } from '../src';
import type { Column, DataGridProps } from '../src';
import type { Column, DataGridProps, CheckboxFormatterProps } from '../src';
import { getRows, setup } from './utils';

interface Row {
Expand All @@ -25,13 +25,16 @@ function GlobalNoRowsFallback() {
return <div>Global no rows fallback</div>;
}

function Checkbox() {
return <div>Local checkbox</div>;
}
const Checkbox = forwardRef<HTMLDivElement, CheckboxFormatterProps>(function Checkbox(props, ref) {
return <div ref={ref}>Local checkbox</div>;
});

function GlobalCheckbox() {
return <div>Global checkbox</div>;
}
const GlobalCheckbox = forwardRef<HTMLDivElement, CheckboxFormatterProps>(function GlobalCheckbox(
props,
ref
) {
return <div ref={ref}>Global checkbox</div>;
});

function setupProvider<R, SR, K extends React.Key>(props: DataGridProps<R, SR, K>) {
return render(
Expand Down