File size: 9,576 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'tss-react/mui';
const defaultResizeStyles = {
root: {
position: 'absolute',
},
resizer: {
position: 'absolute',
width: '1px',
height: '100%',
left: '100px',
cursor: 'ew-resize',
border: '0.1px solid rgba(224, 224, 224, 1)',
},
};
function getParentOffsetLeft(tableEl) {
let ii = 0,
parentOffsetLeft = 0,
offsetParent = tableEl.offsetParent;
while (offsetParent) {
parentOffsetLeft = parentOffsetLeft + (offsetParent.offsetLeft || 0) - (offsetParent.scrollLeft || 0);
offsetParent = offsetParent.offsetParent;
ii++;
if (ii > 1000) break;
}
return parentOffsetLeft;
}
class TableResize extends React.Component {
static propTypes = {
/** Extend the style applied to components */
classes: PropTypes.object,
};
state = {
resizeCoords: {},
priorPosition: {},
tableWidth: '100%',
tableHeight: '100%',
};
handleResize = () => {
if (window.innerWidth !== this.windowWidth) {
this.windowWidth = window.innerWidth;
this.setDividers();
}
};
componentDidMount() {
this.minWidths = [];
this.windowWidth = null;
this.props.setResizeable(this.setCellRefs);
this.props.updateDividers(() => this.setState({ updateCoords: true }, () => this.updateWidths));
window.addEventListener('resize', this.handleResize, false);
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize, false);
}
setCellRefs = (cellsRef, tableRef) => {
this.cellsRef = cellsRef;
this.tableRef = tableRef;
this.setDividers();
};
setDividers = () => {
const tableEl = this.tableRef;
const { width: tableWidth, height: tableHeight } = tableEl.getBoundingClientRect();
const { resizeCoords } = this.state;
for (let prop in resizeCoords) {
delete resizeCoords[prop];
}
let parentOffsetLeft = getParentOffsetLeft(tableEl);
let finalCells = Object.entries(this.cellsRef);
let cellMinusOne = finalCells.filter((_item, ix) => ix + 1 < finalCells.length);
cellMinusOne.forEach(([key, item], idx) => {
if (!item) return;
let elRect = item.getBoundingClientRect();
let left = elRect.left;
left = (left || 0) - parentOffsetLeft;
const elStyle = window.getComputedStyle(item, null);
resizeCoords[key] = { left: left + item.offsetWidth };
});
this.setState({ tableWidth, tableHeight, resizeCoords }, this.updateWidths);
};
updateWidths = () => {
let lastPosition = 0;
const { resizeCoords, tableWidth } = this.state;
Object.entries(resizeCoords).forEach(([key, item]) => {
let newWidth = Number(((item.left - lastPosition) / tableWidth) * 100);
/*
Using .toFixed(2) causes the columns to jitter when resized. On all browsers I (patrojk) have tested,
a width with a floating point decimal works fine. It's unclear to me why the numbers were being rouned.
However, I'm putting in an undocumented escape hatch to use toFixed in case the change introduces a bug.
The below code will be removed in a later release if no problems with non-rounded widths are reported.
*/
if (typeof this.props.resizableColumns === 'object' && this.props.resizableColumns.roundWidthPercentages) {
newWidth = newWidth.toFixed(2);
}
lastPosition = item.left;
const thCell = this.cellsRef[key];
if (thCell) thCell.style.width = newWidth + '%';
});
};
onResizeStart = (id, e) => {
const tableEl = this.tableRef;
const originalWidth = tableEl.style.width;
let lastColumn = 0;
tableEl.style.width = '1px';
let finalCells = Object.entries(this.cellsRef);
finalCells.forEach(([key, item], idx) => {
let elRect = item ? item.getBoundingClientRect() : { width: 0, left: 0 };
this.minWidths[key] = elRect.width;
lastColumn = Math.max(key, lastColumn);
});
tableEl.style.width = originalWidth;
this.setState({ isResize: true, id, lastColumn });
};
onResizeMove = (id, e) => {
const { isResize, resizeCoords, lastColumn } = this.state;
const prevCol = id => {
let nextId = id - 1;
while (typeof resizeCoords[nextId] === 'undefined' && nextId >= 0) {
nextId--;
}
return nextId;
};
const nextCol = id => {
let nextId = id + 1;
let tries = 0;
while (typeof resizeCoords[nextId] === 'undefined' && tries < 20) {
nextId++;
tries++;
}
return nextId;
};
const fixedMinWidth1 = this.minWidths[id];
const fixedMinWidth2 = this.minWidths[nextCol(parseInt(id, 10))] || this.minWidths[id];
const idNumber = parseInt(id, 10);
const finalCells = Object.entries(this.cellsRef);
const tableEl = this.tableRef;
const { width: tableWidth, height: tableHeight } = tableEl.getBoundingClientRect();
const { selectableRows } = this.props.options;
let parentOffsetLeft = getParentOffsetLeft(tableEl);
const nextCoord = id => {
let nextId = id + 1;
let tries = 0;
while (typeof resizeCoords[nextId] === 'undefined' && tries < 20) {
nextId++;
tries++;
}
return resizeCoords[nextId];
};
const prevCoord = id => {
let nextId = id - 1;
while (typeof resizeCoords[nextId] === 'undefined' && nextId >= 0) {
nextId--;
}
return resizeCoords[nextId];
};
if (isResize) {
let leftPos = e.clientX - parentOffsetLeft;
const handleMoveRightmostBoundary = (leftPos, tableWidth, fixedMinWidth) => {
if (leftPos > tableWidth - fixedMinWidth) {
return tableWidth - fixedMinWidth;
}
return leftPos;
};
const handleMoveLeftmostBoundary = (leftPos, fixedMinWidth) => {
if (leftPos < fixedMinWidth) {
return fixedMinWidth;
}
return leftPos;
};
const handleMoveRight = (leftPos, resizeCoords, id, fixedMinWidth) => {
if (typeof nextCoord(id) === 'undefined') return leftPos;
if (leftPos > nextCoord(id).left - fixedMinWidth) {
return nextCoord(id).left - fixedMinWidth;
}
return leftPos;
};
const handleMoveLeft = (leftPos, resizeCoords, id, fixedMinWidth) => {
if (typeof prevCoord(id) === 'undefined') return leftPos;
if (leftPos < prevCoord(id).left + fixedMinWidth) {
return prevCoord(id).left + fixedMinWidth;
}
return leftPos;
};
const isFirstColumn = (selectableRows, id) => {
let firstColumn = 1;
while (!resizeCoords[firstColumn] && firstColumn < 20) {
firstColumn++;
}
return (selectableRows !== 'none' && id === 0) || (selectableRows === 'none' && id === firstColumn);
};
const isLastColumn = (id, finalCells) => {
return id === prevCol(lastColumn);
};
if (isFirstColumn(selectableRows, idNumber) && isLastColumn(idNumber, finalCells)) {
leftPos = handleMoveLeftmostBoundary(leftPos, fixedMinWidth1);
leftPos = handleMoveRightmostBoundary(leftPos, tableWidth, fixedMinWidth2);
} else if (!isFirstColumn(selectableRows, idNumber) && isLastColumn(idNumber, finalCells)) {
leftPos = handleMoveRightmostBoundary(leftPos, tableWidth, fixedMinWidth2);
leftPos = handleMoveLeft(leftPos, resizeCoords, idNumber, fixedMinWidth1);
} else if (isFirstColumn(selectableRows, idNumber) && !isLastColumn(idNumber, finalCells)) {
leftPos = handleMoveLeftmostBoundary(leftPos, fixedMinWidth1);
leftPos = handleMoveRight(leftPos, resizeCoords, idNumber, fixedMinWidth2);
} else if (!isFirstColumn(selectableRows, idNumber) && !isLastColumn(idNumber, finalCells)) {
leftPos = handleMoveLeft(leftPos, resizeCoords, idNumber, fixedMinWidth1);
leftPos = handleMoveRight(leftPos, resizeCoords, idNumber, fixedMinWidth2);
}
const curCoord = { ...resizeCoords[id], left: leftPos };
const newResizeCoords = { ...resizeCoords, [id]: curCoord };
this.setState({ resizeCoords: newResizeCoords, tableHeight }, this.updateWidths);
}
};
onResizeEnd = (id, e) => {
this.setState({ isResize: false, id: null });
};
render() {
const { classes, tableId } = this.props;
const { id, isResize, resizeCoords, tableWidth, tableHeight } = this.state;
return (
<div className={classes.root} style={{ width: tableWidth }}>
{Object.entries(resizeCoords).map(([key, val]) => {
return (
<div
data-divider-index={key}
data-tableid={tableId}
aria-hidden="true"
key={key}
onMouseMove={this.onResizeMove.bind(null, key)}
onMouseUp={this.onResizeEnd.bind(null, key)}
style={{
width: isResize && id == key ? tableWidth : 'auto',
position: 'absolute',
height: tableHeight - 2,
cursor: 'ew-resize',
zIndex: 1000,
}}>
<div
aria-hidden="true"
onMouseDown={this.onResizeStart.bind(null, key)}
className={classes.resizer}
style={{ left: val.left }}
/>
</div>
);
})}
</div>
);
}
}
export default withStyles(TableResize, defaultResizeStyles, { name: 'MUIDataTableResize' });
|