Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Table): fix only one column can be resized #2959

Merged
merged 17 commits into from
Jun 27, 2024
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/table/hooks/useColumnResize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export default function useColumnResize(params: {
e: React.MouseEvent<HTMLTableHeaderCellElement, MouseEvent>,
col: BaseTableCol<TableRowData>,
) => {
// 当前列是否可以拖拽宽度,因为外层有判断props.resizable,所以这里只需要确定col.resizable
const colResizable = col.resizable ?? true;
// calculate mouse cursor before drag start
if (!resizeLineRef.current || resizeLineParams.isDragging || !e.target) return;
const target = (e.target as HTMLElement).closest('th');
Expand All @@ -124,7 +126,6 @@ export default function useColumnResize(params: {
const thLeftCursor = e.pageX - targetBoundRect.left <= distance;
const isFixedToRight = isColRightFixActive(col);
if (thRightCursor || isFixedToRight) {
const colResizable = col.resizable ?? true;
if (colResizable) {
target.style.cursor = thRightCursor || (isFixedToRight && thLeftCursor) ? 'col-resize' : '';
const isCurrent = (thRightCursor && !isFixedToRight) || (isFixedToRight && thLeftCursor);
Expand All @@ -134,15 +135,11 @@ export default function useColumnResize(params: {
}
} else if (thLeftCursor) {
const prevEl = target.previousElementSibling;
if (prevEl) {
const effectPrevCol = effectColMap.current[col.colKey]?.prev;
const colResizable = effectPrevCol?.resizable ?? true;
uyarn marked this conversation as resolved.
Show resolved Hide resolved
if (colResizable) {
target.style.cursor = 'col-resize';
resizeLineParams.draggingCol = prevEl as HTMLElement;
resizeLineParams.effectCol = 'prev';
return;
}
if (prevEl && colResizable) {
target.style.cursor = 'col-resize';
resizeLineParams.draggingCol = prevEl as HTMLElement;
resizeLineParams.effectCol = 'prev';
return;
}
}
// 重置记录值
Expand Down Expand Up @@ -265,14 +262,20 @@ export default function useColumnResize(params: {
const tmpCurrentCol = col.resizable !== false ? col : currentSibling;
// 是否允许调整相邻列宽:列宽未超出时,且并非是最后一列(最后一列的右侧拉伸会认为是表格整体宽度调整)
const canResizeSiblingColWidth = !(isWidthOverflow || index === leafColumns.length - 1);
if (resizeLineParams.effectCol === 'next') {

if (!effectNextCol?.colKey) {
// 已经不存在最后一列,比如整个表格只有一列可以调整的场景,需要直接影响到表格本身的宽度
if (resizeLineParams.effectCol) newThWidthList[tmpCurrentCol?.colKey] -= moveDistance;
else newThWidthList[tmpCurrentCol?.colKey] += moveDistance;
newThWidthList.tableWidth = getTotalTableWidth(newThWidthList);
} else if (resizeLineParams.effectCol === 'next') {
// 右侧激活态的固定列,需特殊调整
if (isColRightFixActive(col)) {
// 如果不相同,则表示改变相临的右侧列宽
if (target.dataset.colkey !== col.colKey) {
newThWidthList[effectNextCol.colKey] += moveDistance;
} else {
newThWidthList[tmpCurrentCol.colKey] += moveDistance;
newThWidthList[tmpCurrentCol?.colKey] += moveDistance;
}
} else {
// 非右侧激活态的固定列
Expand Down
Loading