diff --git a/README.md b/README.md index 9c6b79ee3a..869b0c3919 100644 --- a/README.md +++ b/README.md @@ -87,20 +87,78 @@ function App() { ###### `columns: readonly Column[]` +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` +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 ; +} +``` + +: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) => 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 ; +} +``` + ###### `rowHeight?: Maybe) => number)>` +**Default:** `35` pixels + +Either a number defining the height of row in pixels, or a function returning dynamic row heights. + ###### `headerRowHeight?: Maybe` +**Default:** `35` pixels + +A number defining the height of the header row. + ###### `summaryRowHeight?: Maybe` +**Default:** `35` pixels + +A number defining the height of summary rows. + ###### `selectedRows?: Maybe>` ###### `onSelectedRowsChange?: Maybe<(selectedRows: Set) => void>` @@ -137,7 +195,27 @@ function App() { ###### `enableVirtualization?: Maybe` -###### `rowRenderer?: Maybe>>` +###### `rowRenderer?: Maybe>>` + +The default `` 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) { + return ( + + + + ); +} + +function MyGrid() { + return ; +} +``` + +: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` @@ -165,7 +243,12 @@ See [`EditorProps`](#editorprops) #### `` -See [`RowRendererProps`](#rowrendererprops). +See [`rowRenderer`](#rowRenderer) + +##### Props + +See [`RowRendererProps`](#rowrendererprops) + The `ref` prop is supported. #### `` diff --git a/src/DataGrid.tsx b/src/DataGrid.tsx index 3cdc22836e..1a07b3cbe1 100644 --- a/src/DataGrid.tsx +++ b/src/DataGrid.tsx @@ -104,11 +104,20 @@ export interface DataGridProps extends Sha /** * Dimensions props */ - /** The height of each row in pixels */ + /** + * The height of each row in pixels + * @default 35 + */ rowHeight?: Maybe) => number)>; - /** The height of the header row in pixels */ + /** + * The height of the header row in pixels + * @default 35 + */ headerRowHeight?: Maybe; - /** The height of each summary row in pixels */ + /** + * The height of each summary row in pixels + * @default 35 + */ summaryRowHeight?: Maybe; /** diff --git a/stories/demos/RowsReordering.tsx b/stories/demos/RowsReordering.tsx index 6943723f69..7d136bfcee 100644 --- a/stories/demos/RowsReordering.tsx +++ b/stories/demos/RowsReordering.tsx @@ -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; @@ -37,7 +37,8 @@ const columns: readonly Column[] = [ }, { key: 'task', - name: 'Title' + name: 'Title', + editor: TextEditor }, { key: 'priority', @@ -56,20 +57,21 @@ const columns: readonly Column[] = [ 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) => { + 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 ; + }, []); return ( - } - /> + ); }