From 5d5e4f2c32a3ba5f126cc978ec4254f44f36c0a9 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 10 Sep 2021 15:09:02 +0100 Subject: [PATCH 1/4] Document rowRenderer --- README.md | 31 +++++++++++++++++++++++++++++-- stories/demos/RowsReordering.tsx | 30 ++++++++++++++++-------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9c6b79ee3a..445e45bac1 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,8 @@ function App() { ###### `columns: readonly Column[]` +See [`Column`](#column) + ###### `rows: readonly R[]` ###### `summaryRows?: Maybe` @@ -137,7 +139,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 +187,12 @@ See [`EditorProps`](#editorprops) #### `` -See [`RowRendererProps`](#rowrendererprops). +See [`rowRenderer`](#rowRenderer) + +##### Props + +See [`RowRendererProps`](#rowrendererprops) + The `ref` prop is supported. #### `` 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 ( - } - /> + ); } From 17d830c4f522eed94b797dca0de9d427130b5b9d Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 10 Sep 2021 17:11:41 +0100 Subject: [PATCH 2/4] Document Column - summaryRowHeight --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++- src/DataGrid.tsx | 15 ++++++++++--- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 445e45bac1..7f6b991394 100644 --- a/README.md +++ b/README.md @@ -87,22 +87,78 @@ function App() { ###### `columns: readonly Column[]` -See [`Column`](#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 be work. + +```tsx +import DataGrid from 'react-data-grid'; + +interface Row { + id: number; + name: string; +} + +function rowKeyGetter(row: Row) { + return row.id; +} + +function MyGrid() { + return ; +} +``` + +:+1: 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>` 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; /** From 505d04e1c710eef6c01c58613a8de3cd666b1a9e Mon Sep 17 00:00:00 2001 From: Nicolas Stepien <567105+nstepien@users.noreply.github.com> Date: Fri, 10 Sep 2021 17:14:07 +0100 Subject: [PATCH 3/4] Use bulb instead --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7f6b991394..f580a42916 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ function MyGrid() { } ``` -:+1: 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. +: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>` From 893ffa9cdc1e65a832f121e0af1b9b36d293802c Mon Sep 17 00:00:00 2001 From: Nicolas Stepien <567105+nstepien@users.noreply.github.com> Date: Fri, 10 Sep 2021 19:07:53 +0100 Subject: [PATCH 4/4] Update README.md Co-authored-by: Aman Mahajan --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f580a42916..869b0c3919 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ An optional array of summary rows, usually used to display total values for exam ###### `rowKeyGetter?: Maybe<(row: R) => K>` -A function returning a unique key/identifier per row. `rowKeyGetter` is required for row selection to be work. +A function returning a unique key/identifier per row. `rowKeyGetter` is required for row selection to work. ```tsx import DataGrid from 'react-data-grid';