Skip to content

Commit ba8fb29

Browse files
authored
selectedPosition -> activePosition, column/row iterators (#3979)
* `selectedPosition -> `activePosition`, column/row iterators * isCellActive * review * review * review * review * review * review * review * review * review * last review * setPosition -> setActivePosition * return isCellActive
1 parent 36143b1 commit ba8fb29

34 files changed

Lines changed: 692 additions & 671 deletions

README.md

Lines changed: 55 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ An array of rows, the rows data can be of any type.
276276

277277
###### `ref?: Maybe<React.Ref<DataGridHandle>>`
278278

279-
Optional ref for imperative APIs like scrolling/selecting a cell. See [`DataGridHandle`](#datagridhandle).
279+
Optional ref for imperative APIs like scrolling to or focusing a cell. See [`DataGridHandle`](#datagridhandle).
280280

281281
###### `topSummaryRows?: Maybe<readonly SR[]>`
282282

@@ -507,11 +507,9 @@ function MyGrid() {
507507
}
508508
```
509509

510-
###### `onFill?: Maybe<(event: FillEvent<R>) => R>`
511-
512510
###### `onCellMouseDown?: CellMouseEventHandler<R, SR>`
513511

514-
Callback triggered when a pointer becomes active in a cell. The default behavior is to select the cell. Call `preventGridDefault` to prevent the default behavior.
512+
Callback triggered when a pointer becomes active in a cell. The default behavior is to focus the cell. Call `preventGridDefault` to prevent the default behavior.
515513

516514
```tsx
517515
function onCellMouseDown(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
@@ -542,7 +540,7 @@ This event can be used to open cell editor on single click
542540
```tsx
543541
function onCellClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
544542
if (args.column.key === 'id') {
545-
args.selectCell(true);
543+
args.setActivePosition(true);
546544
}
547545
}
548546
```
@@ -586,7 +584,7 @@ A function called when keydown event is triggered on a cell. This event can be u
586584

587585
```tsx
588586
function onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
589-
if (args.mode === 'SELECT' && event.key === 'Enter') {
587+
if (args.mode === 'ACTIVE' && event.key === 'Enter') {
590588
event.preventGridDefault();
591589
}
592590
}
@@ -596,7 +594,7 @@ function onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
596594

597595
```tsx
598596
function onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
599-
if (args.mode === 'SELECT' && event.key === 'Tab') {
597+
if (args.mode === 'ACTIVE' && event.key === 'Tab') {
600598
event.preventGridDefault();
601599
}
602600
}
@@ -614,15 +612,13 @@ Callback triggered when content is pasted into a cell.
614612

615613
Return the updated row; the grid will call `onRowsChange` with it.
616614

617-
###### `onSelectedCellChange?: Maybe<(args: CellSelectArgs<R, SR>) => void>`
615+
###### `onActivePositionChange?: Maybe<(args: PositionChangeArgs<R, SR>) => void>`
618616

619-
Triggered when the selected cell is changed.
617+
Triggered when the active position changes.
620618

621-
Arguments:
619+
See the [`PositionChangeArgs`](#positionchangeargstrow-tsummaryrow) type in the Types section below.
622620

623-
- `args.rowIdx`: `number` - row index
624-
- `args.row`: `R | undefined` - row object of the currently selected cell
625-
- `args.column`: `CalculatedColumn<TRow, TSummaryRow>` - column object of the currently selected cell
621+
###### `onFill?: Maybe<(event: FillEvent<R>) => R>`
626622

627623
###### `onScroll?: Maybe<(event: React.UIEvent<HTMLDivElement>) => void>`
628624

@@ -1370,7 +1366,7 @@ Control whether cells can be edited with `renderEditCell`.
13701366

13711367
##### `colSpan?: Maybe<(args: ColSpanArgs<TRow, TSummaryRow>) => Maybe<number>>`
13721368

1373-
Function to determine how many columns this cell should span. Returns the number of columns to span, or `undefined` for no spanning. See the `ColSpanArgs` type in the Types section below.
1369+
Function to determine how many columns this cell should span. Returns the number of columns to span, or `undefined` for no spanning. See the [`ColSpanArgs`](#colspanargstrow-tsummaryrow) type in the Types section below.
13741370

13751371
**Example:**
13761372

@@ -1583,7 +1579,7 @@ interface RenderEditCellProps<TRow, TSummaryRow = unknown> {
15831579
row: TRow;
15841580
rowIdx: number;
15851581
onRowChange: (row: TRow, commitChanges?: boolean) => void;
1586-
onClose: (commitChanges?: boolean, shouldFocusCell?: boolean) => void;
1582+
onClose: (commitChanges?: boolean, shouldFocus?: boolean) => void;
15871583
}
15881584
```
15891585

