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

289 lines
9.3 KiB
TypeScript
Raw Normal View History

2020-06-16 07:37:39 +00:00
import React, { 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-06-16 07:37:39 +00:00
// import { forIn } from "lodash";
2020-06-03 10:50:10 +00:00
import ReactTableComponent from "components/designSystems/appsmith/ReactTableComponent";
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";
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";
2020-06-16 07:37:39 +00:00
import { Classes } from "@blueprintjs/core";
2019-10-31 10:10:57 +00:00
2020-06-16 07:37:39 +00:00
// const ROW_HEIGHT = 37;
// const TABLE_HEADER_HEIGHT = 39;
// const TABLE_FOOTER_HEIGHT = 48;
// const TABLE_EXPORT_HEIGHT = 43;
2020-04-29 05:13:02 +00:00
2020-06-16 07:37:39 +00:00
// function constructColumns(
// data: object[],
// hiddenColumns?: string[],
// ): ColumnModel[] | ColumnDirTypecast[] {
// let cols: ColumnModel[] | ColumnDirTypecast[] = [];
// const listItemWithAllProperties = {};
// data.forEach(dataItem => {
// Object.assign(listItemWithAllProperties, dataItem);
// });
// forIn(listItemWithAllProperties, (value: any, key: string) => {
// cols.push({
// field: key,
// visible: !hiddenColumns?.includes(key),
// });
// });
// cols = (cols as any[]).filter(col => col.field !== "_color") as
// | ColumnModel[]
// | ColumnDirTypecast[];
// return cols;
// }
2019-10-31 10:10:57 +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-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;
2020-06-16 07:37:39 +00:00
// 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
2020-06-16 07:37:39 +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;
2020-06-03 10:50:10 +00:00
// Use below code to calculate page size for old table component
// const pageSize = Math.floor(tableContentHeight / ROW_HEIGHT);
const pageSize = Math.floor((componentHeight - 104) / 52);
2020-04-29 05:13:02 +00:00
if (pageSize !== this.props.pageSize) {
super.updateWidgetMetaProperty("pageSize", pageSize);
}
2020-06-03 10:50:10 +00:00
// /*
return (
<Suspense fallback={<Skeleton />}>
<ReactTableComponent
height={componentHeight}
width={componentWidth}
tableData={tableData}
isLoading={this.props.isLoading}
widgetId={this.props.widgetId}
renderMode={this.props.renderMode}
hiddenColumns={hiddenColumns}
columnActions={this.props.columnActions}
columnNameMap={this.props.columnNameMap}
columnTypeMap={this.props.columnTypeMap}
columnOrder={this.props.columnOrder}
pageSize={pageSize}
onCommandClick={this.onCommandClick}
selectedRowIndex={
this.props.selectedRowIndex === undefined
? -1
: this.props.selectedRowIndex
}
serverSidePaginationEnabled={serverSidePaginationEnabled}
onRowClick={this.handleRowClick}
pageNo={pageNo}
nextPageClick={this.handleNextPageClick}
prevPageClick={this.handlePrevPageClick}
updatePageNo={(pageNo: number) => {
super.updateWidgetMetaProperty("pageNo", pageNo);
}}
updateHiddenColumns={(hiddenColumns?: string[]) => {
super.updateWidgetProperty("hiddenColumns", hiddenColumns);
}}
updateColumnType={(columnTypeMap: {
[key: string]: { type: string; format: string };
}) => {
super.updateWidgetProperty("columnTypeMap", columnTypeMap);
}}
updateColumnName={(columnNameMap: { [key: string]: string }) => {
super.updateWidgetProperty("columnNameMap", columnNameMap);
}}
handleResizeColumn={(columnSizeMap: { [key: string]: number }) => {
super.updateWidgetProperty("columnSizeMap", columnSizeMap);
}}
handleReorderColumn={(columnOrder: string[]) => {
super.updateWidgetProperty("columnOrder", columnOrder);
}}
columnSizeMap={this.props.columnSizeMap}
resetSelectedRowIndex={this.resetSelectedRowIndex}
disableDrag={(disable: boolean) => {
this.disableDrag(disable);
}}
/>
2020-06-03 10:50:10 +00:00
</Suspense>
);
// */
/*
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
);
2020-06-03 10:50:10 +00:00
*/
2019-09-12 08:11:25 +00:00
}
2020-03-30 08:16:43 +00:00
updateHiddenColumns = (hiddenColumns?: string[]) => {
2020-06-03 10:50:10 +00:00
super.updateWidgetProperty("hiddenColumns", hiddenColumns);
2020-03-30 08:16:43 +00:00
};
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[];
2020-06-03 10:50:10 +00:00
columnOrder?: string[];
columnNameMap?: { [key: string]: string };
columnTypeMap?: { [key: string]: { type: string; format: string } };
columnSizeMap?: { [key: string]: number };
2019-09-12 08:11:25 +00:00
}
export default TableWidget;