Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 19 #3503

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5590f80
React 19
nstepien Apr 26, 2024
b27490c
Merge branch 'main' into react19
amanmahajan7 Jul 8, 2024
ad5099d
Merge remote-tracking branch 'origin/main' into react19
nstepien Jul 15, 2024
ebc2830
beta -> rc
nstepien Jul 15, 2024
02c5eb7
Merge remote-tracking branch 'origin/main' into react19
nstepien Jul 15, 2024
929e6ca
Merge branch 'main' into react19
amanmahajan7 Jul 18, 2024
b057c8f
Merge branch 'main' into react19
nstepien Jul 18, 2024
06fb414
remove patch script
nstepien Jul 18, 2024
8cd5916
Merge branch 'main' into react19
amanmahajan7 Jul 18, 2024
4bfba76
-forwardRef
nstepien Jul 19, 2024
8a74558
use ComponentProps
nstepien Jul 19, 2024
5d1c3bf
import useLayoutEffect directly
nstepien Jul 19, 2024
c01a5b8
use contexts directly
nstepien Jul 19, 2024
fe5eb24
fix lint
nstepien Jul 19, 2024
85c0564
cleanup useRef type
nstepien Jul 19, 2024
27ee3fd
Merge branch 'main' into react19
nstepien Jul 23, 2024
5246a72
Merge remote-tracking branch 'origin/main' into react19
nstepien Sep 11, 2024
52c0643
add missing exports
nstepien Sep 11, 2024
6954550
Merge branch 'main' into react19
nstepien Sep 25, 2024
97c7af2
Merge branch 'main' into react19
amanmahajan7 Oct 8, 2024
dbdaeff
Merge branch 'main' into react19
amanmahajan7 Nov 13, 2024
4809482
Merge branch 'main' into react19
nstepien Dec 21, 2024
142d12b
use stable React 19
nstepien Dec 21, 2024
e6bc742
provider -> context
nstepien Dec 22, 2024
8040416
-1 forwardRef
nstepien Dec 22, 2024
df10bc1
add forceConsistentCasingInFileNames
nstepien Dec 22, 2024
a67188f
Merge branch 'main' into react19
nstepien Dec 29, 2024
d8c5f9a
Merge branch 'main' into react19
nstepien Jan 5, 2025
801943a
Merge branch 'main' into react19
amanmahajan7 Jan 7, 2025
dbf8cb1
Merge branch 'main' into react19
nstepien Jan 20, 2025
7ace72b
bump
nstepien Jan 20, 2025
b50c390
remove reference to old vitest.workspace.ts
nstepien Jan 20, 2025
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
12 changes: 1 addition & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,12 @@ on:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
react: [18, 19]
fail-fast: false
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 23
check-latest: true
- name: set up react 19
if: matrix.react == 19
run: |
node ./.github/workflows/patch-react19.js
cat package.json
- name: npm install
run: npm i
- name: Biome
Expand All @@ -45,12 +36,11 @@ jobs:
run: node --run test
timeout-minutes: 4
- name: Upload coverage
if: matrix.react == 18
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Deploy gh-pages
if: matrix.react == 18 && github.event_name == 'push' && github.ref == 'refs/heads/main'
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
git config --global user.email '[email protected]'
git config --global user.name 'GitHub Action'
Expand Down
15 changes: 0 additions & 15 deletions .github/workflows/patch-react19.js

This file was deleted.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

## Features

- [React 18.0+](package.json) support
- [React 19.0+](package.json) support
- [Evergreen browsers and server-side rendering](browserslist) support
- Tree-shaking support and only [one npm dependency](package.json) to keep your bundles slim
- Great performance thanks to virtualization: columns and rows outside the viewport are not rendered
Expand Down Expand Up @@ -419,16 +419,16 @@ interface Renderers<TRow, TSummaryRow> {
}
```

For example, the default `<Row />` component can be wrapped via the `renderRow` prop to add context providers or tweak props
For example, the default `<Row />` component can be wrapped via the `renderRow` prop to add contexts or tweak props

```tsx
import DataGrid, { RenderRowProps, Row } from 'react-data-grid';

