MatrixTable.tsx 1.38 KB
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}
    />
  );
}