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): 修复全选时 onChange 第 4 个参数返回 undefined 问题 (#2789) #2790

Merged
merged 1 commit into from
Apr 12, 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/curly-pans-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(table): 修复全选时 onChange 第 4 个参数返回 undefined 问题
5 changes: 5 additions & 0 deletions .changeset/lucky-plums-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/table": patch
---

fix: 修复全选时 onChange 第 4 个参数返回 undefined 问题
29 changes: 24 additions & 5 deletions packages/ui/table/src/hooks/use-check.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCheck } from '@hi-ui/use-check'
import React from 'react'
import React, { useEffect } from 'react'
import { useUncontrolledState } from '@hi-ui/use-uncontrolled-state'
import { FlattedTableRowData, TableRowSelection } from '../types'

Expand Down Expand Up @@ -30,6 +30,12 @@ export const useTableCheck = ({
rowSelection?.onChange
)

useEffect(() => {
checkedRowDataItemsRef.current = checkedRowDataItemsRef.current.filter(({ key }) =>
checkedRowKeys?.includes(key)
)
}, [checkedRowKeys])

// 已选中的行数据集合
const checkedRowDataItemsRef = React.useRef<Record<string, any>[]>([])
const checkedRowDataItems = checkedRowDataItemsRef.current
Expand All @@ -47,7 +53,9 @@ export const useTableCheck = ({
const onCheckedRowKeysChange = React.useCallback(
(rowItem: Record<string, any>, checked: boolean) => {
// 记录选中的行数据集合
const nextCheckedDataItems = checkedRowDataItems
const nextCheckedDataItems = checkedRowDataItems.filter(({ key }) =>
checkedRowKeys.includes(key)
)

if (checked) {
if (!nextCheckedDataItems.find((item) => item[fieldKey] === rowItem[fieldKey])) {
Expand All @@ -61,7 +69,7 @@ export const useTableCheck = ({

handleCheckedRowKeysChange(rowItem, checked)
},
[checkedRowDataItems, fieldKey, handleCheckedRowKeysChange]
[checkedRowDataItems, checkedRowKeys, fieldKey, handleCheckedRowKeysChange]
)

// 判断是否全选
Expand Down Expand Up @@ -101,23 +109,34 @@ export const useTableCheck = ({
const checkedRowKeysSet = new Set(checkedRowKeys)

if (checkedAll) {
checkedRowDataItemsRef.current = checkedRowDataItemsRef.current.filter(
({ key }) => !checkedRowKeysSet.has(key)
)

// 移除当前页所有行 ids
trySetCheckedRowKeys(
(prev) => prev.filter((id) => !checkedRowKeysSet.has(id)),
targetRowItems,
false
false,
checkedRowDataItemsRef.current
)

return
}

checkedRowDataItemsRef.current = targetRowItems.concat(
checkedRowDataItemsRef.current.filter((item) => !checkedRowKeysSet.has(item.key))
)

trySetCheckedRowKeys(
// 添加当前页所有行 ids
(prev) => {
prev.forEach((id) => checkedRowKeysSet.add(id))
return Array.from(checkedRowKeysSet)
},
targetRowItems,
true
true,
checkedRowDataItemsRef.current
)
}, [trySetCheckedRowKeys, flattedData, checkRowIsDisabledCheckbox, checkedAll])

Expand Down