PromucFlow_constructor/app/client/src/widgets/TableWidget.tsx

125 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
2019-11-14 09:28:51 +00:00
import _ from "lodash";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
2019-09-12 08:11:25 +00:00
import { WidgetType } from "../constants/WidgetConstants";
import { ActionPayload } from "../constants/ActionConstants";
2019-11-07 11:17:53 +00:00
import { AutoResizer } from "react-base-table";
2019-10-31 10:10:57 +00:00
import "react-base-table/styles.css";
import { forIn } from "lodash";
2019-11-07 11:17:53 +00:00
import SelectableTable, {
Column,
} from "../components/designSystems/appsmith/TableComponent";
2019-10-31 10:10:57 +00:00
function constructColumns(data: object[]): Column[] {
2019-10-31 11:26:37 +00:00
const cols: Column[] = [];
const listItemWithAllProperties = {};
data.forEach(dataItem => {
2019-10-31 11:26:37 +00:00
Object.assign(listItemWithAllProperties, dataItem);
});
forIn(listItemWithAllProperties, (value, key) => {
2019-10-31 10:10:57 +00:00
cols.push({
key: key,
dataKey: key,
width: 200,
title: key,
});
});
return cols;
}
const getTableArrayData = (
tableData: string | object[] | undefined,
): object[] => {
2019-10-31 10:10:57 +00:00
try {
2019-11-14 09:28:51 +00:00
if (!tableData) return [];
2019-11-14 14:26:23 +00:00
let data = tableData;
2019-11-14 09:28:51 +00:00
if (_.isString(tableData)) {
2019-11-14 14:26:23 +00:00
data = JSON.parse(data as string);
}
if (!Array.isArray(data)) {
return [];
} else {
return data;
2019-11-06 06:35:15 +00:00
}
2019-11-14 09:28:51 +00:00
} catch (error) {
console.error({ error });
return [];
2019-10-31 10:10:57 +00:00
}
};
2019-09-12 08:11:25 +00:00
class TableWidget extends BaseWidget<TableWidgetProps, WidgetState> {
2019-09-12 08:11:25 +00:00
getPageView() {
2019-11-14 09:28:51 +00:00
const { tableData } = this.props;
const data = getTableArrayData(tableData);
2019-11-14 14:26:23 +00:00
console.log({ data });
2019-11-14 09:28:51 +00:00
const columns = constructColumns(data);
2019-10-31 10:10:57 +00:00
return (
<AutoResizer>
{({ width, height }: { width: number; height: number }) => (
2019-11-07 11:17:53 +00:00
<SelectableTable
2019-10-31 10:10:57 +00:00
width={width}
height={height}
columns={columns}
2019-11-14 09:28:51 +00:00
data={data}
2019-10-31 10:10:57 +00:00
maxHeight={height}
2019-11-07 11:17:53 +00:00
selectedRowIndex={
this.props.selectedRow && this.props.selectedRow.rowIndex
2019-11-07 11:17:53 +00:00
}
onRowClick={(rowData: object, index: number) => {
const { onRowSelected } = this.props;
this.updateSelectedRowProperty(rowData, index);
2019-11-07 11:17:53 +00:00
super.executeAction(onRowSelected);
}}
2019-10-31 10:10:57 +00:00
/>
)}
</AutoResizer>
);
2019-09-12 08:11:25 +00:00
}
componentDidUpdate(prevProps: TableWidgetProps) {
2019-11-14 12:16:11 +00:00
super.componentDidUpdate(prevProps);
2019-11-14 14:26:23 +00:00
const newData = getTableArrayData(this.props.tableData);
if (prevProps.tableData !== this.props.tableData && prevProps.selectedRow) {
this.updateSelectedRowProperty(
2019-11-14 14:26:23 +00:00
newData[prevProps.selectedRow.rowIndex],
prevProps.selectedRow.rowIndex,
);
}
}
updateSelectedRowProperty(rowData: object, index: number) {
const { widgetId } = this.props;
2019-11-14 14:26:23 +00:00
this.updateWidgetProperty(widgetId, "selectedRow", {
...rowData,
rowIndex: index,
});
}
2019-09-12 08:11:25 +00:00
getWidgetType(): WidgetType {
return "TABLE_WIDGET";
}
}
export type PaginationType = "PAGES" | "INFINITE_SCROLL";
2019-09-12 08:11:25 +00:00
export interface TableAction extends ActionPayload {
actionName: string;
}
type RowData = {
rowIndex: number;
};
type SelectedRow = object & RowData;
export interface TableWidgetProps extends WidgetProps {
nextPageKey?: string;
prevPageKey?: string;
label: string;
tableData?: string | object[];
recordActions?: TableAction[];
onPageChange?: ActionPayload[];
onRowSelected?: ActionPayload[];
selectedRow?: SelectedRow;
2019-09-12 08:11:25 +00:00
}
export default TableWidget;