2019-09-12 08:11:25 +00:00
|
|
|
import React from "react";
|
2019-09-13 10:45:49 +00:00
|
|
|
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
|
2019-09-12 08:11:25 +00:00
|
|
|
import { WidgetType } from "../constants/WidgetConstants";
|
2019-09-13 11:59:45 +00:00
|
|
|
import { ActionPayload } from "../constants/ActionConstants";
|
2019-10-31 10:10:57 +00:00
|
|
|
import BaseTable, { AutoResizer } from "react-base-table";
|
|
|
|
|
import "react-base-table/styles.css";
|
|
|
|
|
import { forIn } from "lodash";
|
|
|
|
|
|
|
|
|
|
interface Column {
|
|
|
|
|
key: string;
|
|
|
|
|
dataKey: string;
|
|
|
|
|
title: string;
|
|
|
|
|
width: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function constructColumns(data: object[]): Column[] {
|
|
|
|
|
let cols: Column[] = [];
|
2019-10-31 11:13:59 +00:00
|
|
|
let listItemWithAllProperties = {}
|
|
|
|
|
data.forEach(dataItem => {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseTableArray(parsable: string): object[] {
|
|
|
|
|
let data: object[] = [];
|
|
|
|
|
try {
|
|
|
|
|
data = JSON.parse(parsable);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
console.log(ex);
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
class TableWidget extends BaseWidget<TableWidgetProps, WidgetState> {
|
2019-09-12 08:11:25 +00:00
|
|
|
getPageView() {
|
2019-10-31 10:10:57 +00:00
|
|
|
const tableData = parseTableArray(
|
|
|
|
|
this.props.tableData ? ((this.props.tableData as any) as string) : "",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let columns = constructColumns(tableData);
|
|
|
|
|
return (
|
|
|
|
|
<AutoResizer>
|
|
|
|
|
{({ width, height }: { width: number; height: number }) => (
|
|
|
|
|
<BaseTable
|
|
|
|
|
width={width}
|
|
|
|
|
height={height}
|
|
|
|
|
columns={columns}
|
|
|
|
|
data={tableData}
|
|
|
|
|
maxHeight={height}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AutoResizer>
|
|
|
|
|
);
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getWidgetType(): WidgetType {
|
|
|
|
|
return "TABLE_WIDGET";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
export type PaginationType = "PAGES" | "INFINITE_SCROLL";
|
2019-09-12 08:11:25 +00:00
|
|
|
|
2019-09-19 11:29:24 +00:00
|
|
|
export interface TableAction extends ActionPayload {
|
|
|
|
|
actionName: string;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
export interface TableWidgetProps extends WidgetProps {
|
2019-09-19 11:29:24 +00:00
|
|
|
nextPageKey?: string;
|
|
|
|
|
prevPageKey?: string;
|
2019-09-13 10:45:49 +00:00
|
|
|
label: string;
|
|
|
|
|
tableData?: object[];
|
2019-09-19 11:29:24 +00:00
|
|
|
recordActions?: TableAction[];
|
2019-09-13 11:59:45 +00:00
|
|
|
onPageChange?: ActionPayload[];
|
|
|
|
|
onRowSelected?: ActionPayload[];
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TableWidget;
|