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

131 lines
4.0 KiB
TypeScript
Raw Normal View History

2019-09-12 08:11:25 +00:00
import React from "react";
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";
2020-01-17 09:28:26 +00:00
import { forIn } from "lodash";
import TableComponent from "components/designSystems/syncfusion/TableComponent";
2019-11-25 05:07:27 +00:00
import { VALIDATION_TYPES } from "constants/WidgetValidation";
import { WidgetPropertyValidationType } from "utils/ValidationFactory";
import { ColumnModel } from "@syncfusion/ej2-grids";
import { ColumnDirTypecast } from "@syncfusion/ej2-react-grids";
2020-01-23 07:53:36 +00:00
import { ColumnAction } from "components/propertyControls/ColumnActionSelectorControl";
2019-10-31 10:10:57 +00:00
function constructColumns(data: object[]): ColumnModel[] | ColumnDirTypecast[] {
const cols: ColumnModel[] | ColumnDirTypecast[] = [];
2019-10-31 11:26:37 +00:00
const listItemWithAllProperties = {};
data.forEach(dataItem => {
2019-10-31 11:26:37 +00:00
Object.assign(listItemWithAllProperties, dataItem);
});
forIn(listItemWithAllProperties, (value: any, key: string) => {
2019-10-31 10:10:57 +00:00
cols.push({
field: key,
2019-10-31 10:10:57 +00:00
width: 200,
});
});
return cols;
}
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,
2020-01-17 09:28:26 +00:00
selectedRowIndex: VALIDATION_TYPES.NUMBER,
};
}
static getDerivedPropertiesMap() {
return {
selectedRow: "{{this.tableData[this.selectedRowIndex]}}",
2019-11-19 12:44:58 +00:00
};
}
2019-09-12 08:11:25 +00:00
getPageView() {
const { tableData } = this.props;
2019-11-22 13:12:39 +00:00
const columns = constructColumns(tableData);
2020-02-07 02:32:52 +00:00
2020-02-07 11:34:57 +00:00
const serverSidePaginationEnabled = (this.props
.serverSidePaginationEnabled &&
this.props.serverSidePaginationEnabled) as boolean;
2020-02-07 02:32:52 +00:00
let pageNo = this.props.pageNo;
if (pageNo === undefined) {
pageNo = 1;
super.updateWidgetMetaProperty("pageNo", pageNo);
2020-02-07 02:32:52 +00:00
}
2019-10-31 10:10:57 +00:00
return (
<TableComponent
data={this.props.tableData}
columns={columns}
isLoading={this.props.isLoading}
height={this.state.componentHeight}
width={this.state.componentWidth}
disableDrag={(disable: boolean) => {
this.disableDrag(disable);
}}
2020-01-23 07:53:36 +00:00
columnActions={this.props.columnActions}
onCommandClick={this.onCommandClick}
onRowClick={(rowData: object, index: number) => {
const { onRowSelected } = this.props;
this.updateWidgetProperty("selectedRowIndex", index);
2020-01-17 09:28:26 +00:00
super.executeAction(onRowSelected);
}}
2020-02-07 02:32:52 +00:00
serverSidePaginationEnabled={serverSidePaginationEnabled}
pageNo={pageNo}
nextPageClick={() => {
let pageNo = this.props.pageNo || 1;
pageNo = pageNo + 1;
super.updateWidgetMetaProperty("pageNo", pageNo);
2020-02-07 02:32:52 +00:00
super.executeAction(this.props.onPageChange, "NEXT");
}}
prevPageClick={() => {
let pageNo = this.props.pageNo || 1;
pageNo = pageNo - 1;
if (pageNo >= 1) {
super.updateWidgetMetaProperty("pageNo", pageNo);
2020-02-07 02:32:52 +00:00
super.executeAction(this.props.onPageChange, "PREV");
}
}}
updatePageNo={(pageNo: number) => {
super.updateWidgetMetaProperty("pageNo", pageNo);
2020-02-07 02:32:52 +00:00
}}
updatePageSize={(pageSize: number) => {
super.updateWidgetMetaProperty("pageSize", pageSize);
2020-02-07 02:32:52 +00:00
}}
2020-01-17 09:28:26 +00:00
/>
2019-10-31 10:10:57 +00:00
);
2019-09-12 08:11:25 +00:00
}
2020-01-23 07:53:36 +00:00
onCommandClick = (actions: ActionPayload[]) => {
super.executeAction(actions);
};
2019-09-12 08:11:25 +00:00
getWidgetType(): WidgetType {
return "TABLE_WIDGET";
}
}
type RowData = {
rowIndex: number;
};
type SelectedRow = object & RowData;
export interface TableWidgetProps extends WidgetProps {
nextPageKey?: string;
prevPageKey?: string;
label: string;
2019-11-22 13:12:39 +00:00
tableData: object[];
recordActions?: TableAction[];
onPageChange?: ActionPayload[];
onRowSelected?: ActionPayload[];
2020-01-17 09:28:26 +00:00
selectedRowIndex?: number;
2020-01-23 07:53:36 +00:00
columnActions?: ColumnAction[];
2020-02-07 11:34:57 +00:00
serverSidePaginationEnabled?: boolean;
2019-09-12 08:11:25 +00:00
}
export default TableWidget;