function myRowRenderer(key: React.Key, props: RenderRowProps<Row>) {
return (
<MyContext.Provider key={key} value={123}>
<MyContext key={key} value={123}>
<Row {...props} />
</MyContext.Provider>
</MyContext>
);
}

Expand Down
5 changes: 0 additions & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,6 @@ export default [
importNames: ['default'],
message: 'Use named imports instead.'
},
{
name: 'react',
importNames: ['useLayoutEffect'],
message: 'Use the override from src/hooks instead.'
},
{
name: 'react-dom',
importNames: ['default'],
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"@testing-library/react": "^16.1.0",
"@testing-library/user-event": "^14.5.2",
"@types/node": "^22.0.0",
"@types/react": "^18.3.9",
"@types/react-dom": "^18.3.0",
"@types/react": "^19.0.7",
"@types/react-dom": "^19.0.3",
"@typescript-eslint/eslint-plugin": "^8.19.1",
"@typescript-eslint/parser": "^8.19.1",
"@vitejs/plugin-react": "^4.3.4",
Expand All @@ -93,10 +93,10 @@
"playwright": "^1.49.1",
"postcss": "^8.4.25",
"prettier": "3.4.2",
"react": "^18.3.1",
"react": "^19.0.0",
"react-dnd": "^16.0.1",
"react-dnd-html5-backend": "^16.0.1",
"react-dom": "^18.3.1",
"react-dom": "^19.0.0",
"rolldown": "^1.0.0-beta.1",
"typescript": "~5.7.2",
"vite": "^6.0.3",
Expand Down
42 changes: 19 additions & 23 deletions src/Cell.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, memo, type RefAttributes } from 'react';
import { memo, type RefAttributes } from 'react';
import { css } from '@linaria/core';

