-
-
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): support dataSource disabled props in SelectTable (#2902
- Loading branch information
Showing
6 changed files
with
121 additions
and
27 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { getTreeKeys, hasSelectedKey, completedKeys } from './utils' | ||
|
||
/** | ||
* 判断该字段的 indeterminate 属性 | ||
* @param record 当前字段 | ||
* @param selected 已选中的字段值集合 | ||
* @param primaryKey 键名称 | ||
* @returns indeterminate 属性值 | ||
*/ | ||
const getIndeterminate = (record: any, selected: any[], primaryKey: string) => { | ||
if (selected?.includes(record[primaryKey])) { | ||
return undefined | ||
} | ||
return hasSelectedKey(record.children, selected, primaryKey) || undefined | ||
} | ||
|
||
interface ICheckSlackly { | ||
( | ||
currentSelected: any[], | ||
allSelected: any[], | ||
primaryKey: string, | ||
flatDataSource: any[] | ||
): { | ||
selectedRowKeys: any[] | ||
records: any[] | ||
} | ||
} | ||
|
||
const useCheckSlackly: ICheckSlackly = ( | ||
currentSelected, // onChange 返回的 keys | ||
allSelected, // Table UI 展示的 keys | ||
primaryKey, | ||
flatDataSource | ||
) => { | ||
const isSelected = currentSelected.length > allSelected.length // 判断是选中还是取消 | ||
const currentKey = [...currentSelected, ...allSelected].find( | ||
(key) => !(currentSelected.includes(key) && allSelected.includes(key)) // 当前变化key不同时存在于两个selected | ||
) | ||
const currentRecords = flatDataSource.find( | ||
(item) => item[primaryKey] === currentKey | ||
) | ||
const currentTreeKeys = getTreeKeys([currentRecords], primaryKey) | ||
let newSelectedRowKeys = [] | ||
if (isSelected) { | ||
// 选中当前key及其子keys | ||
newSelectedRowKeys = [...new Set([...allSelected, ...currentTreeKeys])] | ||
} else { | ||
// 移除当前key及其子keys | ||
newSelectedRowKeys = allSelected.filter( | ||
(key) => !currentTreeKeys.includes(key) | ||
) | ||
} | ||
|
||
newSelectedRowKeys = completedKeys( | ||
flatDataSource, | ||
newSelectedRowKeys, | ||
primaryKey | ||
) | ||
|
||
return { | ||
selectedRowKeys: newSelectedRowKeys, | ||
records: flatDataSource.filter((item) => | ||
newSelectedRowKeys.includes(item[primaryKey]) | ||
), | ||
} | ||
} | ||
|
||
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
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
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