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): 修复表头分组超过 2 列时冻结列交互异常问题 (#2744) #2748

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/nervous-worms-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(table): 修复表头分组超过 2 列时冻结列交互异常问题
6 changes: 6 additions & 0 deletions .changeset/stale-mugs-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hi-ui/table": patch
"@hi-ui/tree-utils": patch
---

fix: 修复表头分组超过 2 列时冻结列交互异常问题
24 changes: 22 additions & 2 deletions packages/ui/table/src/use-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getNodeRootParent,
flattedTreeSort,
// getNodeRootParent,
findNodeById,
} from '@hi-ui/tree-utils'
import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state'
import { isArrayNonEmpty, isNullish } from '@hi-ui/type-assertion'
Expand Down Expand Up @@ -210,7 +211,9 @@ export const useTable = ({
onFixedToColumn
)

const leftFreezeColumn = columns.some((item) => item.dataKey === fixedToColumn.left)
const leftFreezeColumn = findNodeById(columns, fixedToColumn.left || '', {
idFieldName: 'dataKey',
})
? fixedToColumn.left
: SELECTION_DATA_KEY

Expand Down Expand Up @@ -282,9 +285,26 @@ export const useTable = ({
if (colWidths) {
// colWidths 记录的是最新的列宽,当它有值时,重置一下列宽,否则会导致冻结列动态调整宽度后定位不准
nextColumns = nextColumns.map((item, index) => {
const { width, children } = item
let parentWidth = 0

// 计算父节点宽度
const dig = (children?: FlattedTableColumnItemData[], index = 0) => {
children?.forEach((child, childIndex) => {
if (child.children) {
dig(child.children, childIndex)
} else {
parentWidth += colWidths[index + childIndex]
}
})
}

dig(children, index)

return {
...item,
width: colWidths[index],
// 如果是表头分组中的父节点则 width 设置为所有子节点的宽度总和,否则直接拿 colWidths 中的宽度
width: children ? parentWidth || width : colWidths[index],
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion packages/ui/table/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ export const parseFixedColumns = (
columns,
children,
key,
index === 0 ? 0 : _parentStickyWidth
idx === 0 ? _parentStickyWidth : 0
)

item.children[idx] = child
children[idx] = child
columns[child.index] = child
})
}

return item
}

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/tree-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export const findNodeById = <T extends BaseTreeNode>(
}

if (node[childrenFieldName]) {
const foundNode = findNodeById(node[childrenFieldName], targetId)
const foundNode = findNodeById(node[childrenFieldName], targetId, options)

if (foundNode) {
return foundNode as T
Expand Down