This commit is contained in:
shams mosowi
2019-10-03 08:43:38 +10:00
6 changed files with 77 additions and 40 deletions

View File

@@ -62,7 +62,7 @@
### Functionality:
- Sort rows
- reorder columns
- reorder columns
- Locked columns
- Table view only mode
- SubCollection tables

View File

@@ -28,7 +28,8 @@ 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";
const { DraggableContainer } = DraggableHeader;
const deleteAlgoliaRecord = functions.httpsCallable(
CLOUD_FUNCTIONS.deleteAlgoliaRecord
);
@@ -161,10 +162,13 @@ function Table(props: Props) {
);
}
};
const onHeaderDrop = (dragged: any, target: any) => {
tableActions.column.reorder(dragged, target);
};
if (tableState.columns) {
let columns = tableState.columns.map((column: any) => ({
width: 220,
draggable: true,
editable: editable(column.type),
resizable: true,
headerRenderer: headerRenderer,
@@ -200,7 +204,6 @@ function Table(props: Props) {
),
});
const rows = tableState.rows;
return (
<>
<div className={classes.tableHeader}>
@@ -221,39 +224,41 @@ function Table(props: Props) {
</Button>
</div>
</div>
<ReactDataGrid
rowHeight={40}
columns={columns}
rowGetter={i => rows[i]}
rowsCount={rows.length}
onGridRowsUpdated={onGridRowsUpdated}
enableCellSelect={true}
minHeight={size.height ? size.height - 102 : 100}
onCellSelected={onCellSelected}
onColumnResize={(idx, width) =>
tableActions.column.resize(idx, width)
}
emptyRowsView={() => {
return (
<div
style={{
textAlign: "center",
backgroundColor: "#ddd",
padding: "100px",
}}
>
<h3>no data to show</h3>
<Button
onClick={() => {
tableActions.row.add();
<DraggableContainer onHeaderDrop={onHeaderDrop}>
<ReactDataGrid
rowHeight={40}
columns={columns}
rowGetter={i => rows[i]}
rowsCount={rows.length}
onGridRowsUpdated={onGridRowsUpdated}
enableCellSelect={true}
minHeight={size.height ? size.height - 102 : 100}
onCellSelected={onCellSelected}
onColumnResize={(idx, width) =>
tableActions.column.resize(idx, width)
}
emptyRowsView={() => {
return (
<div
style={{
textAlign: "center",
backgroundColor: "#ddd",
padding: "100px",
}}
>
Add Row
</Button>
</div>
);
}}
/>
<h3>no data to show</h3>
<Button
onClick={() => {
tableActions.row.add();
}}
>
Add Row
</Button>
</div>
);
}}
/>
</DraggableContainer>
<ColumnEditor
handleClose={handleCloseHeader}

View File

@@ -8,6 +8,7 @@ export type FiretableActions = {
rename: Function;
remove: Function;
update: Function;
reorder: Function;
};
row: { add: any; delete: Function };
set: Function;
@@ -47,6 +48,7 @@ const useFiretable = (collectionName: string) => {
rename: configActions.rename,
update: configActions.update,
remove: configActions.remove,
reorder: configActions.reorder,
},
row: {
add: tableActions.addRow,

View File

@@ -2,6 +2,8 @@ import { useEffect } from "react";
import useDoc, { DocActions } from "../useDoc";
import { FieldType } from "../../components/Fields";
import _camelCase from "lodash/camelCase";
import _findIndex from "lodash/findIndex";
import { arrayMover } from "../../util/fns";
const useTableConfig = (tablePath: string) => {
const [tableConfigState, documentDispatch] = useDoc({
@@ -44,12 +46,24 @@ const useTableConfig = (tablePath: string) => {
columns.splice(index, 1);
documentDispatch({ action: DocActions.update, data: { columns } });
};
const reorder = (draggedColumnKey: string, droppedColumnKey: string) => {
const { columns } = tableConfigState;
const draggedColumnIndex = _findIndex(columns, ["key", draggedColumnKey]);
const droppedColumnIndex = _findIndex(columns, ["key", droppedColumnKey]);
const reorderedColumns = [...columns];
arrayMover(reorderedColumns, draggedColumnIndex, droppedColumnIndex);
documentDispatch({
action: DocActions.update,
data: { columns: reorderedColumns },
});
};
const actions = {
update,
add,
resize,
setTable,
remove,
reorder,
};
return [tableConfigState, actions];
};

20
src/util/fns.ts Normal file
View File

@@ -0,0 +1,20 @@
export const arrayMover = (
arr: any[],
old_index: number,
new_index: number
) => {
while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
var k = new_index - arr.length + 1;
while (k--) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr; // for testing purposes
};

View File

@@ -41,11 +41,7 @@ export default function AuthView() {
<Card className={classes.card}>
<CardContent>
<Typography className={classes.header}>Fire Table</Typography>
<Button
onClick={handleAuth}
color="secondary"
className={classes.button}
>
<Button onClick={handleAuth} color="primary" className={classes.button}>
Authenticate With Google
</Button>
</CardContent>