Skip to content
Open
3 changes: 2 additions & 1 deletion src/Cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { css } from 'ecij';
import { useRovingTabIndex } from './hooks';
import { createCellEvent, getCellClassname, getCellStyle, isCellEditableUtil } from './utils';
import type { CellMouseEventHandler, RenderCellProps, SetActivePositionOptions } from './types';
import { cellActiveClassname } from './style/cell';

const cellDraggedOver = css`
@layer rdg.Cell {
Expand Down Expand Up @@ -43,6 +44,7 @@ function Cell<R, SR>({
const { cellClass } = column;
className = getCellClassname(
column,
isCellActive && cellActiveClassname,
isDraggedOver && cellDraggedOverClassname,
typeof cellClass === 'function' ? cellClass(row) : cellClass,
className
Expand Down Expand Up @@ -101,7 +103,6 @@ function Cell<R, SR>({
role="gridcell"
aria-colindex={column.idx + 1} // aria-colindex is 1-based
aria-colspan={colSpan > 1 ? colSpan : undefined}
aria-selected={isCellActive}
aria-readonly={!isEditable || undefined}
tabIndex={tabIndex}
className={className}
Expand Down
3 changes: 2 additions & 1 deletion src/EditCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { css } from 'ecij';

import { createCellEvent, getCellClassname, getCellStyle, onEditorNavigation } from './utils';
import type { EditCellProps } from './types';
import { cellActiveClassname } from './style/cell';

/*
* To check for outside `mousedown` events, we listen to all `mousedown` events at their birth,
Expand Down Expand Up @@ -187,6 +188,7 @@ export default function EditCell<R, SR>({
const { cellClass } = column;
const className = getCellClassname(
column,
cellActiveClassname,
'rdg-editor-container',
editCell,
!column.editorOptions?.displayCellContent && cellEditing,
Expand All @@ -199,7 +201,6 @@ export default function EditCell<R, SR>({
role="gridcell"
aria-colindex={column.idx + 1} // aria-colindex is 1-based
aria-colspan={colSpan > 1 ? colSpan : undefined}
aria-selected
className={className}
style={getCellStyle(column, colSpan)}
onKeyDownCapture={handleKeyDownCapture}
Expand Down
4 changes: 2 additions & 2 deletions src/GroupCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { memo } from 'react';
import { useRovingTabIndex } from './hooks';
import { getCellClassname, getCellStyle } from './utils';
import type { CalculatedColumn, GroupRow } from './types';
import { cellActiveClassname } from './style/cell';

interface GroupCellProps<R, SR> {
column: CalculatedColumn<R, SR>;
Expand Down Expand Up @@ -33,12 +34,11 @@ function GroupCell<R, SR>({
key={column.key}
role="gridcell"
aria-colindex={column.idx + 1}
aria-selected={isCellActive}
// tabIndex={undefined} prevents clicks on the cell
// from stealing focus from the row.
// onMouseDown={preventDefault} would break mousewheel clicks
tabIndex={tabIndex === -1 ? undefined : tabIndex}
className={getCellClassname(column)}
className={getCellClassname(column, isCellActive && cellActiveClassname)}
style={{
...getCellStyle(column, 1),
cursor: isLevelMatching ? 'pointer' : 'default'
Expand Down
9 changes: 6 additions & 3 deletions src/GroupedColumnHeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRovingTabIndex } from './hooks';
import { classnames, getHeaderCellRowSpan, getHeaderCellStyle } from './utils';
import type { CalculatedColumnParent } from './types';
import type { GroupedColumnHeaderRowProps } from './GroupedColumnHeaderRow';
import { cellClassname } from './style/cell';
import { cellActiveClassname, cellClassname } from './style/cell';

type SharedGroupedColumnHeaderRowProps<R, SR> = Pick<
GroupedColumnHeaderRowProps<R, SR>,
Expand Down Expand Up @@ -35,9 +35,12 @@ export default function GroupedColumnHeaderCell<R, SR>({
aria-colindex={index}
aria-colspan={colSpan}
aria-rowspan={rowSpan}
aria-selected={isCellActive}
tabIndex={tabIndex}
className={classnames(cellClassname, column.headerCellClass)}
className={classnames(
cellClassname,
isCellActive && cellActiveClassname,
column.headerCellClass
)}
style={{
...getHeaderCellStyle(column, rowIdx, rowSpan),
gridColumnStart: index,
Expand Down
3 changes: 2 additions & 1 deletion src/HeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './utils';
import type { CalculatedColumn, SortColumn } from './types';
import type { HeaderRowProps } from './HeaderRow';
import { cellActiveClassname } from './style/cell';

const cellSortableClassname = css`
@layer rdg.HeaderCell {
Expand Down Expand Up @@ -121,6 +122,7 @@ export default function HeaderCell<R, SR>({

const className = getCellClassname(
column,
isCellActive && cellActiveClassname,
column.headerCellClass,
sortable && cellSortableClassname,
resizable && cellResizableClassname,
Expand Down Expand Up @@ -297,7 +299,6 @@ export default function HeaderCell<R, SR>({
aria-colindex={column.idx + 1}
aria-colspan={colSpan > 1 ? colSpan : undefined}
aria-rowspan={rowSpan}
aria-selected={isCellActive}
aria-sort={ariaSort}
tabIndex={tabIndex}
className={className}
Expand Down
3 changes: 2 additions & 1 deletion src/SummaryCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { memo } from 'react';
import { useRovingTabIndex } from './hooks';
import { getCellClassname, getCellStyle } from './utils';
import type { RenderCellProps } from './types';
import { cellActiveClassname } from './style/cell';

type SharedRenderCellProps<R, SR> = Pick<
RenderCellProps<R, SR>,
Expand All @@ -25,6 +26,7 @@ function SummaryCell<R, SR>({
const { summaryCellClass } = column;
const className = getCellClassname(
column,
isCellActive && cellActiveClassname,
typeof summaryCellClass === 'function' ? summaryCellClass(row) : summaryCellClass
);

Expand All @@ -37,7 +39,6 @@ function SummaryCell<R, SR>({
role="gridcell"
aria-colindex={column.idx + 1}
aria-colspan={colSpan > 1 ? colSpan : undefined}
aria-selected={isCellActive}
tabIndex={tabIndex}
className={className}
style={getCellStyle(column, colSpan)}
Expand Down
15 changes: 10 additions & 5 deletions src/style/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ export const cell = css`
overflow: clip;
text-overflow: ellipsis;
outline: none;

&[aria-selected='true'] {
outline: var(--rdg-selection-width) solid var(--rdg-selection-color);
outline-offset: calc(var(--rdg-selection-width) * -1);
}
}
`;

export const cellClassname = `rdg-cell ${cell}`;

// Declared after `cell` so its outline overrides `outline: none` in the same layer.
const cellActive = css`
@layer rdg.Cell {
outline: var(--rdg-selection-width) solid var(--rdg-selection-color);
outline-offset: calc(var(--rdg-selection-width) * -1);
}
`;

export const cellActiveClassname = `rdg-cell-active ${cellActive}`;

// Single shared sticky/z-index rule reused by both edge-frozen variants.
// Selectors that need to distinguish start vs end use the marker classes
// (`.rdg-cell-frozen-start` / `.rdg-cell-frozen-end`) directly rather than this ecij identifier.
Expand Down
14 changes: 7 additions & 7 deletions test/browser/TreeDataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,24 +475,24 @@ test('cell navigation in a treegrid', async () => {
// focus cell
const cells = getCellsAtRowIndex(5);
await userEvent.click(cells.nth(1));
await expect.element(cells.nth(1)).toHaveAttribute('aria-selected', 'true');
await expect.element(cells.nth(1)).toHaveClass('rdg-cell-active');
await expect.element(cells.nth(1)).toHaveFocus();
await expect.element(cells.nth(1)).toHaveAttribute('tabIndex', '0');

// focus the previous cell
await userEvent.keyboard('{arrowleft}');
await expect.element(cells.nth(1)).toHaveAttribute('aria-selected', 'false');
await expect.element(cells.nth(0)).toHaveAttribute('aria-selected', 'true');
await expect.element(cells.nth(1)).not.toHaveClass('rdg-cell-active');
await expect.element(cells.nth(0)).toHaveClass('rdg-cell-active');

// if the first cell is focused then arrowleft should focus the row
await userEvent.keyboard('{arrowleft}');
await expect.element(cells.nth(0)).toHaveAttribute('aria-selected', 'false');
await expect.element(cells.nth(0)).not.toHaveClass('rdg-cell-active');
await expect.element(rows.nth(4)).toHaveClass(rowActiveClassname);
await expect.element(rows.nth(4)).toHaveFocus();

// if the row is focused then arrowright should focus the first cell on the same row
await userEvent.keyboard('{arrowright}');
await expect.element(cells.nth(0)).toHaveAttribute('aria-selected', 'true');
await expect.element(cells.nth(0)).toHaveClass('rdg-cell-active');

await userEvent.keyboard('{arrowleft}{arrowup}');

Expand Down Expand Up @@ -624,11 +624,11 @@ test('onCellKeyDown and onActivePositionChange receive group rows', async () =>

// activate the first cell of the group row
await userEvent.keyboard('{arrowright}');
await expect.element(usaGroupRow.getCell().nth(0)).toHaveAttribute('aria-selected', 'true');
await expect.element(usaGroupRow.getCell().nth(0)).toHaveClass('rdg-cell-active');

// activate a cell of a regular row
await userEvent.click(page.getCell({ name: '2020' }));
await expect.element(page.getCell({ name: '2020' })).toHaveAttribute('aria-selected', 'true');
await expect.element(page.getCell({ name: '2020' })).toHaveClass('rdg-cell-active');
await userEvent.keyboard('{arrowright}');
await expect.element(activeCell).toHaveTextContent('value: 1');

Expand Down
6 changes: 3 additions & 3 deletions test/browser/events.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const rows: readonly Row[] = [
];

describe('Events', () => {
it('should not select cell if onCellMouseDown prevents grid default', async () => {
it('should not activate the cell if onCellMouseDown prevents grid default', async () => {
await page.render(
<DataGrid
columns={columns}
Expand All @@ -65,9 +65,9 @@ describe('Events', () => {
/>
);
await userEvent.click(page.getCell({ name: '1' }));
await expect.element(page.getCell({ name: '1' })).toHaveAttribute('aria-selected', 'false');
await expect.element(page.getCell({ name: '1' })).not.toHaveClass('rdg-cell-active');
await userEvent.click(page.getCell({ name: 'a1' }));
await expect.element(page.getCell({ name: 'a1' })).toHaveAttribute('aria-selected', 'true');
await expect.element(page.getCell({ name: 'a1' })).toHaveClass('rdg-cell-active');
});

it('should activate and focus the cell if onCellMouseDown calls setActivePosition with shouldFocus', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/setupBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ locators.extend({
},

getActiveCell() {
return this.getCell({ selected: true }).or(this.getHeaderCell({ selected: true }));
return '.rdg-cell-active';
},

getDragHandle() {
Expand Down