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-11-25 05:07:27 +00:00
|
|
|
import { WidgetType } from "constants/WidgetConstants";
|
2019-12-03 04:28:14 +00:00
|
|
|
import { ActionPayload, TableAction } 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";
|
2019-12-23 12:16:33 +00:00
|
|
|
import _, { forIn } from "lodash";
|
2019-11-07 11:17:53 +00:00
|
|
|
import SelectableTable, {
|
|
|
|
|
Column,
|
2019-11-25 05:07:27 +00:00
|
|
|
} from "components/designSystems/appsmith/TableComponent";
|
|
|
|
|
import { VALIDATION_TYPES } from "constants/WidgetValidation";
|
|
|
|
|
import { WidgetPropertyValidationType } from "utils/ValidationFactory";
|
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 = {};
|
2019-10-31 11:13:59 +00:00
|
|
|
data.forEach(dataItem => {
|
2019-10-31 11:26:37 +00:00
|
|
|
Object.assign(listItemWithAllProperties, dataItem);
|
|
|
|
|
});
|
2019-10-31 11:13:59 +00:00
|
|
|
forIn(listItemWithAllProperties, (value, key) => {
|
2019-10-31 10:10:57 +00:00
|
|
|
cols.push({
|
|
|
|
|
key: key,
|
|
|
|
|
dataKey: key,
|
|
|
|
|
width: 200,
|
|
|
|
|
title: key,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return cols;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 10:45:49 +00:00
|
|
|
class TableWidget extends BaseWidget<TableWidgetProps, WidgetState> {
|
2019-11-19 12:44:58 +00:00
|
|
|
static getPropertyValidationMap(): WidgetPropertyValidationType {
|
|
|
|
|
return {
|
|
|
|
|
tableData: VALIDATION_TYPES.TABLE_DATA,
|
2019-11-22 13:12:39 +00:00
|
|
|
nextPageKey: VALIDATION_TYPES.TEXT,
|
|
|
|
|
prevPageKey: VALIDATION_TYPES.TEXT,
|
|
|
|
|
label: VALIDATION_TYPES.TEXT,
|
2019-12-19 09:33:27 +00:00
|
|
|
selectedRow: VALIDATION_TYPES.OBJECT,
|
2019-11-19 12:44:58 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-12 08:11:25 +00:00
|
|
|
getPageView() {
|
2019-11-14 09:28:51 +00:00
|
|
|
const { tableData } = this.props;
|
2019-11-22 13:12:39 +00:00
|
|
|
const columns = constructColumns(tableData);
|
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-22 13:12:39 +00:00
|
|
|
data={tableData}
|
2019-10-31 10:10:57 +00:00
|
|
|
maxHeight={height}
|
2019-11-20 08:10:01 +00:00
|
|
|
isLoading={this.props.isLoading}
|
2019-12-19 09:33:27 +00:00
|
|
|
selectedRowIndex={
|
|
|
|
|
this.props.selectedRow && this.props.selectedRow.rowIndex
|
|
|
|
|
}
|
2019-11-07 11:17:53 +00:00
|
|
|
onRowClick={(rowData: object, index: number) => {
|
2019-11-14 11:05:45 +00:00
|
|
|
const { onRowSelected } = this.props;
|
2019-12-19 09:33:27 +00:00
|
|
|
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
|
|
|
}
|
2019-12-19 09:33:27 +00:00
|
|
|
componentDidUpdate(prevProps: TableWidgetProps) {
|
|
|
|
|
super.componentDidUpdate(prevProps);
|
|
|
|
|
if (
|
|
|
|
|
!_.isEqual(prevProps.tableData, this.props.tableData) &&
|
|
|
|
|
prevProps.selectedRow
|
|
|
|
|
) {
|
|
|
|
|
this.updateSelectedRowProperty(
|
|
|
|
|
this.props.tableData[prevProps.selectedRow.rowIndex],
|
|
|
|
|
prevProps.selectedRow.rowIndex,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-14 11:05:45 +00:00
|
|
|
|
2019-12-19 09:33:27 +00:00
|
|
|
updateSelectedRowProperty(rowData: object, index: number) {
|
2019-11-14 11:05:45 +00:00
|
|
|
const { widgetId } = this.props;
|
2019-12-19 09:33:27 +00:00
|
|
|
this.updateWidgetProperty(widgetId, "selectedRow", {
|
|
|
|
|
...rowData,
|
|
|
|
|
rowIndex: index,
|
|
|
|
|
});
|
2019-11-14 11:05:45 +00:00
|
|
|
}
|
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-11-14 11:05:45 +00:00
|
|
|
type RowData = {
|
2019-11-12 06:03:46 +00:00
|
|
|
rowIndex: number;
|
2019-11-14 11:05:45 +00:00
|
|
|
};
|
|
|
|
|
type SelectedRow = object & RowData;
|
2019-11-12 06:03:46 +00:00
|
|
|
|
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;
|
2019-11-22 13:12:39 +00:00
|
|
|
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-12-19 09:33:27 +00:00
|
|
|
selectedRow?: SelectedRow;
|
2019-09-12 08:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default TableWidget;
|