-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(next/antd): fix selected bug3 by search in SelectTable (#2927)
* fix(next): fix selected bug2 by search in SelectTable * feat(next/antd): fix selected bug3 by search in SelectTable
- Loading branch information
Showing
6 changed files
with
198 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,91 @@ | ||
import { getTreeKeys, hasSelectedKey, completedKeys } from './utils' | ||
import { | ||
getTreeKeys, | ||
hasSelectedKey, | ||
completedKeys, | ||
getCompatibleAllSelected, | ||
} from './utils' | ||
|
||
/** | ||
* 判断该字段的 indeterminate 属性 | ||
* @param record 当前字段 | ||
* @param flatDataSource 完整平铺数据 | ||
* @param selected 已选中的字段值集合 | ||
* @param primaryKey 键名称 | ||
* @returns indeterminate 属性值 | ||
*/ | ||
const getIndeterminate = (record: any, selected: any[], primaryKey: string) => { | ||
const getIndeterminate = ( | ||
record: any, | ||
flatDataSource: any, | ||
selected: any[], | ||
primaryKey: string | ||
) => { | ||
if (selected?.includes(record[primaryKey])) { | ||
return undefined | ||
} | ||
return hasSelectedKey(record.children, selected, primaryKey) || undefined | ||
const wholeRecord = flatDataSource.find( | ||
(item) => item[primaryKey] === record[primaryKey] | ||
) | ||
return hasSelectedKey(wholeRecord.children, selected, primaryKey) || undefined | ||
} | ||
|
||
interface ICheckSlackly { | ||
( | ||
currentSelected: any[], | ||
allSelected: any[], | ||
selected: any[], | ||
flatDataSource: any[], | ||
flatFilteredDataSource: any[], | ||
primaryKey: string, | ||
flatDataSource: any[] | ||
): { | ||
selectedRowKeys: any[] | ||
records: any[] | ||
} | ||
checkStrictly: boolean | ||
): { selectedRowKeys: any[] } | ||
} | ||
|
||
// 父子节点(节点状态按全完整数据计算,节点操作按筛选数据计算) | ||
const useCheckSlackly: ICheckSlackly = ( | ||
currentSelected, // onChange 返回的 keys | ||
allSelected, // Table UI 展示的 keys | ||
selected, // Table UI 展示的 keys | ||
flatDataSource, | ||
flatFilteredDataSource, | ||
primaryKey, | ||
flatDataSource | ||
checkStrictly | ||
) => { | ||
const isSelected = currentSelected.length > allSelected.length // 判断是选中还是取消 | ||
const currentKey = [...currentSelected, ...allSelected].find( | ||
(key) => !(currentSelected.includes(key) && allSelected.includes(key)) // 当前变化key不同时存在于两个selected | ||
let isSelected = currentSelected.length > selected.length // 判断是选中还是取消 | ||
|
||
const currentKey = [...currentSelected, ...selected].find( | ||
(key) => !(currentSelected.includes(key) && selected.includes(key)) // 当前变化key不同时存在于两个selected | ||
) | ||
const currentRecords = flatDataSource.find( | ||
// 从过滤后的数据中获取当前record | ||
const currentRecord = flatFilteredDataSource.find( | ||
(item) => item[primaryKey] === currentKey | ||
) | ||
const currentTreeKeys = getTreeKeys([currentRecords], primaryKey) | ||
let newSelectedRowKeys = [] | ||
const currentTreeKeys = getTreeKeys(currentRecord.children, primaryKey) | ||
|
||
// 在筛选状态下(按钮的indeterminate状态处于异常)需要通过数据对比判断是否处于全选中状态 | ||
if ( | ||
getCompatibleAllSelected( | ||
selected, | ||
currentRecord.children, | ||
currentTreeKeys, | ||
checkStrictly, | ||
primaryKey | ||
) | ||
) { | ||
isSelected = false | ||
} | ||
|
||
let newSelected = [] | ||
if (isSelected) { | ||
// 选中当前key及其子keys | ||
newSelectedRowKeys = [...new Set([...allSelected, ...currentTreeKeys])] | ||
newSelected = [...new Set([...selected, currentKey, ...currentTreeKeys])] | ||
} else { | ||
// 移除当前key及其子keys | ||
newSelectedRowKeys = allSelected.filter( | ||
(key) => !currentTreeKeys.includes(key) | ||
newSelected = selected.filter( | ||
(key) => ![currentKey, ...currentTreeKeys].includes(key) | ||
) | ||
} | ||
|
||
newSelectedRowKeys = completedKeys( | ||
flatDataSource, | ||
newSelectedRowKeys, | ||
primaryKey | ||
) | ||
newSelected = completedKeys(flatDataSource, newSelected, primaryKey) | ||
|
||
return { | ||
selectedRowKeys: newSelectedRowKeys, | ||
records: flatDataSource.filter((item) => | ||
newSelectedRowKeys.includes(item[primaryKey]) | ||
), | ||
} | ||
return { selectedRowKeys: newSelected } | ||
} | ||
|
||
export { useCheckSlackly, getIndeterminate } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.