2020-04-15 12:15:12 +00:00
|
|
|
import React, { lazy, Suspense } 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";
|
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-12-30 10:02:23 +00:00
|
|
|
|
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";
|
2019-12-30 10:02:23 +00:00
|
|
|
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";
|
2019-10-31 10:10:57 +00:00
|
|
|
|
2020-04-15 12:15:12 +00:00
|
|
|
const TableComponent = lazy(() =>
|
|
|
|
|
import("components/designSystems/syncfusion/TableComponent"),
|
|
|
|
|
);
|
|
|
|
|
|
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 = {};
|
2019-10-31 11:13:59 +00:00
|
|
|
data.forEach(dataItem => {
|
2019-10-31 11:26:37 +00:00
|
|
|
Object.assign(listItemWithAllProperties, dataItem);
|
|
|
|
|
});
|
2019-12-30 10:02:23 +00:00
|
|
|
forIn(listItemWithAllProperties, (value: any, key: string) => {
|
2019-10-31 10:10:57 +00:00
|
|
|
cols.push({
|
2019-12-30 10:02:23 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
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-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;
|
2020-02-11 10:13:06 +00:00
|
|
|
super.updateWidgetMetaProperty("pageNo", pageNo);
|
2020-02-07 02:32:52 +00:00
|
|
|
}
|
2020-03-27 09:02:11 +00:00
|
|
|
const { componentWidth, componentHeight } = this.getComponentDimensions();
|
2019-10-31 10:10:57 +00:00
|
|
|
return (
|
2020-04-15 12:15:12 +00:00
|
|
|
<Suspense fallback={null}>
|
|
|
|
|
<TableComponent
|
|
|
|
|
id={this.props.widgetName}
|
|
|
|
|
data={this.props.tableData}
|
|
|
|
|
columns={columns}
|
|
|
|
|
isLoading={this.props.isLoading}
|
|
|
|
|
height={componentHeight}
|
|
|
|
|
width={componentWidth}
|
|
|
|
|
disableDrag={(disable: boolean) => {
|
|
|
|
|
this.disableDrag(disable);
|
|
|
|
|
}}
|
|
|
|
|
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}
|
|
|
|
|
updatePageSize={(pageSize: number) => {
|
|
|
|
|
super.updateWidgetMetaProperty("pageSize", pageSize);
|
|
|
|
|
}}
|
|
|
|
|
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
|
|
|
}
|
2019-12-30 10:02:23 +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-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 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[];
|
2020-02-18 10:41:52 +00:00
|
|
|
onPageChange?: string;
|
|
|
|
|
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;
|