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

116 lines
3.5 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() {
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 (
<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;
2020-01-17 09:28:26 +00:00
this.updateSelectedRowProperty(index);
super.executeAction(onRowSelected);
}}
2020-01-17 09:28:26 +00:00
/>
2019-10-31 10:10:57 +00:00
);
2019-09-12 08:11:25 +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,
// );
// }
// }
2020-01-23 07:53:36 +00:00
onCommandClick = (actions: ActionPayload[]) => {
super.executeAction(actions);
};
2020-01-17 09:28:26 +00:00
updateSelectedRowProperty(index: number) {
const { widgetId } = this.props;
2020-01-17 09:28:26 +00:00
this.updateWidgetProperty(widgetId, "selectedRowIndex", 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
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[];
2019-09-12 08:11:25 +00:00
}
export default TableWidget;