ChildForm.jsx 7.4 KB
Newer Older
徐立's avatar
徐立 committed
1
import React from 'react';
2
import { Button, Row, Col, message } from 'antd';
徐立's avatar
徐立 committed
3
import ZdyTable from '../Table';
徐立's avatar
徐立 committed
4 5
import UUID from 'react-native-uuid';

6
import { Card, WingBlank } from 'antd-mobile';
徐立's avatar
徐立 committed
7 8

export default class ChildForm extends React.Component {
9 10 11
	constructor(props) {
		super(props);
		const value = props.value || {};
徐立's avatar
徐立 committed
12

13 14
		this.state = value;
	}
15

16 17 18 19 20 21 22
	triggerChange = (changedValue) => {
		// Should provide an event to pass value to Form.
		const onChange = this.props.onChange;
		if (onChange) {
			onChange(changedValue);
		}
	};
23

24 25 26 27 28 29 30
	componentWillReceiveProps(nextProps) {
		// Should be a controlled component.
		if ('value' in nextProps) {
			const value = nextProps.value;
			this.state = value;
		}
	}
钟是志's avatar
钟是志 committed
31

32 33 34 35 36 37 38 39 40 41 42 43 44
	componentDidMount = () => {
		if (Object.keys(this.state).length == 0 && this.props.isEdit) {
			if (this.props.num != null && this.props.num > 0) {
				const objs = this.state;
				if (this.props.min != null && this.props.min > this.props.num) {
					for (var i = 0; i < this.props.min; i++) {
						objs['id_' + UUID.v4().replace(/-/g, '2')] = {};
					}
				} else {
					for (var i = 0; i < this.props.num; i++) {
						objs['id_' + UUID.v4().replace(/-/g, '2')] = {};
					}
				}
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
				if (!('value' in this.props)) {
					this.setState(objs);
				}
				this.triggerChange(objs);
			}
		}
	};
	delete = (uuid) => {
		const objs = this.state;
		const {
			json: { numCode },
			form,
			base52,
		} = this.props;
		if (this.props.min != null && Object.keys(objs).length - 1 < this.props.min) {
			message.error('不能小于最小限制');
62

63 64 65 66 67
			return;
		}
		const x = form.getFieldsValue()[base52];
		delete x[uuid];
		delete x[''];
徐立's avatar
徐立 committed
68

69 70 71 72
		if (!('value' in this.props)) {
			this.state = x;
		}
		this.triggerChange(x);
钟是志's avatar
钟是志 committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86
		if (numCode != null) {
			this.props.form.setFieldsValue({ [numCode]: Object.keys(objs).length });
		}
	};
	add = () => {
		const objs = this.state;
		const {
			json: { numCode },
			form,
			base52,
		} = this.props;
		if (this.props.max != null && Object.keys(objs).length + 1 > this.props.max) {
			message.error('不能大于最大限制');
徐立's avatar
徐立 committed
87

88 89 90
			return;
		}
		objs['id_' + UUID.v4().replace(/-/g, '2')] = {};
徐立's avatar
徐立 committed
91

92 93 94 95 96 97 98 99
		if (!('value' in this.props)) {
			this.setState(objs);
		}
		this.triggerChange(objs);
		if (numCode != null) {
			this.props.form.setFieldsValue({ [numCode]: Object.keys(objs).length });
		}
	};
钟是志's avatar
钟是志 committed
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	render() {
		const objs = this.state;
		const {
			form,
			mapData,
			sqlData,
			defaultValues,
			datas,
			base52,
			isEdit,
			addName,
			deleteName,
			obj,
			isMobile,
			json,
			modalInit,
			isPreview,
			hfInstance,
			getCurrentFormTitle,
			getCellValue,
			taskId,
122
      datasAll,
123
		} = this.props;
徐立's avatar
徐立 committed
124

125 126 127
		const span = json.span || 24;
		const gutter = json.gutter != null ? json.gutter : 0;
		const rights = this.props.rights;
128
		// console.log(datasAll);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
		if (datas == null) {
			return <div>还没有配置子表单key</div>;
		}
		if (isMobile) {
			return (
				<WingBlank size="lg">
					{Object.keys(objs).length > 1 || rights.includes('add') ? (
						<Card>
							<Card.Body style={{ minHeight: 10 }}>
								{Object.keys(objs).map((r) => {
									if (r == '') {
										return '';
									}
									return (
										<Row key={r} gutter={gutter}>
											<Col
												style={{
													zIndex: 55,
												}}
												span={isEdit ? 24 : span}>
												<ZdyTable
ch's avatar
ch committed
150
												changedValues={this.props.changedValues}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
													setRealTimeValues={this.props.setRealTimeValues}
													trees={this.props.trees}
													key={r}
													taskId={taskId}
													modalInit={modalInit}
													hfInstance={hfInstance}
													currentFormTitle={
														getCurrentFormTitle ? getCurrentFormTitle(json.childFormKey) : null
													}
													hfInstance={hfInstance}
													getCellValue={getCellValue}
													formCode={this.props.formCode}
													formId={this.props.formId}
													isPreview={isPreview}
													currentFormKey={json.childFormKey}
													formConfig={datas}
													get="mobile"
													fatherCode={base52}
													isEdit={isEdit}
													index={r}
													obj={objs[r]}
													fatherObj={this.props.fatherObj}
													init={objs}
													isChild={true}
													form={form}
													mapData={mapData}
													sqlData={sqlData}
178
                          datas={datasAll}
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
													{...datas}
													defaultValues={defaultValues}
												/>
											</Col>
											{rights.includes('delete') && isEdit ? (
												<Col style={{ textAlign: 'right' }}>
													<Button type="danger" size="small" onClick={this.delete.bind(this, r)}>
														{deleteName || '删除'}{' '}
													</Button>{' '}
												</Col>
											) : (
												''
											)}
										</Row>
									);
								})}
徐立's avatar
徐立 committed
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
								{rights.includes('add') && isEdit ? (
									<Row>
										<Col style={{ textAlign: 'center' }}>
											<Button
												type="primary"
												size="small"
												style={{ margin: 'auto' }}
												onClick={this.add}>
												{addName || '新增'}
											</Button>{' '}
										</Col>
									</Row>
								) : (
									''
								)}
							</Card.Body>
						</Card>
					) : (
						''
					)}
				</WingBlank>
			);
		}
钟是志's avatar
钟是志 committed
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
		/***
		 * 子表单
		 * */
		return (
			<div style={{ width: '100%' }}>
				<Row gutter={gutter}>
					{Object.keys(objs).map((r) => {
						if (r == '') {
							return '';
						}
						// console.log('子表单', r);
						// console.log(form.getFieldsValue());
						return (
							<Col span={span} key={r}>
								<ZdyTable
ch's avatar
ch committed
235
								    changedValues={this.props.changedValues}
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
									taskId={taskId}
									setRealTimeValues={this.props.setRealTimeValues}
									modalInit={modalInit}
									hfInstance={hfInstance}
									currentFormTitle={
										getCurrentFormTitle ? getCurrentFormTitle(json.childFormKey) : null
									}
									getCellValue={getCellValue}
									formConfig={datas}
									key={r}
									fatherCode={base52}
									isEdit={isEdit}
									index={r}
									isPreview={isPreview}
									obj={objs[r]}
									currentFormKey={json.childFormKey}
									formCode={this.props.formCode}
									formId={this.props.formId}
									fatherObj={this.props.fatherObj}
									init={objs}
									isChild={true}
									get="web"
									form={form}
									mapData={mapData}
									sqlData={sqlData}
261 262
                  datas={datasAll}
                  {...datas}
263 264
									defaultValues={defaultValues}
								/>
徐立's avatar
徐立 committed
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
								{rights.includes('delete') && isEdit ? (
									<Button
										size="small"
										style={{
											position: 'absolute',
											left: ' 95%',
											top: '10px',
											transform: 'translateY(-50%)',
											borderRadius: '50%',
											height: '13px',
											width: '13px',
											padding: 0,
											lineHeight: '10px',
										}}
										type="danger"
										onClick={this.delete.bind(this, r)}>
										{deleteName || '-'}
									</Button>
								) : (
									''
								)}
							</Col>
						);
					})}
				</Row>
				{rights.includes('add') && isEdit ? (
					<Row>
						<Col span={24} style={{ textAlign: 'center' }}>
							<Button style={{ margin: 'auto' }} type="primary" onClick={this.add} size="small">
								{addName || '新增'}
							</Button>
						</Col>{' '}
					</Row>
				) : (
					''
				)}
			</div>
		);
	}
徐立's avatar
徐立 committed
305
}