122 lines
4.3 KiB
TypeScript
122 lines
4.3 KiB
TypeScript
import React, { useEffect, useMemo, useState } from 'react';
|
||||
|
|
import { Table, Space, Popconfirm, Tag } from 'antd';
|
|||
|
|
import type { ColumnsType } from 'antd/es/table';
|
|||
|
|
import type { TenantTreeNode } from '@/services/tenant';
|
|||
|
|
|
|||
|
|
export interface TenantTreeTableProps {
|
|||
|
|
data: TenantTreeNode[];
|
|||
|
|
loading?: boolean;
|
|||
|
|
onAddChild?: (record: TenantTreeNode) => void;
|
|||
|
|
onEdit?: (record: TenantTreeNode) => void;
|
|||
|
|
onDelete?: (record: TenantTreeNode) => void;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const TenantTreeTable: React.FC<TenantTreeTableProps> = ({ data, loading, onAddChild, onEdit, onDelete }) => {
|
|||
|
|
const normalize = (nodes?: any[]): any[] => {
|
|||
|
|
if (!Array.isArray(nodes)) return [];
|
|||
|
|
return nodes.map((n: any) => ({
|
|||
|
|
...n,
|
|||
|
|
children: Array.isArray(n?.children) && n.children.length ? normalize(n.children) : undefined,
|
|||
|
|
}));
|
|||
|
|
};
|
|||
|
|
const safeData = useMemo(() => normalize(data as any), [data]);
|
|||
|
|
|
|||
|
|
// 计算并控制展开行,保证“默认展开”在异步数据加载后仍生效
|
|||
|
|
const allExpandableKeys = useMemo(() => {
|
|||
|
|
const keys: React.Key[] = [];
|
|||
|
|
const walk = (arr?: any[]) => {
|
|||
|
|
if (!Array.isArray(arr)) return;
|
|||
|
|
for (const n of arr) {
|
|||
|
|
const hasChildren = Array.isArray(n?.children) && n.children.length > 0;
|
|||
|
|
if (hasChildren) keys.push(n.tenantId);
|
|||
|
|
if (hasChildren) walk(n.children);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
walk(safeData);
|
|||
|
|
return keys;
|
|||
|
|
}, [safeData]);
|
|||
|
|
|
|||
|
|
const [expandedRowKeys, setExpandedRowKeys] = useState<React.Key[]>([]);
|
|||
|
|
useEffect(() => {
|
|||
|
|
// 仅当列表 keys 实际发生变化时才更新,避免无意义的重复 setState
|
|||
|
|
setExpandedRowKeys((prev) => {
|
|||
|
|
const a = allExpandableKeys;
|
|||
|
|
if (prev.length === a.length && prev.every((v, i) => v === a[i])) return prev;
|
|||
|
|
return a;
|
|||
|
|
});
|
|||
|
|
}, [allExpandableKeys]);
|
|||
|
|
|
|||
|
|
const columns: ColumnsType<TenantTreeNode> = [
|
|||
|
|
{
|
|||
|
|
title: '租户名称',
|
|||
|
|
dataIndex: 'tenantName',
|
|||
|
|
render: (_: any, record: TenantTreeNode) => <span>{record.tenantName}</span>,
|
|||
|
|
},
|
|||
|
|
{ title: '租户ID', dataIndex: 'tenantId', width: 180, ellipsis: true },
|
|||
|
|
{
|
|||
|
|
title: '类型', dataIndex: 'tenantType', width: 120,
|
|||
|
|
render: (v: any) => {
|
|||
|
|
const map: Record<string, { color: string; text: string }> = {
|
|||
|
|
PLATFORM: { color: 'purple', text: '平台' },
|
|||
|
|
GROUP: { color: 'cyan', text: '集团' },
|
|||
|
|
COMPANY: { color: 'blue', text: '公司' },
|
|||
|
|
DEPARTMENT: { color: 'geekblue', text: '部门' },
|
|||
|
|
};
|
|||
|
|
const m = map[String(v)] || { color: 'default', text: String(v || '-') } as any;
|
|||
|
|
return <Tag color={m.color}>{m.text}</Tag>;
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
title: '状态', dataIndex: 'status', width: 100,
|
|||
|
|
render: (v: any, record: TenantTreeNode) => {
|
|||
|
|
const enabled = typeof v === 'boolean' ? v : (v === 1 || v === '1' || (record as any)?.enabled === true || v == null);
|
|||
|
|
return enabled ? <Tag color='green'>启用</Tag> : <Tag color='red'>停用</Tag>;
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
{ title: '联系人', dataIndex: 'contactPerson', width: 120 },
|
|||
|
|
{ title: '联系电话', dataIndex: 'contactPhone', width: 140 },
|
|||
|
|
{
|
|||
|
|
title: '操作', key: 'action', width: 220,
|
|||
|
|
render: (_: any, rec: TenantTreeNode | undefined) => {
|
|||
|
|
const record = (rec || ({} as TenantTreeNode));
|
|||
|
|
const hasId = !!(record as any)?.tenantId;
|
|||
|
|
return (
|
|||
|
|
<Space>
|
|||
|
|
<a onClick={() => hasId && onEdit?.(record)}>编辑</a>
|
|||
|
|
{onAddChild ? <a onClick={() => hasId && onAddChild?.(record)}>新增下级</a> : null}
|
|||
|
|
<Popconfirm title={`确认删除租户[${record?.tenantName ?? ''}]?`} onConfirm={() => hasId && onDelete?.(record)}>
|
|||
|
|
<a style={{ color: '#1677ff' }}>删除</a>
|
|||
|
|
</Popconfirm>
|
|||
|
|
</Space>
|
|||
|
|
);
|
|||
|
|
},
|
|||
|
|
},
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<Table
|
|||
|
|
rowKey='tenantId'
|
|||
|
|
columns={columns}
|
|||
|
|
dataSource={safeData as any}
|
|||
|
|
loading={loading}
|
|||
|
|
pagination={false}
|
|||
|
|
expandable={{
|
|||
|
|
expandedRowKeys,
|
|||
|
|
onExpand: (expanded, record: any) => {
|
|||
|
|
setExpandedRowKeys((prev) => {
|
|||
|
|
const key = record?.tenantId;
|
|||
|
|
if (key == null) return prev;
|
|||
|
|
const set = new Set(prev);
|
|||
|
|
if (expanded) set.add(key);
|
|||
|
|
else set.delete(key);
|
|||
|
|
return Array.from(set);
|
|||
|
|
});
|
|||
|
|
},
|
|||
|
|
}}
|
|||
|
|
size='middle'
|
|||
|
|
/>
|
|||
|
|
);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export default TenantTreeTable;
|