Skip to content

Commit 080ce82

Browse files
author
Domino987
committed
fix: hidden columns resizing now works for correct col
1 parent febedb3 commit 080ce82

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

src/components/MTableHeader/index.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import { Tooltip } from '@material-ui/core';
1010
import { withStyles } from '@material-ui/core/styles';
1111
import * as CommonValues from '../../utils/common-values';
1212

13-
export function MTableHeader({ onColumnResized, ...props }) {
13+
export function MTableHeader({ onColumnResized, columns, ...props }) {
1414
const defaultMinColumnWidth = 20;
1515
const defaultMaxColumnWidth = 10000;
1616

1717
const [resizing, setResizing] = React.useState(undefined);
1818
const [lastX, setLastX] = React.useState(0);
19+
const displayingColumns = React.useMemo(
20+
() => columns.filter((c) => c.hidden !== true),
21+
[columns]
22+
);
1923

2024
const handleMouseDown = (e, columnDef, colIndex) => {
2125
const startX = e.clientX;
@@ -30,7 +34,7 @@ export function MTableHeader({ onColumnResized, ...props }) {
3034
nextWidth =
3135
nextTh &&
3236
Math.round(+window.getComputedStyle(nextTh).width.slice(0, -2));
33-
nextColIndex = props.columns.findIndex(
37+
nextColIndex = displayingColumns.findIndex(
3438
(c) => c.tableData.id === columnDef.tableData.id + 1
3539
);
3640
} else if (!initialColWidths) {
@@ -45,9 +49,9 @@ export function MTableHeader({ onColumnResized, ...props }) {
4549
colIndex,
4650
nextColIndex,
4751
lastColData: { ...columnDef.tableData, width: currentWidth },
48-
...(nextColIndex && {
52+
...(nextColIndex !== -1 && {
4953
lastNextColData: {
50-
...props.columns[nextColIndex].tableData,
54+
...displayingColumns[nextColIndex].tableData,
5155
width: nextWidth
5256
}
5357
}),
@@ -80,7 +84,7 @@ export function MTableHeader({ onColumnResized, ...props }) {
8084
}
8185

8286
const curX = e.clientX;
83-
const col = props.columns[resizing.colIndex];
87+
const col = displayingColumns[resizing.colIndex];
8488
const alreadyOffset =
8589
col.tableData.additionalWidth - resizing.lastColData.additionalWidth;
8690
let offset = constrainedColumnResize(
@@ -90,9 +94,9 @@ export function MTableHeader({ onColumnResized, ...props }) {
9094
);
9195
offset = Math.round(offset);
9296
const widths = [resizing.lastColData.width + alreadyOffset];
93-
if (props.tableWidth === 'full') {
97+
if (props.tableWidth === 'full' && resizing.lastNextColData) {
9498
offset = -constrainedColumnResize(
95-
props.columns[resizing.nextColIndex],
99+
displayingColumns[resizing.nextColIndex],
96100
resizing.lastNextColData.width - alreadyOffset,
97101
-offset
98102
);
@@ -116,7 +120,7 @@ export function MTableHeader({ onColumnResized, ...props }) {
116120
(e) => {
117121
if (resizing && lastX !== resizing.startX) {
118122
onColumnResized(
119-
props.columns[resizing.colIndex].tableData.id,
123+
displayingColumns[resizing.colIndex].tableData.id,
120124
0,
121125
[],
122126
[]
@@ -193,11 +197,8 @@ export function MTableHeader({ onColumnResized, ...props }) {
193197
function RenderHeader() {
194198
const size = props.options.padding === 'default' ? 'medium' : 'small';
195199

196-
return props.columns
197-
.filter(
198-
(columnDef) =>
199-
!columnDef.hidden && !(columnDef.tableData.groupOrder > -1)
200-
)
200+
return displayingColumns
201+
.filter((columnDef) => !(columnDef.tableData.groupOrder > -1))
201202
.sort((a, b) => a.tableData.columnOrder - b.tableData.columnOrder)
202203
.map((columnDef, index, allCols) => {
203204
let content = columnDef.title;
@@ -396,7 +397,7 @@ export function MTableHeader({ onColumnResized, ...props }) {
396397
/>
397398
);
398399
}
399-
props.columns
400+
displayingColumns
400401
.filter((columnDef) => columnDef.tableData.groupOrder > -1)
401402
.forEach((columnDef) => {
402403
headers.splice(

0 commit comments

Comments
 (0)