Sanakey
2021-01-31 53cc6f817625897935fb10c3845ad7be400f3036
提交 | 用户 | age
53cc6f 1 import { useParent } from '../common/relation';
d98d05 2 import { VantComponent } from '../common/component';
S 3 function emit(target, value) {
4   target.$emit('input', value);
5   target.$emit('change', value);
6 }
7 VantComponent({
8   field: true,
53cc6f 9   relation: useParent('checkbox-group'),
d98d05 10   classes: ['icon-class', 'label-class'],
S 11   props: {
12     value: Boolean,
13     disabled: Boolean,
14     useIconSlot: Boolean,
15     checkedColor: String,
16     labelPosition: String,
17     labelDisabled: Boolean,
18     shape: {
19       type: String,
20       value: 'round',
21     },
22     iconSize: {
23       type: null,
24       value: 20,
25     },
26   },
27   data: {
28     parentDisabled: false,
29   },
30   methods: {
31     emitChange(value) {
32       if (this.parent) {
33         this.setParentValue(this.parent, value);
34       } else {
35         emit(this, value);
36       }
37     },
38     toggle() {
39       const { parentDisabled, disabled, value } = this.data;
40       if (!disabled && !parentDisabled) {
41         this.emitChange(!value);
42       }
43     },
44     onClickLabel() {
45       const { labelDisabled, parentDisabled, disabled, value } = this.data;
46       if (!disabled && !labelDisabled && !parentDisabled) {
47         this.emitChange(!value);
48       }
49     },
50     setParentValue(parent, value) {
51       const parentValue = parent.data.value.slice();
52       const { name } = this.data;
53       const { max } = parent.data;
54       if (value) {
55         if (max && parentValue.length >= max) {
56           return;
57         }
58         if (parentValue.indexOf(name) === -1) {
59           parentValue.push(name);
60           emit(parent, parentValue);
61         }
62       } else {
63         const index = parentValue.indexOf(name);
64         if (index !== -1) {
65           parentValue.splice(index, 1);
66           emit(parent, parentValue);
67         }
68       }
69     },
70   },
71 });