/* eslint-disable no-unused-vars */ import * as React from "react"; import PropTypes from "prop-types"; import TableHead from "@mui/material/TableHead"; import TableRow from "@mui/material/TableRow"; import TableCell from "@mui/material/TableCell"; import TableSortLabel from "@mui/material/TableSortLabel"; import Checkbox from "@mui/material/Checkbox"; import { withStyles } from "@mui/styles"; import { Draggable } from "react-beautiful-dnd"; import { Tooltip } from "@mui/material"; import * as CommonValues from "../utils/common-values"; import equal from "fast-deep-equal"; /* eslint-enable no-unused-vars */ export class MTableHeader extends React.Component { constructor(props) { super(props); this.state = { lastX: 0, resizingColumnDef: undefined, }; } // shouldComponentUpdate(nextProps, nextState){ // return !equal(nextProps, this.props) || !equal(nextState, this.state); // } componentDidMount() { document.addEventListener("mousemove", this.handleMouseMove); document.addEventListener("mouseup", this.handleMouseUp); } componentWillUnmount() { document.removeEventListener("mousemove", this.handleMouseMove); document.removeEventListener("mouseup", this.handleMouseUp); } handleMouseDown = (e, columnDef) => { this.setState({ lastAdditionalWidth: columnDef.tableData.additionalWidth, lastX: e.clientX, resizingColumnDef: columnDef, }); }; handleMouseMove = (e) => { if (!this.state.resizingColumnDef) { return; } let additionalWidth = this.state.lastAdditionalWidth + e.clientX - this.state.lastX; additionalWidth = Math.min( this.state.resizingColumnDef.maxWidth || additionalWidth, additionalWidth ); if ( this.state.resizingColumnDef.tableData.additionalWidth !== additionalWidth ) { this.props.onColumnResized( this.state.resizingColumnDef.tableData.id, additionalWidth ); } }; handleMouseUp = (e) => { this.setState({ resizingColumnDef: undefined }); }; getCellStyle = (columnDef) => { const width = CommonValues.reducePercentsInCalc( columnDef.tableData.width, this.props.scrollWidth ); const style = { ...this.props.headerStyle, ...columnDef.headerStyle, boxSizing: "border-box", width, maxWidth: columnDef.maxWidth, minWidth: columnDef.minWidth, }; if ( this.props.options.tableLayout === "fixed" && this.props.options.columnResizable && columnDef.resizable !== false ) { style.paddingRight = 2; } return style; }; renderHeader() { const size = this.props.options.padding === "default" ? "medium" : "small"; const mapArr = this.props.columns .filter( (columnDef) => !columnDef.hidden && !(columnDef.tableData.groupOrder > -1) ) .sort((a, b) => a.tableData.columnOrder - b.tableData.columnOrder) .map((columnDef, index) => { let content = columnDef.title; if (this.props.draggable) { content = ( {(provided, snapshot) => (
{columnDef.title}
)}
); } if (columnDef.sorting !== false && this.props.sorting) { content = ( { const orderDirection = columnDef.tableData.id !== this.props.orderBy ? "asc" : this.props.orderDirection === "asc" ? "desc" : this.props.orderDirection === "desc" && this.props.thirdSortClick ? "" : this.props.orderDirection === "desc" && !this.props.thirdSortClick ? "asc" : this.props.orderDirection === "" ? "asc" : "desc"; this.props.onOrderChange( columnDef.tableData.id, orderDirection ); }} > {content} ); } if (columnDef.tooltip) { content = ( {content} ); } if ( this.props.options.tableLayout === "fixed" && this.props.options.columnResizable && columnDef.resizable !== false ) { content = (
{content}
this.handleMouseDown(e, columnDef)} />
); } const cellAlignment = columnDef.align !== undefined ? columnDef.align : ["numeric", "currency"].indexOf(columnDef.type) !== -1 ? "right" : "left"; return ( {content} ); }); return mapArr; } renderActionsHeader() { const localization = { ...MTableHeader.defaultProps.localization, ...this.props.localization, }; const width = CommonValues.actionsColumnWidth(this.props); return ( {localization.actions} ); } renderSelectionHeader() { const selectionWidth = CommonValues.selectionMaxWidth( this.props, this.props.treeDataMaxLevel ); return ( {this.props.showSelectAllCheckbox && ( 0 && this.props.selectedCount < this.props.dataCount } checked={ this.props.dataCount > 0 && this.props.selectedCount === this.props.dataCount } onChange={(event, checked) => this.props.onAllSelected && this.props.onAllSelected(checked) } {...this.props.options.headerSelectionProps} /> )} ); } renderDetailPanelColumnCell() { return ( ); } render() { const headers = this.renderHeader(); if (this.props.hasSelection) { headers.splice(0, 0, this.renderSelectionHeader()); } if (this.props.showActionsColumn) { if (this.props.actionsHeaderIndex >= 0) { let endPos = 0; if (this.props.hasSelection) { endPos = 1; } headers.splice( this.props.actionsHeaderIndex + endPos, 0, this.renderActionsHeader() ); } else if (this.props.actionsHeaderIndex === -1) { headers.push(this.renderActionsHeader()); } } if (this.props.hasDetailPanel) { if (this.props.detailPanelColumnAlignment === "right") { headers.push(this.renderDetailPanelColumnCell()); } else { headers.splice(0, 0, this.renderDetailPanelColumnCell()); } } if (this.props.isTreeData > 0) { headers.splice( 0, 0, ); } this.props.columns .filter((columnDef) => columnDef.tableData.groupOrder > -1) .forEach((columnDef) => { headers.splice( 0, 0, ); }); return ( {headers} ); } } MTableHeader.defaultProps = { dataCount: 0, hasSelection: false, headerStyle: {}, selectedCount: 0, sorting: true, localization: { actions: "Actions", }, orderBy: undefined, orderDirection: "asc", actionsHeaderIndex: 0, detailPanelColumnAlignment: "left", draggable: true, thirdSortClick: true, }; MTableHeader.propTypes = { columns: PropTypes.array.isRequired, dataCount: PropTypes.number, hasDetailPanel: PropTypes.bool.isRequired, detailPanelColumnAlignment: PropTypes.string, hasSelection: PropTypes.bool, headerStyle: PropTypes.object, localization: PropTypes.object, selectedCount: PropTypes.number, sorting: PropTypes.bool, onAllSelected: PropTypes.func, onOrderChange: PropTypes.func, orderBy: PropTypes.number, orderDirection: PropTypes.string, actionsHeaderIndex: PropTypes.number, showActionsColumn: PropTypes.bool, showSelectAllCheckbox: PropTypes.bool, draggable: PropTypes.bool, thirdSortClick: PropTypes.bool, tooltip: PropTypes.string, }; export const styles = (theme) => ({ header: { // display: 'inline-block', position: "sticky", top: 0, zIndex: 10, backgroundColor: theme.palette.background.paper, // Change according to theme, }, }); export default withStyles(styles, { withTheme: true })(MTableHeader);