Blame view

src/Form/Form.js 1.68 KB
3a3ecabe   Imshann   init
1
2
3
4
5
6
  import template from "./Form.html";
  import style from "antd/lib/form/style/index.css";
  
  class Form {
      useModules = ["esNgAntd"];
  
50f4289a   Imshann   优化Form组件
7
      context = true;
dd962f77   Imshann   优化
8
9
  
      state = {
a468667f   Imshann   优化
10
          formItems: [],
3a3ecabe   Imshann   init
11
      };
3a3ecabe   Imshann   init
12
  
50f4289a   Imshann   优化Form组件
13
      constructor($element) {
3a3ecabe   Imshann   init
14
          esNgAntd.createStyle("ant-form", style);
4b23b387   Imshann   feat(pagination):...
15
16
17
18
  
          if (this.form !== undefined) {
              this.form = $scope;
          }
2c0a9da3   Imshann   优化
19
          
3a3ecabe   Imshann   init
20
21
22
23
24
25
26
27
28
          if (this.props.name) {
              let inputs = $element[0].querySelectorAll("input");
              for (let i = 0; i < inputs.length; i++) {
                  const element = inputs[i];
                  element.id = this.props.name + "_" + element.id;
              }
          }
      }
  
dd962f77   Imshann   优化
29
30
      resetFields() {
          this.state.formItems.forEach(function (item) {
a468667f   Imshann   优化
31
32
33
34
35
36
              if (typeof item.setValue === "function") {
                  item.setValue(item.defaultValue || null);
              } else {
                  item.value = null;
              }
          });
dd962f77   Imshann   优化
37
38
      }
  
061629e7   Imshann   add
39
40
41
42
      submit() {
          this.handleSubmit();
      }
  
3a3ecabe   Imshann   init
43
44
      handleSubmit() {
          let values = {};
dd962f77   Imshann   优化
45
          this.state.formItems.forEach(function (item) {
81f8a467   Imshann   调整组件前缀
46
              let name = item.antdFormItem && item.antdFormItem.name;
fe17acf4   Imshann   优化组件
47
48
49
50
51
52
              let value = null;
              if (item.state.type === "checkbox") {
                  value = item.state.checked;
              } else {
                  value = item.value || item.state.value
              }
dd962f77   Imshann   优化
53
              values[name] = value;
a468667f   Imshann   优化
54
          });
3a3ecabe   Imshann   init
55
56
57
58
          this.props.onFinish({
              values: values,
          });
      }
50f4289a   Imshann   优化Form组件
59
60
61
62
  
      render() {
          return template;
      }
3a3ecabe   Imshann   init
63
  }
50f4289a   Imshann   优化Form组件
64
65
66
67
68
69
70
71
  
  Form.propTypes = {
      name: PropTypes.string,
      labelCol: PropTypes.object,
      wrapperCol: PropTypes.object,
      form: PropTypes.object,
      onFinish: PropTypes.function,
  };