InvoiceDetailTable.tsx 3.32 KB
import type { ProColumns } from '@ant-design/pro-components';
import {
  EditableProTable,
  ProCard,
  ProFormField,
} from '@ant-design/pro-components';
import { Button } from 'antd';
import { useEffect, useState } from 'react';

export default ({ details, updateDetails, readOnly }) => {
  const [editableKeys, setEditableRowKeys] = useState([]);

  useEffect(() => {
    updateDetails(details);
  }, []);

  useEffect(() => {
    setEditableRowKeys(details?.map((item) => item.tid));
  }, [details]);
  const columns: ProColumns[] = [
    {
      title: '项目名称',
      readonly: readOnly,
      dataIndex: 'projectName',
      valueType: 'text',
      width: '30%',
    },
    {
      title: '规格型号',
      readonly: readOnly,
      dataIndex: 'specification',
      valueType: 'text',
      width: '30%',
    },
    {
      title: '单位',
      readonly: readOnly,
      dataIndex: 'unit',
      valueType: 'text',
      width: '30%',
    },
    {
      title: '数量',
      readonly: readOnly,
      dataIndex: 'quantity',
      valueType: 'digit',
      width: '30%',
    },
    {
      title: '单价',
      readonly: readOnly,
      dataIndex: 'price',
      valueType: 'digit',
      width: '30%',
    },
    {
      title: '金额',
      readonly: readOnly,
      dataIndex: 'taxPrice',
      valueType: 'digit',
      width: '30%',
    },
    {
      title: '税率/征收率',
      readonly: readOnly,
      dataIndex: 'taxRate',
      valueType: () => ({
        type: 'percent',
      }),
      width: '30%',
    },
    {
      title: '税额',
      readonly: readOnly,
      dataIndex: 'taxPrice',
      valueType: 'digit',
      width: '30%',
    },
    {
      title: '操作',
      valueType: 'option',
      width: 250,
      render: () => {
        return null;
      },
    },
  ];

  return (
    <>
      <EditableProTable
        headerTitle="可编辑表格"
        columns={columns}
        rowKey="tid"
        scroll={{
          x: 960,
        }}
        value={details}
        onChange={updateDetails}
        recordCreatorProps={
          readOnly
            ? false
            : {
                newRecordType: 'dataSource',
                record: () => ({
                  tid: Date.now(),
                }),
              }
        }
        toolBarRender={() => {
          return [
            <Button
              type="primary"
              key="save"
              onClick={() => {
                // dataSource 就是当前数据,可以调用 api 将其保存
                console.log(details);
              }}
            >
              保存数据
            </Button>,
          ];
        }}
        editable={{
          type: 'multiple',
          editableKeys,
          actionRender: (row, config, defaultDoms) => {
            return [defaultDoms.delete];
          },

          onValuesChange: (record, recordList) => {
            updateDetails(recordList);
          },
          onChange: setEditableRowKeys,
        }}
      />
      <ProCard title="表格数据" headerBordered collapsible defaultCollapsed>
        <ProFormField
          ignoreFormItem
          fieldProps={{
            style: {
              width: '100%',
            },
          }}
          mode="read"
          valueType="jsonCode"
          text={JSON.stringify(details)}
        />
      </ProCard>
    </>
  );
};