Blame view

src/pages/Instalment/components/productDetail/productDetail.tsx 4.77 KB
PurelzMgnead authored
1
import type { ProColumns } from '@ant-design/pro-components';
PurelzMgnead authored
2
import { EditableProTable, ProFormRadio } from '@ant-design/pro-components';
PurelzMgnead authored
3
import React, { useEffect, useState } from 'react';
PurelzMgnead authored
4
import './productDetail.less';
PurelzMgnead authored
5
6

const waitTime = (time: number = 100) => {
PurelzMgnead authored
7
8
9
10
11
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
PurelzMgnead authored
12
13
};
PurelzMgnead authored
14
15
16
17
18
19
20
21
// type DataSourceType = {
//     count?: number;
//     id: React.Key;
//     deviceModel?: string;
//     deviceName?: string;
//     price?: number;
//     unitPrice?: number;
// };
PurelzMgnead authored
22
23

export default ({ productBody, EditProductBody }) => {
PurelzMgnead authored
24
25
26
27
28
29
30
  const [editableKeys, setEditableRowKeys] = useState<React.Key[]>([]);
  const [dataSource, setDataSource] = useState<readonly DataSourceType[]>([]);
  // const [form] = Form.useForm<{ name: string; company: string }>();
  const [position, setPosition] = useState<'top' | 'bottom' | 'hidden'>(
    'bottom',
  );
  function getDataSourece() {
31
32
33
34
    if (productBody?.length) {
      if (productBody?.length !== 0) {
        setDataSource(productBody);
      }
PurelzMgnead authored
35
    }
PurelzMgnead authored
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  }
  function setEditProductBody(value) {
    const modifiedArray = value.map((obj) => {
      if (obj.dId && Number(obj.dId) <= 1000) {
        return {
          ...obj,
          count: obj.count,
          dId: null,
          deviceModel: obj.deviceModel,
          deviceName: obj.deviceName,
          price: Number(obj.unitPrice) * Number(obj.count),
          unitPrice: obj.unitPrice,
        };
      } else {
        return { ...obj, price: Number(obj.unitPrice) * Number(obj.count) };
      }
    });
PurelzMgnead authored
53
PurelzMgnead authored
54
55
56
    EditProductBody(modifiedArray);
    setDataSource(value);
  }
PurelzMgnead authored
57
PurelzMgnead authored
58
59
60
  useEffect(() => {
    getDataSourece();
  }, [productBody]);
PurelzMgnead authored
61
PurelzMgnead authored
62
63
64
65
66
67
68
69
70
  type DataSourceType = {
    id: React.Key;
    count: number;
    dId?: number;
    deviceModel: string;
    deviceName: string;
    price: number;
    unitPrice: number;
  };
PurelzMgnead authored
71
PurelzMgnead authored
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  const columns: ProColumns<DataSourceType>[] = [
    {
      title: '设备编号',
      dataIndex: 'dId',
      hideInTable: true,
    },
    {
      title: '设备名称',
      dataIndex: 'deviceName',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '设备型号',
      dataIndex: 'deviceModel',
      width: '15%',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '数量',
      dataIndex: 'count',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '单价',
      dataIndex: 'unitPrice',
      formItemProps: () => {
        return {
          rules: [{ required: true, message: '此项为必填项' }],
        };
      },
    },
    {
      title: '总价',
      dataIndex: 'price',
      hideInSetting: true,
      disable: true,
      editable: false,
      render: (_text, record) => [
        <span key={record.dId}>{record.count * record.unitPrice}</span>,
      ],
    },
    {
      title: '操作',
      valueType: 'option',
      width: 200,
      render: (_text, record, _, action) => [
        <a
          key="editable"
          onClick={() => {
            if (record.dId) {
              action?.startEditable?.(record.dId);
PurelzMgnead authored
135
            }
PurelzMgnead authored
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
          }}
        >
          编辑
        </a>,
        <a
          key="delete"
          onClick={() => {
            EditProductBody(
              dataSource.filter((item) => item.dId !== record.dId),
            );
            setDataSource(dataSource.filter((item) => item.dId !== record.dId));
          }}
        >
          删除
        </a>,
      ],
    },
  ];
PurelzMgnead authored
154
PurelzMgnead authored
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  return (
    <>
      <EditableProTable<DataSourceType>
        className="product-detail-index"
        rowKey="dId"
        toolbar={{ style: { display: 'none' } }}
        ghost={true}
        scroll={{
          x: 960,
        }}
        recordCreatorProps={
          position !== 'hidden'
            ? {
                position: position as 'top',
                record: () => ({ dId: (Math.random() * 1000).toFixed(0) }),
              }
            : false
        }
        loading={false}
        toolBarRender={() => [
          <ProFormRadio.Group
            key="render"
            fieldProps={{
              value: position,
              onChange: (e) => setPosition(e.target.value),
            }}
          />,
        ]}
        columns={columns}
        request={dataSource}
        value={dataSource}
        onChange={setEditProductBody}
        editable={{
          type: 'multiple',
          editableKeys,
          onSave: async () => {
            await waitTime(500);
          },
          onChange: setEditableRowKeys,
        }}
      />
    </>
  );
};