Blame view

src/Checkbox/Checkbox.js 1.88 KB
3a3ecabe   Imshann   init
1
2
3
4
  import template from "./Checkbox.html";
  import style from "antd/lib/checkbox/style/index.css";
  
  class Checkbox {
3a3ecabe   Imshann   init
5
6
      useModules = ["esNgAntd"];
  
b9305a60   Imshann   优化checkbox组件
7
      require = ["^?antdForm", "^?antdFormItem"];
3a3ecabe   Imshann   init
8
9
  
      state = {
fe17acf4   Imshann   优化组件
10
          checked: this.props.checked || this.props.defaultChecked || false,
81f8a467   Imshann   调整组件前缀
11
          disabled: false,
b9305a60   Imshann   优化checkbox组件
12
          type: "checkbox",
81f8a467   Imshann   调整组件前缀
13
      };
3a3ecabe   Imshann   init
14
  
b9305a60   Imshann   优化checkbox组件
15
16
      constructor($element, $attrs, $controllers) {
          let [antdForm, antdFormItem] = $controllers;
3a3ecabe   Imshann   init
17
  
fe17acf4   Imshann   优化组件
18
19
          esNgAntd.createStyle("ant-checkbox", style);
  
b9305a60   Imshann   优化checkbox组件
20
21
          // 上下文
          if (antdForm) {
fe17acf4   Imshann   优化组件
22
23
24
25
26
27
28
29
              this.antdForm = antdForm.getContext();
              this.antdForm.state.formItems.push($scope);
          }
          if (antdFormItem) {
              this.antdFormItem = antdFormItem.getContext();
          }
      }
  
b9305a60   Imshann   优化checkbox组件
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
      componentDidUpdate(prevProps) {
          if (prevProps.checked !== this.props.checked) {
              if (newVal !== undefined) {
                  this.state.checked = ["true", "checked", true].includes(newVal)
                      ? true
                      : false;
              }
          }
          if (prevProps.disabled !== this.props.disabled) {
              if (newVal !== undefined) {
                  this.state.disabled = ["true", "disabled", true].includes(
                      newVal
                  )
                      ? true
                      : false;
              }
          }
      }
  
3a3ecabe   Imshann   init
49
      handleClick($event) {
b9305a60   Imshann   优化checkbox组件
50
51
52
          if (this.state.disabled) {
              return;
          }
3a3ecabe   Imshann   init
53
          this.state.checked = !this.state.checked;
81f8a467   Imshann   调整组件前缀
54
          this.props.onChange({ event: $event });
3a3ecabe   Imshann   init
55
56
      }
  
fe17acf4   Imshann   优化组件
57
58
59
60
61
62
      setValue(value) {
          if (value) {
              this.state.checked = value;
          } else {
              this.state.checked = false;
          }
3a3ecabe   Imshann   init
63
      }
b9305a60   Imshann   优化checkbox组件
64
65
66
67
  
      render() {
          return template;
      }
3a3ecabe   Imshann   init
68
  }
b9305a60   Imshann   优化checkbox组件
69
70
71
72
73
74
75
  
  Checkbox.propTypes = {
      defaultChecked: PropTypes.boolean,
      checked: PropTypes.string,
      disabled: PropTypes.string,
      onChange: PropTypes.function,
  };