From 44bb2d9cd90984f5b3ca85f8375681289f2a70e1 Mon Sep 17 00:00:00 2001 From: shams mosowi Date: Wed, 16 Oct 2019 10:13:52 +1100 Subject: [PATCH] refactor data-grid into seprate component to memoize --- src/components/Fields/Color.tsx | 1 - src/components/Table/EmptyTable.tsx | 2 +- src/components/Table/Grid.tsx | 69 +++++++++++++++++++++++++++++ src/components/Table/index.tsx | 68 +++++++++------------------- 4 files changed, 92 insertions(+), 48 deletions(-) create mode 100644 src/components/Table/Grid.tsx diff --git a/src/components/Fields/Color.tsx b/src/components/Fields/Color.tsx index 0fdfb3a3..fc0b8ca5 100644 --- a/src/components/Fields/Color.tsx +++ b/src/components/Fields/Color.tsx @@ -74,7 +74,6 @@ const Color = (props: Props) => { setHex(props.hex); }} onChangeComplete={props => { - console.log(props); onSubmit({ hex: props.hex, hsl: props.hsl, rgb: props.rgb }); setAnchorEl(null); }} diff --git a/src/components/Table/EmptyTable.tsx b/src/components/Table/EmptyTable.tsx index adf7cce0..846ae1b3 100644 --- a/src/components/Table/EmptyTable.tsx +++ b/src/components/Table/EmptyTable.tsx @@ -3,7 +3,7 @@ import Button from "@material-ui/core/Button"; const EmptyTable = (props: any) => { const { isLoading, tableHeight, addRow } = props; - if (isLoading) return

loading row

; + if (isLoading) return

Fetching rows

; else return (
import("react-data-grid")); +const { DraggableContainer } = DraggableHeader; +const Grid = (props: any) => { + const { + onHeaderDrop, + rowHeight, + columns, + RowRenderer, + handleRowGetter, + + tableHeight, + onGridRowsUpdated, + rows, + resizeColumn, + loadingRows, + addRow, + setSelectedCell, + } = props; + return ( + Loading table...
}> + + { + const row = rows[coordinates.rowIdx]; + const column = columns[coordinates.idx]; + // if (editable(column.type)) { + // only editable fields are stored selectedCell, temporary fix for custom fields + setSelectedCell({ row, column }); + //} + }} + onColumnResize={(idx: number, width: number) => + //tableActions.column.resize(idx, width) + resizeColumn(idx, width) + } + emptyRowsView={() => ( + + )} + /> + + + ); +}; + +export default React.memo(Grid, (prevProps, nextProps) => { + return ( + isEqual(prevProps.columns, nextProps.columns) || + isEqual(prevProps.rows, nextProps.rows) + // || prevProps.rowHeight === nextProps.rowHeight || + // prevProps.tableHeight === nextProps.tableHeight + ); +}); diff --git a/src/components/Table/index.tsx b/src/components/Table/index.tsx index 1b4d7b44..26cb5e56 100644 --- a/src/components/Table/index.tsx +++ b/src/components/Table/index.tsx @@ -19,20 +19,16 @@ import Typography from "@material-ui/core/Typography"; import AddIcon from "@material-ui/icons/AddCircle"; import SettingsIcon from "@material-ui/icons/Settings"; import useWindowSize from "../../hooks/useWindowSize"; -import { DraggableHeader } from "react-data-grid-addons"; import Confirmation from "components/Confirmation"; import DeleteIcon from "@material-ui/icons/Delete"; import DuplicateIcon from "@material-ui/icons/FileCopy"; import useStyles from "./useStyle"; -import EmptyTable from "./EmptyTable"; +import Grid from "./Grid"; const Hotkeys = lazy(() => import("./HotKeys")); -const ReactDataGrid = lazy(() => import("react-data-grid")); const TableHeader = lazy(() => import("./TableHeader")); const SearchBox = lazy(() => import("../SearchBox")); const DocSelect = lazy(() => import("../Fields/DocSelect")); const ColumnEditor = lazy(() => import("./ColumnEditor/index")); - -const { DraggableContainer } = DraggableHeader; const deleteAlgoliaRecord = functions.httpsCallable( CLOUD_FUNCTIONS.deleteAlgoliaRecord ); @@ -243,47 +239,27 @@ function Table(props: Props) { columns={columns} addRow={tableActions.row.add} /> - - Loading table...}> - {!tableState.loadingColumns ? ( - - { - const row = rows[coordinates.rowIdx]; - const column = columns[coordinates.idx]; - if (editable(column.type)) { - // only editable fields are stored selectedCell, temporary fix for custom fields - setSelectedCell({ row, column }); - } - }} - onColumnResize={(idx, width) => - tableActions.column.resize(idx, width) - } - emptyRowsView={() => ( - - )} - /> - - ) : ( -

fetching columns

- )} - +
{" "} + {!tableState.loadingColumns ? ( + + ) : ( +

fetching columns

+ )} + Loading helpers...}>