@@ -1639,17 +1635,17 @@ Props passed to custom row renderers.
16391635
```tsx
16401636
interface RenderRowProps<TRow, TSummaryRow = unknown> {
16411637
row: TRow;
1642-
viewportColumns: readonly CalculatedColumn<TRow, TSummaryRow>[];
1638+
iterateOverViewportColumnsForRow: IterateOverViewportColumnsForRow<TRow, TSummaryRow>;
16431639
rowIdx: number;
1644-
selectedCellIdx: number | undefined;
1645-
isRowSelected: boolean;
1640+
activeCellIdx: number | undefined;
16461641
isRowSelectionDisabled: boolean;
1642+
isRowSelected: boolean;
16471643
gridRowStart: number;
1648-
lastFrozenColumnIndex: number;
16491644
draggedOverCellIdx: number | undefined;
1650-
selectedCellEditor: ReactElement<RenderEditCellProps<TRow>> | undefined;
1645+
activeCellEditor: ReactElement<RenderEditCellProps<TRow>> | undefined;
16511646
onRowChange: (column: CalculatedColumn<TRow, TSummaryRow>, rowIdx: number, newRow: TRow) => void;
16521647
rowClass: Maybe<(row: TRow, rowIdx: number) => Maybe<string>>;
1648+
isTreeGrid: boolean;
16531649
// ... and event handlers
16541650
}
16551651
```
@@ -1658,7 +1654,7 @@ interface RenderRowProps<TRow, TSummaryRow = unknown> {
16581654

16591655
Props passed to the cell renderer when using `renderers.renderCell`.
16601656

1661-
Shares a base type with row render props (DOM props and cell event handlers) but only includes cell-specific fields like `column`, `row`, `rowIdx`, `colSpan`, and selection state.
1657+
Shares a base type with row render props (DOM props and cell event handlers) but only includes cell-specific fields like `column`, `row`, `rowIdx`, `colSpan`, and position state.
16621658

16631659
#### `Renderers<TRow, TSummaryRow>`
16641660

@@ -1680,37 +1676,25 @@ Arguments passed to cell mouse event handlers.
16801676

16811677
```tsx
16821678
interface CellMouseArgs<TRow, TSummaryRow = unknown> {
1679+
/** The column object of the cell. */
16831680
column: CalculatedColumn<TRow, TSummaryRow>;
1681+
/** The row object of the cell. */
16841682
row: TRow;
1683+
/** The row index of the cell. */
16851684
rowIdx: number;
1686-
selectCell: (enableEditor?: boolean) => void;
1685+
/** Function to manually focus the cell. Pass `true` to immediately start editing. */
1686+
setActivePosition: (enableEditor?: boolean) => void;
16871687
}
16881688
```
16891689

1690-
##### `column: CalculatedColumn<TRow, TSummaryRow>`
1691-
1692-
The column object of the cell.
1693-
1694-
##### `row: TRow`
1695-
1696-
The row object of the cell.
1697-
1698-
##### `rowIdx: number`
1699-
1700-
The row index of the cell.
1701-
1702-
##### `selectCell: (enableEditor?: boolean) => void`
1703-
1704-
Function to manually select the cell. Pass `true` to immediately start editing.
1705-
17061690
**Example:**
17071691

17081692
```tsx
17091693
import type { CellMouseArgs, CellMouseEvent } from 'react-data-grid';
17101694

17111695
function onCellClick(args: CellMouseArgs<Row>, event: CellMouseEvent) {
17121696
console.log('Clicked cell at row', args.rowIdx, 'column', args.column.key);
1713-
args.selectCell(true); // Select and start editing
1697+
args.setActivePosition(true); // Focus and start editing
17141698
}
17151699
```
17161700

@@ -1743,7 +1727,7 @@ import type { CellMouseArgs, CellMouseEvent } from 'react-data-grid';
17431727

17441728
function onCellClick(args: CellMouseArgs<Row>, event: CellMouseEvent) {
17451729
if (args.column.key === 'actions') {
1746-
event.preventGridDefault(); // Prevent cell selection
1730+
event.preventGridDefault(); // Prevent cell focus
17471731
}
17481732
}
17491733
```
@@ -1770,30 +1754,30 @@ type CellClipboardEvent = React.ClipboardEvent<HTMLDivElement>;
17701754

17711755
#### `CellKeyDownArgs<TRow, TSummaryRow>`
17721756

1773-
Arguments passed to the `onCellKeyDown` handler. The shape differs based on whether the cell is in SELECT or EDIT mode.
1757+
Arguments passed to the `onCellKeyDown` handler. The shape differs based on whether the cell is in ACTIVE or EDIT mode.
17741758

1775-
**SELECT mode:**
1759+
**ACTIVE mode:**
17761760

17771761
```tsx
1778-
interface SelectCellKeyDownArgs<TRow, TSummaryRow> {
1779-
mode: 'SELECT';
1780-
column: CalculatedColumn<TRow, TSummaryRow>;
1781-
row: TRow;
1762+
interface ActiveCellKeyDownArgs<TRow, TSummaryRow = unknown> {
1763+
mode: 'ACTIVE';
1764+
column: CalculatedColumn<TRow, TSummaryRow> | undefined;
1765+
row: TRow | undefined;
17821766
rowIdx: number;
1783-
selectCell: (position: Position, options?: SelectCellOptions) => void;
1767+
setActivePosition: (position: Position, options?: SetActivePositionOptions) => void;
17841768
}
17851769
```
17861770

17871771
**EDIT mode:**
17881772

17891773
```tsx
1790-
interface EditCellKeyDownArgs<TRow, TSummaryRow> {
1774+
interface EditCellKeyDownArgs<TRow, TSummaryRow = unknown> {
17911775
mode: 'EDIT';
17921776
column: CalculatedColumn<TRow, TSummaryRow>;
17931777
row: TRow;
17941778
rowIdx: number;
17951779
navigate: () => void;
1796-
onClose: (commitChanges?: boolean, shouldFocusCell?: boolean) => void;
1780+
onClose: (commitChanges?: boolean, shouldFocus?: boolean) => void;
17971781
}
17981782
```
17991783

@@ -1810,15 +1794,24 @@ function onCellKeyDown(args: CellKeyDownArgs<Row>, event: CellKeyboardEvent) {
18101794
}
18111795
```
18121796

1813-
#### `CellSelectArgs<TRow, TSummaryRow>`
1797+
#### `PositionChangeArgs<TRow, TSummaryRow>`
18141798

1815-
Arguments passed to `onSelectedCellChange`.
1799+
Arguments passed to `onActivePositionChange`.
18161800

18171801
```tsx
1818-
interface CellSelectArgs<TRow, TSummaryRow = unknown> {
1802+
interface PositionChangeArgs<TRow, TSummaryRow = unknown> {
1803+
/** row index of the active position */
18191804
rowIdx: number;
1805+
/**
1806+
* row object of the active position,
1807+
* undefined if the active position is on a header or summary row
1808+
*/
18201809
row: TRow | undefined;
1821-
column: CalculatedColumn<TRow, TSummaryRow>;
1810+
/**
1811+
* column object of the active position,
1812+
* undefined if the active position is a row instead of a cell
1813+
*/
1814+
column: CalculatedColumn<TRow, TSummaryRow> | undefined;
18221815
}
18231816
```
18241817

@@ -1850,9 +1843,9 @@ Arguments passed to the `colSpan` function.
18501843

18511844
```tsx
18521845
type ColSpanArgs<TRow, TSummaryRow> =
1853-
| { type: 'HEADER' }
1854-
| { type: 'ROW'; row: TRow }
1855-
| { type: 'SUMMARY'; row: TSummaryRow };
1846+
| { readonly type: 'HEADER' }
1847+
| { readonly type: 'ROW'; readonly row: TRow }
1848+
| { readonly type: 'SUMMARY'; readonly row: TSummaryRow };
18561849
```
18571850

18581851
**Example:**
@@ -1985,14 +1978,14 @@ interface Position {
19851978
}
19861979
```
19871980

1988-
#### `SelectCellOptions`
1981+
#### `SetActivePositionOptions`
19891982

1990-
Options for programmatically selecting a cell.
1983+
Options for programmatically updating the grid's active position.
19911984

19921985
```tsx
1993-
interface SelectCellOptions {
1986+
interface SetActivePositionOptions {
19941987
enableEditor?: Maybe<boolean>;
1995-
shouldFocusCell?: Maybe<boolean>;
1988+
shouldFocus?: Maybe<boolean>;
19961989
}
19971990
```
19981991

@@ -2050,8 +2043,8 @@ Handle type assigned to a grid's `ref` for programmatic grid control.
20502043
```tsx
20512044
interface DataGridHandle {
20522045
element: HTMLDivElement | null;
2053-
scrollToCell: (position: Partial<Position>) => void;
2054-
selectCell: (position: Position, options?: SelectCellOptions) => void;
2046+
scrollToCell: (position: PartialPosition) => void;
2047+
setActivePosition: (position: Position, options?: SetActivePositionOptions) => void;
20552048
}
20562049
```
20572050

src/Cell.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const cellDraggedOverClassname = `rdg-cell-dragged-over ${cellDraggedOver}`;
1616
function Cell<R, SR>({
1717
column,
1818
colSpan,
19-
isCellSelected,
19+
isCellActive,
2020
isDraggedOver,
2121
row,
2222
rowIdx,
@@ -30,11 +30,11 @@ function Cell<R, SR>({
3030
onContextMenu,
3131
onCellContextMenu,
3232
onRowChange,
33-
selectCell,
33+
setActivePosition,
3434
style,
3535
...props
3636
}: CellRendererProps<R, SR>) {
37-
const { tabIndex, childTabIndex, onFocus } = useRovingTabIndex(isCellSelected);
37+
const { tabIndex, childTabIndex, onFocus } = useRovingTabIndex(isCellActive);
3838

3939
const { cellClass } = column;
4040
className = getCellClassname(
@@ -47,8 +47,8 @@ function Cell<R, SR>({
4747
);
4848
const isEditable = isCellEditableUtil(column, row);
4949

50-
function selectCellWrapper(enableEditor?: boolean) {
51-
selectCell({ rowIdx, idx: column.idx }, { enableEditor });
50+
function setActivePositionWrapper(enableEditor?: boolean) {
51+
setActivePosition({ rowIdx, idx: column.idx }, { enableEditor });
5252
}
5353

5454
function handleMouseEvent(
@@ -58,7 +58,7 @@ function Cell<R, SR>({
5858
let eventHandled = false;
5959
if (eventHandler) {
6060
const cellEvent = createCellEvent(event);
61-
eventHandler({ rowIdx, row, column, selectCell: selectCellWrapper }, cellEvent);
61+
eventHandler({ rowIdx, row, column, setActivePosition: setActivePositionWrapper }, cellEvent);
6262
eventHandled = cellEvent.isGridDefaultPrevented();
6363
}
6464
return eventHandled;
@@ -68,7 +68,7 @@ function Cell<R, SR>({
6868
onMouseDown?.(event);
6969
if (!handleMouseEvent(event, onCellMouseDown)) {
7070
// select cell if the event is not prevented
71-
selectCellWrapper();
71+
setActivePositionWrapper();
7272
}
7373
}
7474

@@ -81,7 +81,7 @@ function Cell<R, SR>({
8181
onDoubleClick?.(event);
8282
if (!handleMouseEvent(event, onCellDoubleClick)) {
8383
// go into edit mode if the event is not prevented
84-
selectCellWrapper(true);
84+
setActivePositionWrapper(true);
8585
}
8686
}
8787

@@ -91,15 +91,15 @@ function Cell<R, SR>({
9191
}
9292

9393
function handleRowChange(newRow: R) {
94-
onRowChange(column, newRow);
94+
onRowChange(column, rowIdx, newRow);
9595
}
9696

9797
return (
9898
<div
9999
role="gridcell"
100100
aria-colindex={column.idx + 1} // aria-colindex is 1-based
101101
aria-colspan={colSpan}
102-
aria-selected={isCellSelected}
102+
aria-selected={isCellActive}
103103
aria-readonly={!isEditable || undefined}
104104
tabIndex={tabIndex}
105105
className={className}

0 commit comments

Comments
 (0)