2019-12-30 10:02:23 +00:00
|
|
|
import {
|
|
|
|
|
ColumnDirective,
|
|
|
|
|
ColumnsDirective,
|
|
|
|
|
GridComponent,
|
|
|
|
|
ColumnModel,
|
|
|
|
|
Grid,
|
|
|
|
|
Inject,
|
|
|
|
|
Resize,
|
2020-01-03 10:50:29 +00:00
|
|
|
Page,
|
2020-01-06 06:28:52 +00:00
|
|
|
SelectionSettingsModel,
|
2019-12-30 10:02:23 +00:00
|
|
|
} from "@syncfusion/ej2-react-grids";
|
2020-01-08 12:53:26 +00:00
|
|
|
import React, { useEffect, useRef, MutableRefObject, memo } from "react";
|
2019-12-30 10:02:23 +00:00
|
|
|
import styled from "constants/DefaultTheme";
|
|
|
|
|
|
|
|
|
|
export interface TableComponentProps {
|
|
|
|
|
data: object[];
|
|
|
|
|
columns: ColumnModel[];
|
|
|
|
|
selectedRowIndex?: number;
|
|
|
|
|
onRowClick: (rowData: object, rowIndex: number) => void;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
height: number;
|
|
|
|
|
width: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StyledGridComponent = styled(GridComponent)`
|
|
|
|
|
.e-altrow {
|
|
|
|
|
background-color: #fafafa;
|
|
|
|
|
}
|
|
|
|
|
`;
|
2020-01-06 06:28:52 +00:00
|
|
|
const settings: SelectionSettingsModel = {
|
|
|
|
|
type: "Multiple",
|
|
|
|
|
};
|
2019-12-30 10:02:23 +00:00
|
|
|
|
2020-01-08 12:53:26 +00:00
|
|
|
type GridRef = MutableRefObject<Grid | undefined>;
|
|
|
|
|
|
|
|
|
|
function reCalculatePageSize(grid: GridRef) {
|
|
|
|
|
if (grid.current) {
|
|
|
|
|
const rowHeight: number = grid.current.getRowHeight();
|
|
|
|
|
/** Grid height */
|
|
|
|
|
const gridHeight: number = grid.current.height as number;
|
|
|
|
|
/** initial page size */
|
|
|
|
|
const pageSize: number = grid.current.pageSettings.pageSize as number;
|
|
|
|
|
/** new page size is obtained here */
|
|
|
|
|
const pageResize: any = (gridHeight - pageSize * rowHeight) / rowHeight;
|
|
|
|
|
grid.current.pageSettings.pageSize = pageSize + Math.round(pageResize);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* eslint-disable react/display-name */
|
|
|
|
|
const TableComponent = memo(
|
|
|
|
|
(props: TableComponentProps) => {
|
|
|
|
|
const grid: GridRef = useRef();
|
|
|
|
|
|
|
|
|
|
// componentDidUpdate start
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
props.height && reCalculatePageSize(grid);
|
|
|
|
|
}, [props.height]);
|
|
|
|
|
// componentDidUpdate end
|
|
|
|
|
|
|
|
|
|
function dataBound() {
|
|
|
|
|
if (grid.current) {
|
|
|
|
|
grid.current.autoFitColumns();
|
2020-01-03 10:50:29 +00:00
|
|
|
}
|
2019-12-30 10:02:23 +00:00
|
|
|
}
|
2020-01-03 10:50:29 +00:00
|
|
|
|
2020-01-08 12:53:26 +00:00
|
|
|
function rowSelected() {
|
|
|
|
|
if (grid.current) {
|
|
|
|
|
/** Get the selected row indexes */
|
|
|
|
|
const selectedrowindex: number[] = grid.current.getSelectedRowIndexes();
|
|
|
|
|
/** Get the selected records. */
|
|
|
|
|
const selectedrecords: object[] = grid.current.getSelectedRecords();
|
|
|
|
|
if (selectedrecords.length !== 0) {
|
|
|
|
|
props.onRowClick(selectedrecords[0], selectedrowindex[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-30 10:02:23 +00:00
|
|
|
}
|
2020-01-06 06:28:52 +00:00
|
|
|
|
2019-12-30 10:02:23 +00:00
|
|
|
return (
|
|
|
|
|
<StyledGridComponent
|
2020-01-06 06:28:52 +00:00
|
|
|
selectionSettings={settings}
|
2020-01-08 12:53:26 +00:00
|
|
|
dataSource={props.data}
|
|
|
|
|
rowSelected={rowSelected}
|
|
|
|
|
ref={grid}
|
|
|
|
|
width={props.width - 16}
|
|
|
|
|
height={props.height - 107}
|
|
|
|
|
dataBound={dataBound}
|
2020-01-03 10:50:29 +00:00
|
|
|
allowPaging={true}
|
2019-12-30 10:02:23 +00:00
|
|
|
>
|
2020-01-03 10:50:29 +00:00
|
|
|
<Inject services={[Resize, Page]} />
|
2019-12-30 10:02:23 +00:00
|
|
|
<ColumnsDirective>
|
2020-01-08 12:53:26 +00:00
|
|
|
{props.columns.map(col => {
|
2019-12-30 10:02:23 +00:00
|
|
|
return <ColumnDirective key={col.field} field={col.field} />;
|
|
|
|
|
})}
|
|
|
|
|
</ColumnsDirective>
|
|
|
|
|
</StyledGridComponent>
|
|
|
|
|
);
|
2020-01-08 12:53:26 +00:00
|
|
|
},
|
|
|
|
|
(prevProps, nextProps) => {
|
|
|
|
|
const propsNotEqual =
|
|
|
|
|
JSON.stringify(nextProps.data) !== JSON.stringify(prevProps.data) ||
|
|
|
|
|
nextProps.height !== prevProps.height ||
|
|
|
|
|
nextProps.width !== prevProps.width;
|
|
|
|
|
|
|
|
|
|
return !propsNotEqual;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default TableComponent;
|