Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
87 changes: 85 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,78 @@ function App() {

###### `columns: readonly Column<R, SR>[]`

See [`Column`](#column).

An array describing the grid's columns.

:warning: Passing a new `columns` array will trigger a re-render for the whole grid, avoid changing it as much as possible for optimal performance.

###### `rows: readonly R[]`

An array of rows, the rows data can be of any type.

###### `summaryRows?: Maybe<readonly SR[]>`

An optional array of summary rows, usually used to display total values for example.

###### `rowKeyGetter?: Maybe<(row: R) => K>`

A function returning a unique key/identifier per row. `rowKeyGetter` is required for row selection to work.

```tsx
import DataGrid from 'react-data-grid';

interface Row {
id: number;
name: string;
}

function rowKeyGetter(row: Row) {
return row.id;
}

function MyGrid() {
return <DataGrid columns={columns} rows={rows} rowKeyGetter={rowKeyGetter} />;
}
```

:bulb: While optional, setting this prop is recommended for optimal performance as the returned value is used to set the `key` prop on the row elements.

###### `onRowsChange?: Maybe<(rows: R[], data: RowsChangeData<R, SR>) => void>`

A function receiving row updates.
The first parameter is a new rows array with both the updated rows and the other untouched rows.
The second parameter is an object with an `indexes` array highlighting which rows have changed by their index, and the `column` where the change happened.

```tsx
import { useState } from 'react';
import DataGrid from 'react-data-grid';

function MyGrid() {
const [rows, setRows] = useState(initialRows);

return <DataGrid columns={columns} rows={rows} onRowsChange={setRows} />;
}
```

###### `rowHeight?: Maybe<number | ((args: RowHeightArgs<R>) => number)>`

**Default:** `35` pixels

Either a number defining the height of row in pixels, or a function returning dynamic row heights.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

May be we can rename variable row height example to dynamic row height

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Both are good


###### `headerRowHeight?: Maybe<number>`

**Default:** `35` pixels

A number defining the height of the header row.

###### `summaryRowHeight?: Maybe<number>`

**Default:** `35` pixels

A number defining the height of summary rows.

###### `selectedRows?: Maybe<ReadonlySet<K>>`

###### `onSelectedRowsChange?: Maybe<(selectedRows: Set<K>) => void>`
Expand Down Expand Up @@ -137,7 +195,27 @@ function App() {

###### `enableVirtualization?: Maybe<boolean>`

###### `rowRenderer?: Maybe<React.ComponentType<RowRendererProps<R, SR>>>`
###### <span name="rowRenderer">`rowRenderer?: Maybe<React.ComponentType<RowRendererProps<R, SR>>>`</span>

The default `<Row />` component can be wrapped via the `rowRenderer` prop to add context providers or tweak props for example.

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

function MyRowRenderer(props: RowRendererProps<Row>) {
return (
<MyContext.Provider value={123}>
<Row {...props} />
</MyContext.Provider>
);
}

function MyGrid() {
return <DataGrid columns={columns} rows={rows} rowRenderer={MyRowRenderer} />;
}
```

:warning: To prevent all rows from being unmounted on re-renders, make sure to pass a static or memoized component to `rowRenderer`.

###### `noRowsFallback?: React.ReactNode`

Expand Down Expand Up @@ -165,7 +243,12 @@ See [`EditorProps`](#editorprops)

#### `<Row />`

See [`RowRendererProps`](#rowrendererprops).
See [`rowRenderer`](#rowRenderer)

##### Props

See [`RowRendererProps`](#rowrendererprops)

The `ref` prop is supported.

#### `<SortableHeaderCell />`
Expand Down
15 changes: 12 additions & 3 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,20 @@ export interface DataGridProps<R, SR = unknown, K extends Key = Key> extends Sha
/**
* Dimensions props
*/
/** The height of each row in pixels */
/**
* The height of each row in pixels
* @default 35
*/
rowHeight?: Maybe<number | ((args: RowHeightArgs<R>) => number)>;
/** The height of the header row in pixels */
/**
* The height of the header row in pixels
* @default 35
*/
headerRowHeight?: Maybe<number>;
/** The height of each summary row in pixels */
/**
* The height of each summary row in pixels
* @default 35
*/
summaryRowHeight?: Maybe<number>;

/**
Expand Down
30 changes: 16 additions & 14 deletions stories/demos/RowsReordering.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useState } from 'react';
import { useCallback, useState } from 'react';
import { DndProvider } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';

import { DraggableRowRenderer } from './components/RowRenderers';
import DataGrid from '../../src';
import type { Column } from '../../src';
import DataGrid, { TextEditor } from '../../src';
import type { Column, RowRendererProps } from '../../src';

interface Row {
id: number;
Expand Down Expand Up @@ -37,7 +37,8 @@ const columns: readonly Column<Row>[] = [
},
{
key: 'task',
name: 'Title'
name: 'Title',
editor: TextEditor
},
{
key: 'priority',
Expand All @@ -56,20 +57,21 @@ const columns: readonly Column<Row>[] = [
export function RowsReordering() {
const [rows, setRows] = useState(createRows);

function onRowReorder(fromIndex: number, toIndex: number) {
const newRows = [...rows];
newRows.splice(toIndex, 0, newRows.splice(fromIndex, 1)[0]);
const RowRenderer = useCallback((props: RowRendererProps<Row>) => {
function onRowReorder(fromIndex: number, toIndex: number) {
setRows((rows) => {
const newRows = [...rows];
newRows.splice(toIndex, 0, newRows.splice(fromIndex, 1)[0]);
return newRows;
});
}

setRows(newRows);
}
return <DraggableRowRenderer {...props} onRowReorder={onRowReorder} />;
}, []);

return (
<DndProvider backend={HTML5Backend}>
<DataGrid
columns={columns}
rows={rows}
rowRenderer={(p) => <DraggableRowRenderer {...p} onRowReorder={onRowReorder} />}
/>
<DataGrid columns={columns} rows={rows} onRowsChange={setRows} rowRenderer={RowRenderer} />
</DndProvider>
);
}
Expand Down