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

231 lines
6.8 KiB
TypeScript
Raw Normal View History

import React, { lazy, Suspense } from "react";
import BaseWidget, { WidgetProps, WidgetState } from "./BaseWidget";
2019-11-25 05:07:27 +00:00
import { WidgetType } from "constants/WidgetConstants";
2020-02-18 10:41:52 +00:00
import { EventType } from "constants/ActionConstants";
2020-01-17 09:28:26 +00:00
import { forIn } from "lodash";
2019-11-25 05:07:27 +00:00
import { VALIDATION_TYPES } from "constants/WidgetValidation";
2020-03-16 07:59:07 +00:00
import {
WidgetPropertyValidationType,
BASE_WIDGET_VALIDATION,
} 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";
2020-02-18 10:41:52 +00:00
import { TriggerPropertiesMap } from "utils/WidgetFactory";
2020-04-16 08:32:58 +00:00
import Skeleton from "components/utils/Skeleton";
2019-10-31 10:10:57 +00:00
const TableComponent = lazy(() =>
import(
2020-04-17 04:59:43 +00:00
/* webpackPrefetch: true, webpackChunkName: "table" */ "components/designSystems/syncfusion/TableComponent"
),
);
2020-04-29 05:13:02 +00:00
const ROW_HEIGHT = 37;
const TABLE_HEADER_HEIGHT = 39;
const TABLE_FOOTER_HEIGHT = 48;
const TABLE_EXPORT_HEIGHT = 43;
2020-03-30 08:16:43 +00:00
function constructColumns(
data: object[],
hiddenColumns?: string[],
): ColumnModel[] | ColumnDirTypecast[] {
2020-04-15 09:03:35 +00:00
let 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,
2020-03-30 08:16:43 +00:00
visible: !hiddenColumns?.includes(key),
2019-10-31 10:10:57 +00:00
});
});
2020-04-15 09:03:35 +00:00
cols = (cols as any[]).filter(col => col.field !== "_color") as
| ColumnModel[]
| ColumnDirTypecast[];
2019-10-31 10:10:57 +00:00
return cols;
}
class TableWidget extends BaseWidget<TableWidgetProps, WidgetState> {
2019-11-19 12:44:58 +00:00
static getPropertyValidationMap(): WidgetPropertyValidationType {
return {
2020-03-16 07:59:07 +00:00
...BASE_WIDGET_VALIDATION,
2019-11-19 12:44:58 +00:00
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,
2020-03-31 10:40:52 +00:00
columnActions: VALIDATION_TYPES.ARRAY_ACTION_SELECTOR,
onRowSelected: VALIDATION_TYPES.ACTION_SELECTOR,
onPageChange: VALIDATION_TYPES.ACTION_SELECTOR,
2020-01-17 09:28:26 +00:00
};
}
static getDerivedPropertiesMap() {
return {
selectedRow: "{{this.tableData[this.selectedRowIndex]}}",
2019-11-19 12:44:58 +00:00
};
}
2020-04-17 16:15:09 +00:00
static getMetaPropertiesMap(): Record<string, any> {
return {
pageNo: 1,
pageSize: undefined,
selectedRowIndex: -1,
};
}
2020-02-18 10:41:52 +00:00
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onRowSelected: true,
onPageChange: true,
};
}
2019-09-12 08:11:25 +00:00
getPageView() {
2020-03-30 08:16:43 +00:00
const { tableData, hiddenColumns } = this.props;
const columns = constructColumns(tableData, hiddenColumns);
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
}
const { componentWidth, componentHeight } = this.getComponentDimensions();
2020-04-29 05:13:02 +00:00
const exportHeight =
this.props.exportCsv || this.props.exportPDF || this.props.exportCsv
? TABLE_EXPORT_HEIGHT
: 0;
const tableHeaderHeight =
this.props.tableData.length === 0 ? 2 : TABLE_HEADER_HEIGHT;
const tableContentHeight =
componentHeight - TABLE_FOOTER_HEIGHT - tableHeaderHeight - exportHeight;
const pageSize = Math.floor(tableContentHeight / ROW_HEIGHT);
if (pageSize !== this.props.pageSize) {
super.updateWidgetMetaProperty("pageSize", pageSize);
}
2019-10-31 10:10:57 +00:00
return (
2020-04-16 08:32:58 +00:00
<Suspense fallback={<Skeleton />}>
<TableComponent
id={this.props.widgetName}
data={this.props.tableData}
columns={columns}
isLoading={this.props.isLoading}
height={componentHeight}
2020-04-29 05:13:02 +00:00
contentHeight={tableContentHeight}
width={componentWidth}
disableDrag={(disable: boolean) => {
this.disableDrag(disable);
}}
2020-04-29 05:13:02 +00:00
pageSize={pageSize}
rowHeight={ROW_HEIGHT}
columnActions={this.props.columnActions}
onCommandClick={this.onCommandClick}
onRowClick={this.handleRowClick}
selectedRowIndex={this.props.selectedRowIndex || -1}
serverSidePaginationEnabled={serverSidePaginationEnabled}
pageNo={pageNo}
nextPageClick={this.handleNextPageClick}
prevPageClick={this.handlePrevPageClick}
updatePageNo={(pageNo: number) => {
super.updateWidgetMetaProperty("pageNo", pageNo);
}}
updateHiddenColumns={this.updateHiddenColumns}
resetSelectedRowIndex={this.resetSelectedRowIndex}
exportCsv={this.props.exportCsv}
exportPDF={this.props.exportPDF}
exportExcel={this.props.exportExcel}
/>
</Suspense>
2019-10-31 10:10:57 +00:00
);
2019-09-12 08:11:25 +00:00
}
2020-03-30 08:16:43 +00:00
updateHiddenColumns = (hiddenColumns?: string[]) => {
this.updateWidgetProperty("hiddenColumns", hiddenColumns);
};
2020-02-18 10:41:52 +00:00
onCommandClick = (action: string) => {
super.executeAction({
dynamicString: action,
event: {
type: EventType.ON_CLICK,
},
});
};
handleRowClick = (rowData: object, index: number) => {
const { onRowSelected } = this.props;
2020-02-24 10:45:36 +00:00
super.updateWidgetMetaProperty("selectedRowIndex", index);
2020-02-18 10:41:52 +00:00
if (onRowSelected) {
super.executeAction({
dynamicString: onRowSelected,
event: {
type: EventType.ON_ROW_SELECTED,
},
});
}
};
handleNextPageClick = () => {
let pageNo = this.props.pageNo || 1;
pageNo = pageNo + 1;
super.updateWidgetMetaProperty("pageNo", pageNo);
if (this.props.onPageChange) {
2020-02-27 03:05:14 +00:00
this.resetSelectedRowIndex();
2020-02-18 10:41:52 +00:00
super.executeAction({
dynamicString: this.props.onPageChange,
event: {
type: EventType.ON_NEXT_PAGE,
},
});
}
};
2020-02-27 03:05:14 +00:00
resetSelectedRowIndex = () => {
super.updateWidgetMetaProperty("selectedRowIndex", -1);
};
2020-02-18 10:41:52 +00:00
handlePrevPageClick = () => {
let pageNo = this.props.pageNo || 1;
pageNo = pageNo - 1;
if (pageNo >= 1) {
super.updateWidgetMetaProperty("pageNo", pageNo);
if (this.props.onPageChange) {
2020-02-27 03:05:14 +00:00
this.resetSelectedRowIndex();
2020-02-18 10:41:52 +00:00
super.executeAction({
dynamicString: this.props.onPageChange,
event: {
type: EventType.ON_PREV_PAGE,
},
});
}
}
2020-01-23 07:53:36 +00:00
};
2019-09-12 08:11:25 +00:00
getWidgetType(): WidgetType {
return "TABLE_WIDGET";
}
}
export interface TableWidgetProps extends WidgetProps {
nextPageKey?: string;
prevPageKey?: string;
label: string;
2019-11-22 13:12:39 +00:00
tableData: object[];
2020-02-18 10:41:52 +00:00
onPageChange?: string;
2020-04-29 05:13:02 +00:00
pageSize: number;
2020-02-18 10:41:52 +00:00
onRowSelected?: string;
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;
2020-03-30 08:16:43 +00:00
hiddenColumns?: string[];
2019-09-12 08:11:25 +00:00
}
export default TableWidget;