forked from react-component/table
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Change BodyRow nest to flatten strcutrue (react-component#619)
* 扁平化rows, 新增NewBodyRow, 以支持树形结构的虚拟滚动. * fix: 处理eslint警告. * fix: 单元测试报错的问题. * fix: 拉取最新代码, 处理相应的单元测试. * adjust flatColumns & add virtual-tree-table example * add virtual-tree-table demo * update virtuallist-antd version to display demo better * refactor: extract useFlattenRecords hook. * remove virtuallist-antd dependence * rename NewBodyRow to BodyRow * optimize useFlattenRecords * flat useFlattenRecords options. * optimized useFlattenRecords: if expandedKeys.size empty, do not run recursion
- Loading branch information
1 parent
f76ae82
commit e0bab69
Showing
4 changed files
with
137 additions
and
54 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
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,81 @@ | ||
import type { GetRowKey, Key } from '@/interface'; | ||
import * as React from 'react'; | ||
|
||
// recursion (flat tree structure) | ||
function flatRecord<T>( | ||
record: T, | ||
indent: number, | ||
childrenColumnName: string, | ||
expandedKeys: Set<Key>, | ||
getRowKey: GetRowKey<T>, | ||
) { | ||
const arr = []; | ||
|
||
arr.push({ | ||
record, | ||
indent, | ||
}); | ||
|
||
const key = getRowKey(record); | ||
|
||
const expanded = expandedKeys?.has(key); | ||
|
||
if (record && Array.isArray(record[childrenColumnName]) && expanded) { | ||
// expanded state, flat record | ||
for (let i = 0; i < record[childrenColumnName].length; i += 1) { | ||
const tempArr = flatRecord( | ||
record[childrenColumnName][i], | ||
indent + 1, | ||
childrenColumnName, | ||
expandedKeys, | ||
getRowKey, | ||
); | ||
|
||
arr.push(...tempArr); | ||
} | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
/** | ||
* flat tree data on expanded state | ||
* | ||
* @export | ||
* @template T | ||
* @param {*} data : table data | ||
* @param {string} childrenColumnName : 指定树形结构的列名 | ||
* @param {Set<Key>} expandedKeys : 展开的行对应的keys | ||
* @param {GetRowKey<T>} getRowKey : 获取当前rowKey的方法 | ||
* @returns flattened data | ||
*/ | ||
export default function useFlattenRecords<T>( | ||
data, | ||
childrenColumnName: string, | ||
expandedKeys: Set<Key>, | ||
getRowKey: GetRowKey<T>, | ||
) { | ||
const arr: { record: T; indent: number }[] = React.useMemo(() => { | ||
if (expandedKeys?.size) { | ||
const temp: { record: T; indent: number }[] = []; | ||
|
||
// collect flattened record | ||
for (let i = 0; i < data?.length; i += 1) { | ||
const record = data[i]; | ||
|
||
temp.push(...flatRecord<T>(record, 0, childrenColumnName, expandedKeys, getRowKey)); | ||
} | ||
|
||
return temp; | ||
} | ||
|
||
return data?.map(item => { | ||
return { | ||
record: item, | ||
indent: 0, | ||
}; | ||
}); | ||
}, [data, childrenColumnName, expandedKeys, getRowKey]); | ||
|
||
return arr; | ||
} |