From ed32938b27677c393f1a449b62104e1d1501a704 Mon Sep 17 00:00:00 2001 From: shams mosowi Date: Thu, 12 Sep 2019 12:48:48 +1000 Subject: [PATCH] editing cells --- ROADMAP.md | 5 +++-- src/components/Table.tsx | 21 +++++++++++++-------- src/hooks/useCell.ts | 17 +++++++++++++++-- src/hooks/useTable.ts | 14 +++++++++++++- src/views/TableView.tsx | 1 + 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 2494da9c..733dcf63 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -5,7 +5,7 @@ ### Initial fields: - checkbox(boolean) -- simple text(string) +- simple text(string) ✅ - email(string) - phone(string) - url(string) @@ -17,7 +17,7 @@ - Create Tables (Primary collections) ✅ - Create columns (fields) ✅ - Create rows(documents) -- Edit cells +- Edit cells ✅ - Authenicate ✅ - Delete rows ✅ @@ -44,6 +44,7 @@ - Edit tables - Hide tables - Fixed column +- keyboard Navigation: ability to use tab and arrow keys to move focus between cells ## V1 diff --git a/src/components/Table.tsx b/src/components/Table.tsx index 9558676a..f80d5b6b 100644 --- a/src/components/Table.tsx +++ b/src/components/Table.tsx @@ -114,7 +114,6 @@ class MuiVirtualizedTable extends React.PureComponent< ); - console.log(focusedCell); return ( + { + cellActions.updateValue(e.target.value); + }} + /> ) : ( cellData )} @@ -226,17 +231,17 @@ class MuiVirtualizedTable extends React.PureComponent< const VirtualizedTable = withStyles(styles)(MuiVirtualizedTable); export default function Table(props: any) { - const { columns, rows, addColumn, deleteRow } = props; - const [cell, cellActions] = useCell({}); - console.log("cell", cell); + const { columns, rows, addColumn, deleteRow, updateCell } = props; + const tableRows = [...rows]; + const [cell, cellActions] = useCell({ updateCell }); if (columns) return ( rows[index]} + rowCount={tableRows.length} + rowGetter={({ index }) => tableRows[index]} columns={[ ...columns.map( (column: { diff --git a/src/hooks/useCell.ts b/src/hooks/useCell.ts index 48f449f2..dd8cf06b 100644 --- a/src/hooks/useCell.ts +++ b/src/hooks/useCell.ts @@ -6,6 +6,7 @@ export type Cell = { fieldName: string; rowIndex: number; docId: string; + value: any; }; const cellReducer = (prevState: any, newProps: any) => { @@ -21,11 +22,23 @@ const useCell = (intialOverrides: any) => { ...cellIntialState, ...intialOverrides }); - + useEffect(() => { + const { prevCell, value, updateCell, updatedValue } = cellState; + // check for change + if (updatedValue && prevCell && prevCell.value !== updatedValue) { + updateCell({ ...prevCell, value: updatedValue }); + cellDispatch({ updatedValue: null }); + } + }, [cellState.cell]); const setCell = (cell: Cell) => { cellDispatch({ prevCell: cellState.cell, cell }); }; - const actions = { setCell }; + const updateValue = (value: any) => { + cellDispatch({ updatedValue: value }); + }; + + const actions = { setCell, updateValue }; + return [cellState.cell, actions]; }; diff --git a/src/hooks/useTable.ts b/src/hooks/useTable.ts index 73d068ce..baf26ab1 100644 --- a/src/hooks/useTable.ts +++ b/src/hooks/useTable.ts @@ -1,6 +1,7 @@ import { db } from "../firebase"; import { useEffect, useReducer } from "react"; import equals from "ramda/es/equals"; +import { Cell } from "./useCell"; const CAP = 500; const tableReducer = (prevState: any, newProps: any) => { @@ -131,8 +132,19 @@ const useTable = (intialOverrides: any) => { const setTable = (tableCollection: string) => { tableDispatch({ path: tableCollection }); }; + const updateCell = (cell: Cell) => { + console.log("updateCell:", cell); + // TODO: update row locally + // tableState.rows[cell.rowIndex][cell.fieldName] = cell.value; + // tableDispatch({ rows: tableState.rows }); - const tableActions = { deleteRow, setTable }; + // update document + db.collection(tableState.path) + .doc(cell.docId) + .update({ [cell.fieldName]: cell.value }); + }; + const addRow = (cell: Cell) => {}; + const tableActions = { deleteRow, setTable, updateCell, addRow }; return [tableState, tableActions]; }; diff --git a/src/views/TableView.tsx b/src/views/TableView.tsx index bdc09062..9f7b722b 100644 --- a/src/views/TableView.tsx +++ b/src/views/TableView.tsx @@ -28,6 +28,7 @@ export default function AuthView() { rows={table.rows} addColumn={configActions.addColumn} deleteRow={tableActions.deleteRow} + updateCell={tableActions.updateCell} /> );