import { useRovingTabIndex } from './hooks';
Expand All @@ -25,26 +25,23 @@ const cellDraggedOver = css`

const cellDraggedOverClassname = `rdg-cell-dragged-over ${cellDraggedOver}`;

function Cell<R, SR>(
{
column,
colSpan,
isCellSelected,
isCopied,
isDraggedOver,
row,
rowIdx,
className,
onClick,
onDoubleClick,
onContextMenu,
onRowChange,
selectCell,
style,
...props
}: CellRendererProps<R, SR>,
ref: React.Ref<HTMLDivElement>
) {
function Cell<R, SR>({
column,
colSpan,
isCellSelected,
isCopied,
isDraggedOver,
row,
rowIdx,
className,
onClick,
onDoubleClick,
onContextMenu,
onRowChange,
selectCell,
style,
...props
}: CellRendererProps<R, SR>) {
const { tabIndex, childTabIndex, onFocus } = useRovingTabIndex(isCellSelected);

const { cellClass } = column;
Expand Down Expand Up @@ -101,7 +98,6 @@ function Cell<R, SR>(
aria-colspan={colSpan}
aria-selected={isCellSelected}
aria-readonly={!isEditable || undefined}
ref={ref}
tabIndex={tabIndex}
className={className}
style={{
Expand All @@ -126,7 +122,7 @@ function Cell<R, SR>(
);
}

const CellComponent = memo(forwardRef(Cell)) as <R, SR>(
const CellComponent = memo(Cell) as <R, SR>(
props: CellRendererProps<R, SR> & RefAttributes<HTMLDivElement>
) => React.JSX.Element;

Expand Down
51 changes: 26 additions & 25 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react';
import type { Key, KeyboardEvent, RefAttributes } from 'react';
import {
useCallback,
useImperativeHandle,
useLayoutEffect,
useMemo,
useRef,
useState
} from 'react';
import type { Key, KeyboardEvent } from 'react';
import { flushSync } from 'react-dom';
import clsx from 'clsx';

import {
HeaderRowSelectionChangeProvider,
HeaderRowSelectionProvider,
RowSelectionChangeProvider,
HeaderRowSelectionChangeContext,
HeaderRowSelectionContext,
RowSelectionChangeContext,
useCalculatedColumns,
useColumnWidths,
useGridDimensions,
useLatestFunc,
useLayoutEffect,
useViewportColumns,
useViewportRows,
type HeaderRowSelectionContextValue
Expand Down Expand Up @@ -55,9 +61,9 @@ import type {
import { defaultRenderCell } from './Cell';
import { renderCheckbox as defaultRenderCheckbox } from './cellRenderers';
import {
DataGridDefaultRenderersProvider,
DataGridDefaultRenderersContext,
useDefaultRenderers
} from './DataGridDefaultRenderersProvider';
} from './DataGridDefaultRenderersContext';
import DragHandle from './DragHandle';
import EditCell from './EditCell';
import GroupedColumnHeaderRow from './GroupedColumnHeaderRow';
Expand Down Expand Up @@ -97,7 +103,7 @@ export interface DataGridHandle {
}

type SharedDivProps = Pick<
React.HTMLAttributes<HTMLDivElement>,
React.ComponentProps<'div'>,
| 'role'
| 'aria-label'
| 'aria-labelledby'
Expand All @@ -109,6 +115,7 @@ type SharedDivProps = Pick<
>;

export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends SharedDivProps {
ref?: Maybe<React.Ref<DataGridHandle>>;
/**
* Grid and data Props
*/
Expand Down Expand Up @@ -215,11 +222,9 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
*
* <DataGrid columns={columns} rows={rows} />
*/
function DataGrid<R, SR, K extends Key>(
props: DataGridProps<R, SR, K>,
ref: React.Ref<DataGridHandle>
) {
export default function DataGrid<R, SR, K extends Key>(props: DataGridProps<R, SR, K>) {
const {
ref,
// Grid and data Props
columns: rawColumns,
rows,
Expand Down Expand Up @@ -1126,9 +1131,9 @@ function DataGrid<R, SR, K extends Key>(
data-testid={testId}
data-cy={dataCy}
>
<DataGridDefaultRenderersProvider value={defaultGridComponents}>
<HeaderRowSelectionChangeProvider value={selectHeaderRowLatest}>
<HeaderRowSelectionProvider value={headerSelectionValue}>
<DataGridDefaultRenderersContext value={defaultGridComponents}>
<HeaderRowSelectionChangeContext value={selectHeaderRowLatest}>
<HeaderRowSelectionContext value={headerSelectionValue}>
{Array.from({ length: groupedColumnHeaderRowsCount }, (_, index) => (
<GroupedColumnHeaderRow
key={index}
Expand Down Expand Up @@ -1156,8 +1161,8 @@ function DataGrid<R, SR, K extends Key>(
shouldFocusGrid={!selectedCellIsWithinSelectionBounds}
direction={direction}
/>
</HeaderRowSelectionProvider>
</HeaderRowSelectionChangeProvider>
</HeaderRowSelectionContext>
</HeaderRowSelectionChangeContext>
{rows.length === 0 && noRowsFallback ? (
noRowsFallback
) : (
Expand Down Expand Up @@ -1185,10 +1190,10 @@ function DataGrid<R, SR, K extends Key>(
/>
);
})}
<RowSelectionChangeProvider value={selectRowLatest}>
<RowSelectionChangeContext value={selectRowLatest}>
{/* eslint-disable-next-line react-compiler/react-compiler */}
{getViewportRows()}
</RowSelectionChangeProvider>
</RowSelectionChangeContext>
{bottomSummaryRows?.map((row, rowIdx) => {
const gridRowStart = headerAndTopSummaryRowsCount + rows.length + rowIdx + 1;
const summaryRowIdx = rows.length + rowIdx;
Expand Down Expand Up @@ -1221,7 +1226,7 @@ function DataGrid<R, SR, K extends Key>(
})}
</>
)}
</DataGridDefaultRenderersProvider>
</DataGridDefaultRenderersContext>

{renderDragHandle()}

Expand Down Expand Up @@ -1264,7 +1269,3 @@ function getCellToScroll(gridEl: HTMLDivElement) {
function isSamePosition(p1: Position, p2: Position) {
return p1.idx === p2.idx && p1.rowIdx === p2.rowIdx;
}

export default forwardRef(DataGrid) as <R, SR = unknown, K extends Key = Key>(
props: DataGridProps<R, SR, K> & RefAttributes<DataGridHandle>
) => React.JSX.Element;
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { createContext, useContext } from 'react';
import type { Maybe, Renderers } from './types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const DataGridDefaultRenderersContext = createContext<Maybe<Renderers<any, any>>>(undefined);

export const DataGridDefaultRenderersProvider = DataGridDefaultRenderersContext.Provider;
export const DataGridDefaultRenderersContext = createContext<Maybe<Renderers<any, any>>>(undefined);

export function useDefaultRenderers<R, SR>(): Maybe<Renderers<R, SR>> {
return useContext(DataGridDefaultRenderersContext);
Expand Down
7 changes: 1 addition & 6 deletions src/DragHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ const cellDragHandleFrozenClassname = css`

