ChildForm.jsx 8.9 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 {
徐立's avatar
徐立 committed
9 10 11 12 13 14
  constructor(props) {
    super(props);
    const value = props.value || {};

    this.state = value;
  }
15

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

徐立's avatar
徐立 committed
24 25 26
  componentWillReceiveProps(nextProps) {
    // Should be a controlled component.
    if ('value' in nextProps) {
钟是志's avatar
钟是志 committed
27

徐立's avatar
徐立 committed
28
      const value = nextProps.value;
chlolch's avatar
chlolch committed
29
      this.state = value;
徐立's avatar
徐立 committed
30
    }
徐立's avatar
徐立 committed
31
  }
32

徐立's avatar
徐立 committed
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')] = {};
          }
徐立's avatar
徐立 committed
45
        }
46 47

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

      return;
徐立's avatar
徐立 committed
65
    }
66
    const x = form.getFieldsValue()[base52];
chlolch's avatar
chlolch committed
67 68
    delete x[uuid];
    delete x[""];
钟是志's avatar
钟是志 committed
69

徐立's avatar
徐立 committed
70
    if (!('value' in this.props)) {
71
      this.state = x
徐立's avatar
徐立 committed
72
    }
chlolch's avatar
chlolch committed
73
    this.triggerChange(x);
徐立's avatar
徐立 committed
74

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

      return;
    }
    objs['id_' + UUID.v4().replace(/-/g, '2')] = {};
钟是志's avatar
钟是志 committed
92

徐立's avatar
徐立 committed
93
    if (!('value' in this.props)) {
chlolch's avatar
chlolch committed
94
      this.setState(objs);
徐立's avatar
徐立 committed
95
    }
chlolch's avatar
chlolch committed
96
    this.triggerChange(objs);
徐立's avatar
徐立 committed
97
    if (numCode != null) {
98
      this.props.form.setFieldsValue({[numCode]: Object.keys(objs).length});
徐立's avatar
徐立 committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    }
  };

  render() {
    const objs = this.state;
    const {
      form,
      mapData,
      sqlData,
      defaultValues,
      datas,
      base52,
      isEdit,
      addName,
      deleteName,
      obj,
      isMobile,
      json,
      modalInit,
      isPreview,
tb53863844's avatar
tb53863844 committed
119 120
      hfInstance,
      getCurrentFormTitle,
ch's avatar
ch committed
121 122
      getCellValue,
      taskId
徐立's avatar
徐立 committed
123
    } = this.props;
钟是志's avatar
钟是志 committed
124

徐立's avatar
徐立 committed
125 126 127 128 129 130 131 132 133 134 135 136
    const span = json.span || 24;
    const gutter = json.gutter != null ? json.gutter : 0;
    const rights = this.props.rights;

    if (datas == null) {
      return <div>还没有配置子表单key</div>;
    }
    if (isMobile) {
      return (
        <WingBlank size="lg">
          {Object.keys(objs).length > 1 || rights.includes('add') ? (
            <Card>
137
              <Card.Body style={{minHeight: 10}}>
徐立's avatar
徐立 committed
138 139 140 141 142 143 144 145 146 147 148 149 150
                {Object.keys(objs).map(r => {
                  if (r == '') {
                    return '';
                  }
                  return (
                    <Row key={r} gutter={gutter}>
                      <Col
                        style={{
                          zIndex: 55,
                        }}
                        span={isEdit ? 24 : span}
                      >
                        <ZdyTable
钟是志's avatar
钟是志 committed
151

152 153
                          setRealTimeValues={this.props.setRealTimeValues}
                          trees={this.props.trees}
徐立's avatar
徐立 committed
154
                          key={r}
ch's avatar
ch committed
155
                          taskId={taskId}
徐立's avatar
徐立 committed
156
                          modalInit={modalInit}
tb53863844's avatar
tb53863844 committed
157
                          hfInstance={hfInstance}
158
                          currentFormTitle={getCurrentFormTitle ? getCurrentFormTitle(json.childFormKey) : null}
tb53863844's avatar
tb53863844 committed
159 160 161
                          getCellValue={getCellValue}
                          hfInstance={hfInstance}
                          getCellValue={getCellValue}
徐立's avatar
徐立 committed
162 163 164
                          formCode={this.props.formCode}
                          formId={this.props.formId}
                          isPreview={isPreview}
tb53863844's avatar
tb53863844 committed
165
                          currentFormKey={json.childFormKey}
徐立's avatar
徐立 committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                          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}
                          {...datas}
                          defaultValues={defaultValues}
                        />
                      </Col>
                      {rights.includes('delete') && isEdit ? (
183
                        <Col style={{textAlign: 'right'}}>
徐立's avatar
徐立 committed
184 185 186
                          <Button type="danger" size="small" onClick={this.delete.bind(this, r)}>
                            {deleteName || '删除'}{' '}
                          </Button>{' '}
徐立's avatar
徐立 committed
187
                        </Col>
徐立's avatar
徐立 committed
188 189 190 191 192 193 194
                      ) : (
                        ''
                      )}
                    </Row>
                  );
                })}

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

钟是志's avatar
钟是志 committed
220 221 222 223

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

                {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>
291 292
            <Col span={24} style={{textAlign: 'center'}}>
              <Button style={{margin: 'auto'}} type="primary" onClick={this.add} size="small">
徐立's avatar
徐立 committed
293 294 295 296 297 298 299 300 301 302
                {addName || '新增'}
              </Button>
            </Col>{' '}
          </Row>
        ) : (
          ''
        )}
      </div>
    );
  }
徐立's avatar
徐立 committed
303
}