Skip to content

Commit a2085b6

Browse files
committed
Merge remote-tracking branch 'origin/main' into firefox
2 parents ea06744 + 3abb8f3 commit a2085b6

26 files changed

Lines changed: 485 additions & 291 deletions

README.md

Lines changed: 98 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[size-url]: https://bundlephobia.com/package/react-data-grid
1313
[type-badge]: https://img.shields.io/npm/types/react-data-grid
1414
[codecov-badge]: https://codecov.io/gh/adazzle/react-data-grid/branch/main/graph/badge.svg?token=cvrRSWiz0Q
15-
[codecov-url]: https://app.codecov.io/gh/adazzle/react-data-grid/branch/main
15+
[codecov-url]: https://app.codecov.io/gh/adazzle/react-data-grid
1616
[ci-badge]: https://github.com/adazzle/react-data-grid/workflows/CI/badge.svg
1717
[ci-url]: https://github.com/adazzle/react-data-grid/actions
1818

@@ -25,7 +25,7 @@ The DataGrid component is designed to handle large datasets efficiently while of
2525
- Tree-shaking support and only [one npm dependency](package.json) to keep your bundles slim
2626
- Great performance thanks to virtualization: columns and rows outside the viewport are not rendered
2727
- Strictly typed with TypeScript
28-
- [Keyboard accessibility](<(https://adazzle.github.io/react-data-grid/#/CommonFeatures)>)
28+
- [Keyboard accessibility](https://adazzle.github.io/react-data-grid/#/CommonFeatures)
2929
- Light and dark mode support out of the box. The light or dark themes can be enforced using the `rdg-light` or `rdg-dark` classes.
3030
- [Frozen columns](https://adazzle.github.io/react-data-grid/#/CommonFeatures): Freeze columns to keep them visible during horizontal scrolling.
3131
- [Column resizing](https://adazzle.github.io/react-data-grid/#/CommonFeatures)
@@ -55,14 +55,24 @@ The DataGrid component is designed to handle large datasets efficiently while of
5555

5656
## Installation
5757

58-
to install `react-data-grid`, use npm or yarn:
58+
Install `react-data-grid` using your favorite package manager:
59+
60+
```sh
61+
npm i react-data-grid
62+
```
63+
64+
```sh
65+
pnpm add react-data-grid
66+
```
5967

6068
```sh
61-
npm install react-data-grid
62-
# or
6369
yarn add react-data-grid
6470
```
6571

72+
```sh
73+
bun add react-data-grid
74+
```
75+
6676
Additionally, import the default styles in your application:
6777

6878
```tsx
@@ -78,14 +88,19 @@ Here is a basic example of how to use `react-data-grid` in your React applicatio
7888
```tsx
7989
import 'react-data-grid/lib/styles.css';
8090

81-
import { DataGrid } from 'react-data-grid';
91+
import { DataGrid, type Column } from 'react-data-grid';
8292

83-
const columns = [
93+
interface Row {
94+
id: number;
95+
title: string;
96+
}
97+
98+
const columns: Column<Row>[] = [
8499
{ key: 'id', name: 'ID' },
85100
{ key: 'title', name: 'Title' }
86101
];
87102

88-
const rows = [
103+
const rows: Row[] = [
89104
{ id: 0, title: 'Example' },
90105
{ id: 1, title: 'Demo' }
91106
];
@@ -195,7 +210,7 @@ function addNewRow() {
195210
setColumnWidths(new Map());
196211
}
197212

198-
return <DataGrid columnWidths={columnWidths} onColumnWidthsChange={setColumnWidths} ../>
213+
return <DataGrid columnWidths={columnWidths} onColumnWidthsChange={setColumnWidths} ... />
199214
```
200215

201216
###### `onColumnWidthsChange?: Maybe<(columnWidths: ColumnWidths) => void>`
@@ -286,10 +301,10 @@ function MyGrid() {
286301
}
287302
```
288303

289-
Grid can be sorted on multiple columns using `ctrl (command) + click`. To disable multiple column sorting, change the `onSortColumnsChange` function to
304+
More than one column can be sorted via `ctrl (command) + click`. To disable multiple column sorting, change the `onSortColumnsChange` function to
290305

291306
```tsx
292-
onSortColumnsChange(sortColumns: SortColumn[]) {
307+
function onSortColumnsChange(sortColumns: SortColumn[]) {
293308
setSortColumns(sortColumns.slice(-1));
294309
}
295310
```
@@ -317,51 +332,54 @@ function MyGrid() {
317332

318333
###### `onFill?: Maybe<(event: FillEvent<R>) => R>`
319334

320-
###### `onCellClick?: Maybe<(args: CellClickArgs<R, SR>, event: CellMouseEvent) => void>`
335+
###### `onCellMouseDown: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>`
321336

322-
Callback triggered when a cell is clicked. The default behavior is to select the cell. Call `preventGridDefault` to prevent the default behavior
337+
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.
323338

324339
```tsx
325-
function onCellClick(args: CellClickArgs<R, SR>, event: CellMouseEvent) {
340+
function onCellMouseDown(args: CellMouseDownArgs<R, SR>, event: CellMouseEvent) {
326341
if (args.column.key === 'id') {
327342
event.preventGridDefault();
328343
}
329344
}
330345

331-
<DataGrid rows={rows} columns={columns} onCellClick={onCellClick} />;
346+
<DataGrid rows={rows} columns={columns} onCellMouseDown={onCellMouseDown} />;
332347
```
333348

334-
This event can be used to open cell editor on single click
349+
See [`CellMouseArgs`](#cellmouseargs) and [`CellMouseEvent`](#cellmouseevent)
350+
351+
###### `onCellClick?: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>`
352+
353+
Callback triggered when a cell is clicked.
335354

336355
```tsx
337-
function onCellClick(args: CellClickArgs<R, SR>, event: CellMouseEvent) {
356+
function onCellClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
338357
if (args.column.key === 'id') {
339358
event.preventGridDefault();
340-
args.selectCell(true);
341359
}
342360
}
343-
```
344-
345-
Arguments:
346361

347-
`args: CellClickArgs<R, SR>`
362+
<DataGrid rows={rows} columns={columns} onCellClick={onCellClick} />;
363+
```
348364

349-
- `args.rowIdx`: `number` - row index of the currently selected cell
350-
- `args.row`: `R` - row object of the currently selected cell
351-
- `args.column`: `CalculatedColumn<TRow, TSummaryRow>` - column object of the currently selected cell
352-
- `args.selectCell`: `(enableEditor?: boolean) => void` - function to manually select the cell and optionally pass `true` to start editing
365+
This event can be used to open cell editor on single click
353366

354-
`event` extends `React.MouseEvent<HTMLDivElement>`
367+
```tsx
368+
function onCellClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
369+
if (args.column.key === 'id') {
370+
args.selectCell(true);
371+
}
372+
}
373+
```
355374

356-
- `event.preventGridDefault:`: `() => void`
357-
- `event.isGridDefaultPrevented`: `boolean`
375+
See [`CellMouseArgs`](#cellmouseargs) and [`CellMouseEvent`](#cellmouseevent)
358376

359-
###### `onCellDoubleClick?: Maybe<(args: CellClickArgs<R, SR>, event: CellMouseEvent) => void>`
377+
###### `onCellDoubleClick?: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>`
360378

361-
Callback triggered when a cell is double-clicked. The default behavior is to open the editor if the cell is editable. Call `preventGridDefault` to prevent the default behavior
379+
Callback triggered when a cell is double-clicked. The default behavior is to open the editor if the cell is editable. Call `preventGridDefault` to prevent the default behavior.
362380

363381
```tsx
364-
function onCellDoubleClick(args: CellClickArgs<R, SR>, event: CellMouseEvent) {
382+
function onCellDoubleClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
365383
if (args.column.key === 'id') {
366384
event.preventGridDefault();
367385
}
@@ -370,20 +388,25 @@ function onCellDoubleClick(args: CellClickArgs<R, SR>, event: CellMouseEvent) {
370388
<DataGrid rows={rows} columns={columns} onCellDoubleClick={onCellDoubleClick} />;
371389
```
372390

373-
###### `onCellContextMenu?: Maybe<(args: CellClickArgs<R, SR>, event: CellMouseEvent) => void>`
391+
See [`CellMouseArgs`](#cellmouseargs) and [`CellMouseEvent`](#cellmouseevent)
392+
393+
###### `onCellContextMenu?: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>`
374394

375-
Callback triggered when a cell is right-clicked. The default behavior is to select the cell. Call `preventGridDefault` to prevent the default behavior
395+
Callback triggered when a cell is right-clicked.
376396

377397
```tsx
378-
function onCellContextMenu(args: CellClickArgs<R, SR>, event: CellMouseEvent) {
398+
function onCellContextMenu(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
379399
if (args.column.key === 'id') {
380-
event.preventGridDefault();
400+
event.preventDefault();
401+
// open custom context menu
381402
}
382403
}
383404

384405
<DataGrid rows={rows} columns={columns} onCellContextMenu={onCellContextMenu} />;
385406
```
386407

408+
See [`CellMouseArgs`](#cellmouseargs) and [`CellMouseEvent`](#cellmouseevent)
409+
387410
###### `onCellKeyDown?: Maybe<(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) => void>`
388411

389412
A function called when keydown event is triggered on a cell. This event can be used to customize cell navigation and editing behavior.
@@ -412,11 +435,11 @@ function onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
412435

413436
Check [more examples](website/routes/CellNavigation.tsx)
414437

415-
###### `onCellCopy?: Maybe<(args: CellCopyEvent<NoInfer<R>, NoInfer<SR>>, event: CellClipboardEvent) => void>`
438+
###### `onCellCopy?: Maybe<(args: CellCopyArgs<NoInfer<R>, NoInfer<SR>>, event: CellClipboardEvent) => void>`
416439

417440
Callback triggered when a cell's content is copied.
418441

419-
###### `onCellPaste?: Maybe<(args: CellPasteEvent<NoInfer<R>, NoInfer<SR>>, event: CellClipboardEvent) => void>`
442+
###### `onCellPaste?: Maybe<(args: CellPasteArgs<NoInfer<R>, NoInfer<SR>>, event: CellClipboardEvent) => void>`
420443

421444
Callback triggered when content is pasted into a cell.
422445

@@ -480,7 +503,7 @@ function MyGrid() {
480503
}
481504
```
482505

483-
:warning: To prevent all rows from being unmounted on re-renders, make sure to pass a static or memoized component to `renderRow`.
506+
:warning: To prevent all rows from being unmounted on re-renders, make sure to pass a static or memoized render function to `renderRow`.
484507

485508
###### `rowClass?: Maybe<(row: R, rowIdx: number) => Maybe<string>>`
486509

@@ -508,7 +531,7 @@ This property sets the text direction of the grid, it defaults to `'ltr'` (left-
508531

509532
- Columns flow from right to left
510533
- Frozen columns are pinned on the right
511-
- Column resize handle is shown on the left edge of the column
534+
- Column resize cursor is shown on the left edge of the column
512535
- Scrollbar is moved to the left
513536

514537
###### `className?: string | undefined`
@@ -535,16 +558,17 @@ If the grid has a caption or description, `aria-describedby` can be set on the g
535558

536559
###### `'data-testid'?: Maybe<string>`
537560

538-
This prop can be used to add a testid for testing. We recommend using `role` and `name` to find the grid element
561+
This prop can be used to add a testid for testing. We recommend querying the grid by by its `role` and `name`.
539562

540563
```tsx
541564
function MyGrid() {
542565
return <DataGrid aria-label="my-grid" columns={columns} rows={rows} />;
543566
}
544567

545-
function MyGridTest() {
568+
test('grid', () => {
569+
render(<MyGrid />);
546570
const grid = screen.getByRole('grid', { name: 'my-grid' });
547-
}
571+
});
548572
```
549573

550574
#### `<TreeDataGrid />`
@@ -654,10 +678,10 @@ A unique key to distinguish each column
654678
Width can be any valid css grid column value. If not specified, it will be determined automatically based on grid width and specified widths of other columns.
655679

656680
```tsx
657-
width: 80; // pixels
658-
width: '25%';
659-
width: 'max-content';
660-
width: 'minmax(100px, max-content)';
681+
width: 80, // pixels
682+
width: '25%',
683+
width: 'max-content',
684+
width: 'minmax(100px, max-content)',
661685
```
662686

663687
`max-content` can be used to expand the column to show all the content. Note that the grid is only able to calculate column width for visible rows.
@@ -702,7 +726,7 @@ Render function to render the content of edit cells. When set, the column is aut
702726

703727
##### `editable?: Maybe<boolean | ((row: TRow) => boolean)>`
704728

705-
Enables cell editing. If set and no editor property specified, then a textinput will be used as the cell editor.
729+
Enables cell editing. If set and no editor property specified, then a text input will be used as the cell editor.
706730

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

@@ -758,6 +782,32 @@ Commit changes when clicking outside the cell.
758782

759783
Close the editor when the row changes externally.
760784

785+
#### `CellMouseArgs`
786+
787+
##### `rowIdx: number`
788+
789+
Row index of the currently selected cell
790+
791+
##### `row: TRow`
792+
793+
row object of the currently selected cell
794+
795+
##### `column: CalculatedColumn<TRow, TSummaryRow>`
796+
797+
column object of the currently selected cell
798+
799+
##### `selectCell: (enableEditor?: boolean) => void`
800+
801+
function to manually select the cell and optionally pass `true` to start editing
802+
803+
#### `CellMouseEvent`
804+
805+
Extends `React.MouseEvent<HTMLDivElement>`
806+
807+
##### `event.preventGridDefault: () => void`
808+
809+
##### `event.isGridDefaultPrevented: boolean`
810+
761811
#### `DataGridHandle`
762812

763813
#### `RenderEditCellProps`

0 commit comments

Comments
 (0)