const cellDragHandleClassname = `rdg-cell-drag-handle ${cellDragHandle}`;

// TODO: replace with RefObject once we drop support for React 18
interface LatestDraggedOverRowIdxRef {
readonly current: number | undefined;
}

interface Props<R, SR> extends Pick<DataGridProps<R, SR>, 'rows' | 'onRowsChange'> {
gridRowStart: number;
column: CalculatedColumn<R, SR>;
columnWidth: number | string;
maxColIdx: number;
isLastRow: boolean;
selectedPosition: SelectCellState;
latestDraggedOverRowIdx: LatestDraggedOverRowIdxRef;
latestDraggedOverRowIdx: React.RefObject<number | undefined>;
isCellEditable: (position: Position) => boolean;
onClick: () => void;
onFill: (event: FillEvent<R>) => R;
Expand Down
2 changes: 1 addition & 1 deletion src/EditCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function EditCell<R, SR>({
onKeyDown,
navigate
}: EditCellProps<R, SR>) {
const frameRequestRef = useRef<number | undefined>(undefined);
const frameRequestRef = useRef<number>(undefined);
const commitOnOutsideClick = column.editorOptions?.commitOnOutsideClick !== false;

// We need to prevent the `useEffect` from cleaning up between re-renders,
Expand Down
6 changes: 3 additions & 3 deletions src/GroupRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { memo, useMemo } from 'react';
import { css } from '@linaria/core';
import clsx from 'clsx';

import { RowSelectionProvider, type RowSelectionContextValue } from './hooks';
import { RowSelectionContext, type RowSelectionContextValue } from './hooks';
import { getRowStyle } from './utils';
import type { BaseRenderRowProps, GroupRow } from './types';
import { SELECT_COLUMN_KEY } from './Columns';
Expand Down Expand Up @@ -58,7 +58,7 @@ function GroupedRow<R, SR>({
);

return (
<RowSelectionProvider value={selectionValue}>
<RowSelectionContext value={selectionValue}>
<div
role="row"
aria-level={row.level + 1} // aria-level is 1-based
Expand Down Expand Up @@ -92,7 +92,7 @@ function GroupedRow<R, SR>({
/>
))}
</div>
</RowSelectionProvider>
</RowSelectionContext>
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/HeaderCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export default function HeaderCell<R, SR>({
}
}

let draggableProps: React.HTMLAttributes<HTMLDivElement> | undefined;
let draggableProps: React.ComponentProps<'div'> | undefined;
if (draggable) {
draggableProps = {
draggable: true,
Expand Down
Loading
Loading