Blame view

src/RadioGroup/RadioGroup.js 1.05 KB
3a3ecabe   Imshann   init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  import template from "./RadioGroup.html";
  
  class RadioGroup {
      props = { value: String, defaultValue: String, onChange: Function };
  
      state = {
          value: this.props.value || this.props.defaultValue,
          childrens: [],
      };
  
      template = template;
  
      watch = {
          value: function (newVal) {
              if (newVal !== undefined) {
                  this.state.value = newVal;
                  this.updateChildChecked();
              }
          },
      };
  
      constructor() {
          for (let i = 0; i < $element[0].childNodes.length; i++) {
              let node = $element[0].childNodes[i];
              if (node.nodeType === 3) {
                  $element[0].removeChild(node);
              }
          }
      }
  
      updateChildChecked() {
          this.state.childrens.map(function (item) {
              item.state.checked = this.state.value === item.value;
          });
      }
  
      setValue(event) {
          this.state.value = event.target.value;
          this.updateChildChecked();
          this.props.onChange({
              event: event,
          });
      }
  }