DocSpace-buildtools/packages/asc-web-components/table-container/TableHeader.js

181 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-07-15 07:57:06 +00:00
import React from "react";
import PropTypes from "prop-types";
import throttle from "lodash.throttle";
2021-07-20 13:01:54 +00:00
import {
StyledTableHeader,
StyledTableRow,
StyledEmptyTableContainer,
} from "./StyledTableContainer";
import Checkbox from "../checkbox";
import TableSettings from "./TableSettings";
import TableHeaderCell from "./TableHeaderCell";
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
const TABLE_SIZE = "tableSize";
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
class TableHeader extends React.Component {
constructor(props) {
super(props);
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
this.state = { columnIndex: null };
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
this.headerRef = React.createRef();
this.throttledResize = throttle(this.onResize, 0);
}
componentDidMount() {
this.onResize();
2021-07-15 07:57:06 +00:00
window.addEventListener("resize", this.throttledResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.throttledResize);
}
getSubstring = (str) => str.substring(0, str.length - 2);
onMouseMove = (e) => {
const { columnIndex } = this.state;
const { containerRef } = this.props;
2021-07-13 08:07:50 +00:00
if (!columnIndex) return;
const column = document.getElementById("column_" + columnIndex);
const columnSize = column.getBoundingClientRect();
const newWidth = e.clientX - columnSize.left;
2021-07-15 07:57:06 +00:00
const tableContainer = containerRef.current.style.gridTemplateColumns;
const widths = tableContainer.split(" ");
//getSubstring(widths[+columnIndex])
if (newWidth <= 150) {
widths[+columnIndex] = widths[+columnIndex];
} else {
const offset = +this.getSubstring(widths[+columnIndex]) - newWidth;
const column2Width = +this.getSubstring(widths[+columnIndex + 1]);
//getSubstring(widths[+columnIndex])
if (column2Width + offset >= 150) {
widths[+columnIndex] = newWidth + "px";
widths[+columnIndex + 1] = column2Width + offset + "px";
}
}
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
containerRef.current.style.gridTemplateColumns = widths.join(" ");
this.headerRef.current.style.gridTemplateColumns = widths.join(" ");
};
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
onMouseUp = () => {
localStorage.setItem(
TABLE_SIZE,
this.props.containerRef.current.style.gridTemplateColumns
);
window.removeEventListener("mousemove", this.onMouseMove);
window.removeEventListener("mouseup", this.onMouseUp);
};
onMouseDown = (event) => {
this.setState({ columnIndex: event.target.dataset.column });
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
window.addEventListener("mousemove", this.onMouseMove);
window.addEventListener("mouseup", this.onMouseUp);
};
onResize = () => {
const { containerRef } = this.props;
const container = containerRef.current
? containerRef.current
: document.getElementById("table-container");
2021-07-15 07:57:06 +00:00
const storageSize = localStorage.getItem(TABLE_SIZE);
const tableContainer = storageSize
? storageSize.split(" ")
: container.style.gridTemplateColumns.split(" ");
2021-07-15 07:57:06 +00:00
const containerWidth = +container.clientWidth;
2021-07-15 07:57:06 +00:00
const newContainerWidth = containerWidth - 32 - 80 - 24;
let str = "";
if (tableContainer.length > 1) {
const gridTemplateColumns = [];
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
const oldWidth = tableContainer
.map((column) => +this.getSubstring(column))
.reduce((x, y) => x + y);
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
for (let index in tableContainer) {
const item = tableContainer[index];
if (item !== "24px" && item !== "32px" && item !== "80px") {
const percent = (+this.getSubstring(item) / oldWidth) * 100;
const newItemWidth = (containerWidth * percent) / 100 + "px";
gridTemplateColumns.push(newItemWidth);
} else {
gridTemplateColumns.push(item);
2021-07-13 08:07:50 +00:00
}
2021-07-15 07:57:06 +00:00
str = gridTemplateColumns.join(" ");
}
} else {
const column = (newContainerWidth * 40) / 100 + "px";
const otherColumns = (newContainerWidth * 20) / 100 + "px";
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
str = `32px ${column} ${otherColumns} ${otherColumns} ${otherColumns} 80px 24px`;
}
container.style.gridTemplateColumns = str;
2021-07-15 07:57:06 +00:00
this.headerRef.current.style.gridTemplateColumns = str;
this.headerRef.current.style.width = containerWidth + "px";
2021-07-13 08:07:50 +00:00
2021-07-15 07:57:06 +00:00
localStorage.setItem(TABLE_SIZE, str);
2021-07-13 08:07:50 +00:00
};
onChange = (checked) => {
this.props.setSelected(checked ? "all" : "none");
2021-07-13 08:07:50 +00:00
};
2021-07-15 07:57:06 +00:00
render() {
const { columns, ...rest } = this.props;
2021-07-15 07:57:06 +00:00
return (
2021-07-20 13:01:54 +00:00
<>
<StyledTableHeader
className="table-container_header"
ref={this.headerRef}
{...rest}
>
<StyledTableRow>
<Checkbox onChange={this.onChange} isChecked={false} />
{columns.map((column, index) => {
return (
column.enable && (
<TableHeaderCell
key={column.key}
index={index}
column={column}
onMouseDown={this.onMouseDown}
/>
)
);
})}
<div className="table-container_header-cell">
<TableSettings columns={columns} />
</div>
</StyledTableRow>
</StyledTableHeader>
<StyledEmptyTableContainer />
</>
2021-07-15 07:57:06 +00:00
);
}
}
TableHeader.propTypes = {
containerRef: PropTypes.shape({ current: PropTypes.any }),
columns: PropTypes.array.isRequired,
setSelected: PropTypes.func,
2021-07-13 08:07:50 +00:00
};
export default TableHeader;