index.js 2.3 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
function SingleDataPageTemplate(props) {
19
	const [isAdd, setIsAdd] = useState(false);
王绍森's avatar
王绍森 committed
20

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

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

42 43 44 45 46 47 48 49 50
	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
51

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

export default Form.create()(SingleDataPageTemplate);