MatrixTable.tsx
1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { FillTable, USelect } from '@uj/ui';
import type { PermissionMatrix, PermissionState, ResourceKey, RoleKey } from '@/access';
import { roleLabels } from '@/access';
import { resourceLabels, stateOptions } from '../tools/consts';
interface Props {
matrix: PermissionMatrix;
onChange: (res: ResourceKey, role: RoleKey, value: PermissionState) => void;
}
interface MatrixRow {
resource: ResourceKey;
}
export function MatrixTable({ matrix, onChange }: Props) {
const roles = Object.keys(roleLabels) as RoleKey[];
const resources = Object.keys(resourceLabels) as ResourceKey[];
const rows: MatrixRow[] = resources.map((r) => ({ resource: r }));
const columns = [
{
title: '资源',
dataIndex: 'resource',
key: 'resource',
render: (_: unknown, row: MatrixRow) => resourceLabels[row.resource],
},
...roles.map((rl) => ({
title: roleLabels[rl],
dataIndex: rl,
key: rl,
render: (_: unknown, row: MatrixRow) => (
<USelect
size="small"
style={{ width: 130 }}
value={matrix[row.resource][rl]}
options={stateOptions}
onChange={(v: PermissionState) => onChange(row.resource, rl, v)}
/>
),
})),
];
return (
<FillTable<MatrixRow>
rowKey={(r) => r.resource}
columns={columns as never}
dataSource={rows}
pagination={false}
/>
);
}