Skip to content

Commit 55219d5

Browse files
authoredMar 22, 2022
feat(Table): Table 支持树形数据显示和可展开同时出现 (#703)
1 parent 8706470 commit 55219d5

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed
 

‎packages/react-table/README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,6 @@ ReactDOM.render(<Demo />, _mount_);
729729

730730
可以通过设置 indentSize 以控制每一层的缩进宽度
731731

732-
> ⚠️ 注意: 树形数据展示和`expandable.expandedRowRender`请不要同时出现,后续或将支持
733-
<!--rehype:style=border-left: 8px solid #ffe564;background-color: #ffe56440;padding: 12px 16px;-->
734-
735732
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
736733
```jsx
737734
import ReactDOM from 'react-dom';
@@ -805,7 +802,7 @@ const Demo = () => {
805802
<Table
806803
rowKey="id"
807804
columns={columns}
808-
data={dataSource}
805+
data={dataSource}
809806
/>
810807
</div>
811808
)
@@ -968,7 +965,8 @@ ReactDOM.render(<Demo />, _mount_);
968965

969966
### expandable
970967

971-
注意 expandedRowKeys 与 onExpandedRowsChange 必须成对出现
968+
> ⚠️ 注意: expandedRowKeys 与 onExpandedRowsChange 必须同时出现或不出现
969+
<!--rehype:style=border-left: 8px solid #ffe564;background-color: #ffe56440;padding: 12px 16px;-->
972970
973971
| 参数 | 说明 | 类型 | 默认值 |
974972
|--------- |-------- |--------- |-------- |

‎packages/react-table/src/TableTr.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
3838
locationWidth,
3939
} = props;
4040
const [isOpacity, setIsOpacity] = useState(false);
41+
const [childrenIndex, setChildrenIndex] = useState(0);
4142
const [expandIndex, setExpandIndex] = useState<Array<T[keyof T] | number>>([]);
4243
useEffect(() => {
4344
setIsOpacity(!!data?.find((it) => it[childrenColumnName]));
45+
setChildrenIndex(keys?.findIndex((it) => it.key === 'uiw-expanded') === -1 ? 0 : 1);
4446
}, [data]);
4547

4648
const IconDom = useMemo(() => {
@@ -92,7 +94,7 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
9294
}
9395
}
9496
const isHasChildren = Array.isArray(trData[childrenColumnName]);
95-
if (colNum === 0 && (isOpacity || hierarchy || isHasChildren)) {
97+
if (colNum === childrenIndex && (isOpacity || hierarchy || isHasChildren)) {
9698
objs.children = (
9799
<>
98100
{IconDom(key, isHasChildren)}
@@ -126,10 +128,10 @@ export default function TableTr<T extends { [key: string]: any }>(props: TableTr
126128
);
127129
})}
128130
</tr>
131+
{isExpandedDom(trData, rowNum)}
129132
{expandIndex.includes(key) && (
130133
<TableTr {...props} data={trData[childrenColumnName]} hierarchy={hierarchy + 1} />
131134
)}
132-
{isExpandedDom(trData, rowNum)}
133135
</React.Fragment>
134136
);
135137
})}

‎packages/react-table/src/index.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,20 @@ export default function Table<T extends { [key: string]: V }, V>(props: TablePro
131131
return finalLocationWidth.current;
132132
};
133133
useEffect(() => {
134+
const childKey = expandable?.childrenColumnName || 'children';
135+
const deep = (params: TableColumns<T>) => {
136+
const arr1: Array<T[keyof T] | number> = [];
137+
const arr = params.map((it: T, index: number) => {
138+
if (Array.isArray(it[childKey])) {
139+
arr1.push(...deep(it[childKey]));
140+
}
141+
return rowKey ? it[rowKey] : index;
142+
});
143+
return [...arr1, ...arr];
144+
};
134145
if (expandable) {
135146
if (expandable.defaultExpandAllRows) {
136-
setExpandIndex(data.map((it, index) => (rowKey ? it[rowKey] : index)));
147+
setExpandIndex(deep(data));
137148
return;
138149
}
139150
if (expandable.defaultExpandedRowKeys) {

0 commit comments

Comments
 (0)
Please sign in to comment.