forked from youzan/vant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.js
More file actions
106 lines (91 loc) · 2.19 KB
/
checkbox.js
File metadata and controls
106 lines (91 loc) · 2.19 KB
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
100
101
102
103
104
105
106
/**
* Common part of Checkbox & Radio
*/
import Icon from '../icon';
import { ChildrenMixin } from './relation';
import { addUnit } from '../utils';
export const CheckboxMixin = ({ parent, bem, role }) => ({
mixins: [ChildrenMixin(parent)],
props: {
name: null,
value: null,
disabled: Boolean,
iconSize: [Number, String],
checkedColor: String,
labelPosition: String,
labelDisabled: Boolean,
shape: {
type: String,
default: 'round'
}
},
computed: {
isDisabled() {
return (this.parent && this.parent.disabled) || this.disabled;
},
iconStyle() {
const { checkedColor } = this;
if (checkedColor && this.checked && !this.isDisabled) {
return {
borderColor: checkedColor,
backgroundColor: checkedColor
};
}
},
tabindex() {
if (this.isDisabled || (role === 'radio' && !this.checked)) {
return -1;
}
return 0;
}
},
methods: {
onClick(event) {
const { label } = this.$refs;
const { target } = event;
const labelClicked = label && (label === target || label.contains(target));
if (!this.disabled && !(labelClicked && this.labelDisabled)) {
this.toggle();
}
this.$emit('click', event);
}
},
render() {
const { slots, checked } = this;
const CheckIcon = slots('icon', { checked }) || (
<Icon name="success" style={this.iconStyle} />
);
const Label = slots() && (
<span
ref="label"
class={bem('label', [this.labelPosition, { disabled: this.isDisabled }])}
>
{slots()}
</span>
);
const Children = [
<div
class={bem('icon', [this.shape, { disabled: this.isDisabled, checked }])}
style={{ fontSize: addUnit(this.iconSize) }}
>
{CheckIcon}
</div>
];
if (this.labelPosition === 'left') {
Children.unshift(Label);
} else {
Children.push(Label);
}
return (
<div
role={role}
class={bem()}
tabindex={this.tabindex}
aria-checked={String(this.checked)}
onClick={this.onClick}
>
{Children}
</div>
);
}
});