# 二.数据输入(单/复选框)

前言 --> 单/复选框组件特点

单选框组件多个选择框点击选择只能选择一个,选择之前会先清空选择项,然后选中选择项,点击选中的会清空这个选项;复选框组件多个选择项点击选择,会直接选中选择项,点击选中的会清空这个选项

# 1.目录结构

├── checkbox
│   ├── checkbox-button.vue
│   ├── checkbox-group.vue
│   ├── checkbox.vue
│   └── index.js
1
2
3
4
5

# 2.组件封装

# 2.1 checkbox

实现的功能点:

  • 1.支持单独使用
  • 2.支持在checkbox-group中嵌套使用
  • 3.支持在form-item中使用
<template>
  <label>
    <span>
      <input
        v-if="group"
        type="checkbox"
        :disabled="disabled"
        :value="label"
        v-model="model"
        @change="change"
      />
      <input
        v-else
        type="checkbox"
        :disabled="disabled"
        :checked="currentValue"
        @change="change"
      />
    </span>
    <slot></slot>
  </label>
</template>

<script>
import Emitter from "../../emitter.js";
import { findComponentUpward } from "../../assist.js";
export default {
  name: "VueCheckbox",
  mixins: [Emitter],
  props: {
    label: {
      type: [String, Number, Boolean],
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    value: {
      type: [String, Number, Boolean],
      default: false,
    },
    trueValue: {
      type: [String, Number, Boolean],
      default: true,
    },
    falseValue: {
      type: [String, Number, Boolean],
      default: false,
    },
  },
  data() {
    return {
      currentValue: this.value,
      model: [],
      group: false,
      parent: null,
    };
  },
  watch: {
    value(value) {
      if (value === this.trueValue || value === this.falseValue) {
        this.updateMode();
      } else {
        throw `Value should be trueValue of falseValue`;
      }
    },
  },
  methods: {
    updateModel() {
      this.currentValue = this.value === this.trueValue;
    },
    change(event) {
      if (this.disabled) {
        return false;
      }
      const checked = event.target.checked;
      this.currentValue = checked;
      const value = checked ? this.trueValue : this.falseValue;
      this.$emit("input", value);
      if (this.group) {
        this.parent.change(this.model);
      } else {
        this.$emit("on-change", value);
        this.dispatch("VueFormItem", "on-form-change", value);
      }
    },
  },
  mounted() {
    this.parent = findComponentUpward(this, "VueCheckboxGroup");
    if (this.parent) {
      this.group = true;
    }
    if (this.group) {
      this.parent.updateModel(true);
    } else {
      this.updateModel();
    }
  },
};
</script>
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

# 2.2 checkbox-group

<template>
  <div>
    <slot></slot>
  </div>
</template>
<script>
import { findComponentsDownward } from "../../assist.js"
import Emitter from "../../emitter.js"

export default {
  name: "VueCheckboxGroup",
  mixins: [Emitter],
  props: {
    value: {
      type: Array,
      default() {
        return []
      },
    },
  },
  data() {
    return {
      currentValue: this.value,
      childrens: [],
    }
  },
  methods: {
    updateModel(update) {
      this.childrens = findComponentsDownward(this, "iCheckbox")
      if (this.childrens) {
        const { value } = this
        this.childrens.forEach((child) => {
          child.model = value

          if (update) {
            child.currentValue = value.indexOf(child.label) >= 0
            child.group = true
          }
        })
      }
    },
    change(data) {
      this.currentValue = data
      this.$emit("input", data)
      this.$emit("on-change", data)
      this.dispatch("VueFormItem", "on-form-change", data)
    },
  },
  mounted() {
    this.updateModel(true)
  },
  watch: {
    value() {
      this.updateModel(true)
    },
  },
}
</script>
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
45
46
47
48
49
50
51
52
53
54
55
56
57

# 3.使用案例

刷新
全屏/自适应
刷新
全屏/自适应

总结

通过对前端组件的分析,需要重点关注组件中易变性对组件封装的影响,它会对组件的可复用性、可扩展性产生很大影响