From a6f82d7a6f4353c43397262ecdfe608f412cef35 Mon Sep 17 00:00:00 2001 From: shams mosowi Date: Thu, 12 Sep 2019 13:16:38 +1000 Subject: [PATCH] basic creating row functionality --- src/components/Table.tsx | 89 ++++++++++++++++++++++++---------------- src/hooks/useTable.ts | 4 +- src/views/TableView.tsx | 3 +- 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/components/Table.tsx b/src/components/Table.tsx index f80d5b6b..fb8a8e6e 100644 --- a/src/components/Table.tsx +++ b/src/components/Table.tsx @@ -22,6 +22,7 @@ import { FieldType, getFieldIcon } from "../Fields"; import ColumnDrawer from "./ColumnDrawer"; import IconButton from "@material-ui/core/IconButton"; import DeleteIcon from "@material-ui/icons/Delete"; +import AddIcon from "@material-ui/icons/AddCircle"; import useCell, { Cell } from "../hooks/useCell"; const styles = (theme: Theme) => createStyles({ @@ -103,7 +104,7 @@ class MuiVirtualizedTable extends React.PureComponent< focusedCell } = this.props; const fieldType = columnData.fieldType; - if (fieldType === "DELETE") + if (fieldType === "DELETE" && rowData.id !== "new") return ( ); + else if (fieldType === "DELETE" && rowData.id === "new") { + return ( + { + columnData.actions.deleteRow(rowIndex, rowData.id); + }} + > + + + ); + } return ( {({ height, width }) => ( - - {[ - ...columns.map(({ dataKey, ...other }, index) => { - return ( - - this.headerRenderer({ - ...headerProps, - columnIndex: index - }) - } - className={classes.flexContainer} - cellRenderer={this.cellRenderer} - dataKey={dataKey} - {...other} - /> - ); - }) - ]} - + <> + + {[ + ...columns.map(({ dataKey, ...other }, index) => { + return ( + + this.headerRenderer({ + ...headerProps, + columnIndex: index + }) + } + className={classes.flexContainer} + cellRenderer={this.cellRenderer} + dataKey={dataKey} + {...other} + /> + ); + }) + ]} + + )} ); @@ -231,17 +246,17 @@ class MuiVirtualizedTable extends React.PureComponent< const VirtualizedTable = withStyles(styles)(MuiVirtualizedTable); export default function Table(props: any) { - const { columns, rows, addColumn, deleteRow, updateCell } = props; - const tableRows = [...rows]; - const [cell, cellActions] = useCell({ updateCell }); + const { columns, rows, addColumn, tableActions } = props; + + const [cell, cellActions] = useCell({ updateCell: tableActions.updateCell }); if (columns) return ( tableRows[index]} + rowCount={rows.length} + rowGetter={({ index }) => rows[index]} columns={[ ...columns.map( (column: { @@ -265,11 +280,15 @@ export default function Table(props: any) { dataKey: "add", columnData: { fieldType: "DELETE", - actions: { addColumn, deleteRow } + actions: { + addColumn: addColumn, + deleteRow: tableActions.deleteRow + } } } ]} /> + ); else return <>insert loading Skeleton here; diff --git a/src/hooks/useTable.ts b/src/hooks/useTable.ts index baf26ab1..8cc50462 100644 --- a/src/hooks/useTable.ts +++ b/src/hooks/useTable.ts @@ -143,7 +143,9 @@ const useTable = (intialOverrides: any) => { .doc(cell.docId) .update({ [cell.fieldName]: cell.value }); }; - const addRow = (cell: Cell) => {}; + const addRow = () => { + db.collection(tableState.path).add({}); + }; const tableActions = { deleteRow, setTable, updateCell, addRow }; return [tableState, tableActions]; }; diff --git a/src/views/TableView.tsx b/src/views/TableView.tsx index 9f7b722b..e1b33f0e 100644 --- a/src/views/TableView.tsx +++ b/src/views/TableView.tsx @@ -27,8 +27,7 @@ export default function AuthView() { columns={tableConfig.columns} rows={table.rows} addColumn={configActions.addColumn} - deleteRow={tableActions.deleteRow} - updateCell={tableActions.updateCell} + tableActions={tableActions} /> );