index.js 2.5 KB
Newer Older
王绍森's avatar
王绍森 committed
1
import React, { useState } from 'react';
王绍森's avatar
王绍森 committed
2 3 4
import { Form, Button } from 'antd';
import Shell from '@/baseComponent/Shell';
import { ModalInfo } from '@/baseComponent/Modal';
王绍森's avatar
王绍森 committed
5 6
import { addOrEditTableItem } from '@/webPublic/Services';
import { RenderFormByObjId } from '../RenderForm';
7
import { preHandle } from '@/webPublic/one_stop_public/utils/myutils';
王绍森's avatar
王绍森 committed
8

王绍森's avatar
王绍森 committed
9 10 11 12 13 14 15 16
// 单一数据页面模板,页面只是显示和操作一条数据
// 1. 页面显示通过模板id渲染。
// 2. 页面数据通过元数据表id, key,value 查询和修改。
// templateCode, 页面模板id
// tableId, 元数据列表id
// dataTypeKey, 元数据表格中查找数据的key
// dataTypeValue, 元数据表格中查找数据的value
// children, 可以传children,但children不能是数组(不能传属性),children里可以自定义其他组件。
17
// callBackPromise 保存成功后的 回调Promise函数. 用于业务上的需求
王绍森's avatar
王绍森 committed
18 19
function SingleDataPageTemplate(props) {
  const [isAdd, setIsAdd] = useState(false);
王绍森's avatar
王绍森 committed
20

王绍森's avatar
王绍森 committed
21
  function handleSave() {
22
    const { tableId, form, callBack } = props;
王绍森's avatar
王绍森 committed
23
    const { validateFields } = form;
王绍森's avatar
王绍森 committed
24

王绍森's avatar
王绍森 committed
25 26
    validateFields((err, values) => {
      if (err) return;
27
      // console.log(JSON.stringify(values.JjvkRobXWTE), JSON.stringify(values.JjvkwLqcsyY));
28
      preHandle(values);
29
      // console.log(JSON.stringify(values.JjvkRobXWTE));
30
      addOrEditTableItem({ objId: tableId, isAdd, data: values }).then((res) => {
王绍森's avatar
王绍森 committed
31
        if (res) {
32 33 34 35 36
          if (callBack) {
            callBack();
          } else {
            ModalInfo('保存成功!');
          }
王绍森's avatar
王绍森 committed
37 38 39
        }
      });
    });
王绍森's avatar
王绍森 committed
40
  }
钟是志's avatar
钟是志 committed
41

王绍森's avatar
王绍森 committed
42 43 44 45 46 47 48 49
  const { children, form, templateCode, tableId, dataTypeKey, dataTypeValue } = props;
  let ClonedChildren;
  if (children) {
    ClonedChildren = React.cloneElement(React.Children.only(children), {
      form,
      isAdd,
      url: '/DataObjApi/addFormData',
    });
王绍森's avatar
王绍森 committed
50
  }
王绍森's avatar
王绍森 committed
51 52 53 54 55 56 57 58

  return (
    <Shell styleShell={{ marginTop: 0 }}>
      <RenderFormByObjId
        objId={tableId}
        dataTypeKey={dataTypeKey}
        dataTypeValue={dataTypeValue}
        templateCode={templateCode}
59
        onLoad={(res) => setIsAdd(!!(!res || res.errMsg))}
王绍森's avatar
王绍森 committed
60 61 62 63
        form={form}
      />
      {ClonedChildren || (
        <div style={{ textAlign: 'center', padding: 16 }}>
64 65 66 67
          <Button type="primary"
                  shape="round"
                  ghost
                  onClick={handleSave}>
王绍森's avatar
王绍森 committed
68 69 70 71 72 73
            保存
          </Button>
        </div>
      )}
    </Shell>
  );
王绍森's avatar
王绍森 committed
74
}
王绍森's avatar
王绍森 committed
75 76

export default Form.create()(SingleDataPageTemplate);