DeliverModal.tsx 2.71 KB
import {
  ProColumns,
  ProForm,
  ProFormText,
  ProTable,
} from '@ant-design/pro-components';
import { Button, Input, InputNumber, Modal } from 'antd';
import { cloneDeep } from 'lodash';
import { useEffect, useRef, useState } from 'react';

const DeliverModal = ({ data: propsData, onClose }) => {
  const [data, setData] = useState(propsData || {});
  const form = useRef();

  useEffect(() => {
    setData(propsData);
  }, [propsData]);

  const handleChange = (key: string, index: number) => (e) => {
    const newData = cloneDeep(data);
    newData[index][key] = e.target.value;
    setData(newData);
  };
  const columns: ProColumns<any>[] = [
    {
      title: '商品编号',
      width: 80,
      dataIndex: 'name',
    },
    {
      title: '商品名称',
      dataIndex: 'containers',
      align: 'right',
    },
    {
      title: '商品数量',
      width: 80,
      dataIndex: 'status',
    },
    {
      title: '物流方式',
      width: 180,
      key: 'paymentChannel',
      render: (_, record, index) => (
        <Input
          value={record.paymentChannel}
          onChange={handleChange('paymentChannel', index)}
        />
      ),
    },
    {
      title: '物流单号',
      width: 180,
      key: 'productCode',
      render: (_, record, index) => (
        <InputNumber
          value={record.productCode}
          onChange={handleChange('productCode', index)}
        />
      ),
    },
  ];

  return (
    <Modal
      open
      width={800}
      title="发货"
      onOk={() => {
        console.log(data);
        onClose();
      }}
      onCancel={() => {
        onClose();
      }}
    >
      <strong>将物流方式和物流单号更新到下方所有订单</strong>
      <ProForm
        layout="inline"
        submitter={false}
        className="mb-8"
        formRef={form}
      >
        <ProFormText name="paymentChannel" label="物流方式"></ProFormText>
        <ProFormText name="productCode" label="物流单号"></ProFormText>
        <Button
          type="primary"
          onClick={() => {
            const values = form.current.getFieldsValue();
            let newData = cloneDeep(data);
            newData = newData.map((item) => ({
              ...item,
              paymentChannel: values.paymentChannel,
              productCode: values.productCode, // 物流单号?
            }));
            setData(newData);
          }}
        >
          批量更新
        </Button>
      </ProForm>
      <ProTable<any>
        className="px-0"
        dataSource={data}
        rowKey="key"
        pagination={false}
        columns={columns}
        search={false}
        dateFormatter="string"
        options={false}
      />
    </Modal>
  );
};

export default DeliverModal;