feat: add expand all rows

This commit is contained in:
jialin
2025-03-05 14:05:12 +08:00
parent 3fdde5f8c4
commit fd7ce9027a
12 changed files with 92 additions and 33 deletions
@@ -1,4 +1,4 @@
import { RightOutlined } from '@ant-design/icons';
import { DownOutlined, RightOutlined } from '@ant-design/icons';
import { Button, Checkbox } from 'antd';
import _ from 'lodash';
import React from 'react';
@@ -7,6 +7,8 @@ interface HeaderPrefixProps {
expandable?: boolean | React.ReactNode;
enableSelection?: boolean;
onSelectAll?: (e: any) => void;
onExpandAll?: (e: any) => void;
expandAll?: boolean;
indeterminate?: boolean;
selectAll?: boolean;
hasColumns?: boolean;
@@ -18,16 +20,31 @@ const HeaderPrefix: React.FC<HeaderPrefixProps> = (props) => {
expandable,
enableSelection,
onSelectAll,
onExpandAll,
indeterminate,
selectAll
selectAll,
expandAll
} = props;
const handleToggleExpand = () => {
onExpandAll?.(!expandAll);
};
if (!hasColumns) {
return null;
}
if (expandable && enableSelection) {
return (
<div className="header-row-prefix-wrapper">
<span style={{ marginRight: 5, padding: '0 14px' }}></span>
<div className="header-row-prefix-wrapper flex-center">
<span style={{ marginRight: 5 }}>
{_.isBoolean(expandable) ? (
<Button type="text" size="small" onClick={handleToggleExpand}>
{expandAll ? <DownOutlined /> : <RightOutlined />}
</Button>
) : (
expandable
)}
</span>
<Checkbox
onChange={onSelectAll}
indeterminate={indeterminate}
+13
View File
@@ -16,6 +16,7 @@ const SealTable: React.FC<SealTableProps & { pagination: PaginationProps }> = (
rowKey,
childParentKey,
onExpand,
onExpandAll,
onSort,
onCell,
expandedRowKeys,
@@ -51,6 +52,12 @@ const SealTable: React.FC<SealTableProps & { pagination: PaginationProps }> = (
});
}, [columns, children]);
const expandAll = useMemo(() => {
const allKeys = new Set(expandedRowKeys);
const currentDataKeys = props.dataSource.map((record) => record[rowKey]);
return currentDataKeys.every((key) => allKeys.has(key));
}, [props.dataSource, expandedRowKeys]);
const selectState = useMemo(() => {
const selectedRowKeys = rowSelection?.selectedRowKeys || [];
const selectedKeys = new Set(selectedRowKeys);
@@ -99,6 +106,10 @@ const SealTable: React.FC<SealTableProps & { pagination: PaginationProps }> = (
}
};
const handleExpandAll = (value: boolean) => {
onExpandAll?.(value);
};
const handlePageChange = (page: number, pageSize: number) => {
pagination?.onChange?.(page, pageSize);
};
@@ -115,6 +126,8 @@ const SealTable: React.FC<SealTableProps & { pagination: PaginationProps }> = (
selectAll={selectState.selectAll}
indeterminate={selectState.indeterminate}
onSelectAll={handleSelectAllChange}
onExpandAll={handleExpandAll}
expandAll={expandAll}
expandable={expandable}
enableSelection={rowSelection?.enableSelection}
hasColumns={parsedColumns.length > 0}
+1
View File
@@ -58,6 +58,7 @@ export interface SealTableProps {
onCell?: (record: any, dataIndex: string) => void;
onSort?: (dataIndex: string, order: 'ascend' | 'descend') => void;
onExpand?: (expanded: boolean, record: any, rowKey: any) => void;
onExpandAll?: (expanded: boolean) => void;
renderChildren?: (data: any, parent?: any) => React.ReactNode;
loadChildren?: (record: any, options?: any) => Promise<any[]>;
loadChildrenAPI?: (record: any) => string;