From adcab06eb2e6efc736e9e356d25025430eb98c36 Mon Sep 17 00:00:00 2001 From: shams mosowi Date: Thu, 12 Sep 2019 11:40:27 +1000 Subject: [PATCH] selectable cell --- ROADMAP.md | 4 ++-- src/components/Table.tsx | 35 +++++++++++++++++++++++++++++------ src/hooks/useCell.ts | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 src/hooks/useCell.ts diff --git a/ROADMAP.md b/ROADMAP.md index c4fe017b..2494da9c 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -26,14 +26,14 @@ ### additional fields: - single select(string) -- [https://material-ui.com/components/chips/#chip-array]Multiple select(array of strings) +- [https://material-ui.com/components/chips/#chip-array] Multiple select(array of strings) - date(Firebase timestamp) - time(Firebase timestamp) - file(firebase storage url string) - image(firebase storage url string) - single select reference(DocRefrence) - mulit select reference(DocRefrence) -- [https://material-ui.com/components/rating/]rating(number) +- [https://material-ui.com/components/rating/] rating(number) ### Functionality: diff --git a/src/components/Table.tsx b/src/components/Table.tsx index c92435c0..9558676a 100644 --- a/src/components/Table.tsx +++ b/src/components/Table.tsx @@ -17,11 +17,12 @@ import { } from "react-virtualized"; import Button from "@material-ui/core/Button"; -// import { TextField } from "@material-ui/core"; +import TextField from "@material-ui/core/TextField"; import { FieldType, getFieldIcon } from "../Fields"; import ColumnDrawer from "./ColumnDrawer"; import IconButton from "@material-ui/core/IconButton"; import DeleteIcon from "@material-ui/icons/Delete"; +import useCell, { Cell } from "../hooks/useCell"; const styles = (theme: Theme) => createStyles({ flexContainer: { @@ -59,6 +60,8 @@ interface Row { interface MuiVirtualizedTableProps extends WithStyles { columns: ColumnData[]; + focusedCell: Cell | null; + cellActions: any; headerHeight?: number; onRowClick?: () => void; rowCount: number; @@ -91,7 +94,14 @@ class MuiVirtualizedTable extends React.PureComponent< rowData, rowIndex }) => { - const { columns, classes, rowHeight, onRowClick } = this.props; + const { + columns, + classes, + rowHeight, + onRowClick, + cellActions, + focusedCell + } = this.props; const fieldType = columnData.fieldType; if (fieldType === "DELETE") return ( @@ -104,7 +114,7 @@ class MuiVirtualizedTable extends React.PureComponent< ); - + console.log(focusedCell); return ( { - console.log(rowIndex, rowData.id, columnData.fieldName); + cellActions.setCell({ + rowIndex, + docId: rowData.id, + fieldName: columnData.fieldName + }); }} style={{ height: rowHeight }} align={ @@ -122,7 +136,13 @@ class MuiVirtualizedTable extends React.PureComponent< : "left" } > - {cellData} {} + {focusedCell && + focusedCell.fieldName === columnData.fieldName && + focusedCell.rowIndex === rowIndex ? ( + + ) : ( + cellData + )} ); }; @@ -207,11 +227,14 @@ const VirtualizedTable = withStyles(styles)(MuiVirtualizedTable); export default function Table(props: any) { const { columns, rows, addColumn, deleteRow } = props; - const [focus, setFocus] = useState(false); + const [cell, cellActions] = useCell({}); + console.log("cell", cell); if (columns) return ( rows[index]} columns={[ diff --git a/src/hooks/useCell.ts b/src/hooks/useCell.ts new file mode 100644 index 00000000..48f449f2 --- /dev/null +++ b/src/hooks/useCell.ts @@ -0,0 +1,32 @@ +import { db } from "../firebase"; +import { useEffect, useReducer } from "react"; +import equals from "ramda/es/equals"; + +export type Cell = { + fieldName: string; + rowIndex: number; + docId: string; +}; + +const cellReducer = (prevState: any, newProps: any) => { + return { ...prevState, ...newProps }; +}; +const cellIntialState = { + prevCell: null, + cell: null +}; + +const useCell = (intialOverrides: any) => { + const [cellState, cellDispatch] = useReducer(cellReducer, { + ...cellIntialState, + ...intialOverrides + }); + + const setCell = (cell: Cell) => { + cellDispatch({ prevCell: cellState.cell, cell }); + }; + const actions = { setCell }; + return [cellState.cell, actions]; +}; + +export default useCell;