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

247 lines
7.5 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-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";
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,
searchKey: VALIDATION_TYPES.TEXT,
// 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,
searchKey: "",
2020-04-17 16:15:09 +00:00
};
}
2020-02-18 10:41:52 +00:00
static getTriggerPropertyMap(): TriggerPropertiesMap {
return {
onRowSelected: true,
onPageChange: true,
onSearch: true,
2020-02-18 10:41:52 +00:00
};
}
searchTableData = (tableData: object[]) => {
const searchKey =
this.props.searchKey !== undefined
? this.props.searchKey.toString().toUpperCase()
: "";
return tableData.filter((item: object) => {
return Object.values(item)
.join(", ")
.toUpperCase()
.includes(searchKey);
});
};
2019-09-12 08:11:25 +00:00
getPageView() {
2020-03-30 08:16:43 +00:00
const { tableData, hiddenColumns } = this.props;
const filteredTableData = this.searchTableData(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
}
const { componentWidth, componentHeight } = this.getComponentDimensions();
2020-06-03 10:50:10 +00:00
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={filteredTableData}
isLoading={this.props.isLoading}
widgetId={this.props.widgetId}
searchKey={this.props.searchKey}
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);
}}
searchTableData={this.handleSearchTable}
/>
2020-06-03 10:50:10 +00:00
</Suspense>
);
2019-09-12 08:11:25 +00:00
}
handleSearchTable = (searchKey: any) => {
const { onSearch } = this.props;
super.updateWidgetMetaProperty("searchKey", searchKey);
if (onSearch) {
super.executeAction({
dynamicString: onSearch,
event: {
type: EventType.ON_SEARCH,
},
});
}
};
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
};
onCommandClick = (action: string, onComplete: () => void) => {
2020-02-18 10:41:52 +00:00
super.executeAction({
dynamicString: action,
event: {
type: EventType.ON_CLICK,
callback: onComplete,
2020-02-18 10:41:52 +00:00
},
});
};
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;
searchKey: 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;
onSearch: 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;