forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckbox.jsx
161 lines (155 loc) · 3.79 KB
/
Checkbox.jsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import PropTypes from '../../_util/vue-types';
import classNames from 'classnames';
import {
getOptionProps,
hasProp,
initDefaultProps,
getAttrs,
getListeners,
} from '../../_util/props-util';
import BaseMixin from '../../_util/BaseMixin';
export default {
name: 'Checkbox',
mixins: [BaseMixin],
inheritAttrs: false,
model: {
prop: 'checked',
event: 'change',
},
props: initDefaultProps(
{
prefixCls: PropTypes.string,
name: PropTypes.string,
id: PropTypes.string,
type: PropTypes.string,
defaultChecked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
checked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
disabled: PropTypes.bool,
// onFocus: PropTypes.func,
// onBlur: PropTypes.func,
// onChange: PropTypes.func,
// onClick: PropTypes.func,
tabIndex: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
readOnly: PropTypes.bool,
autoFocus: PropTypes.bool,
value: PropTypes.any,
},
{
prefixCls: 'rc-checkbox',
type: 'checkbox',
defaultChecked: false,
},
),
data() {
const checked = hasProp(this, 'checked') ? this.checked : this.defaultChecked;
return {
sChecked: checked,
};
},
watch: {
checked(val) {
this.sChecked = val;
},
},
mounted() {
this.$nextTick(() => {
if (this.autoFocus) {
this.$refs.input && this.$refs.input.focus();
}
});
},
methods: {
focus() {
this.$refs.input.focus();
},
blur() {
this.$refs.input.blur();
},
handleChange(e) {
const props = getOptionProps(this);
if (props.disabled) {
return;
}
if (!('checked' in props)) {
this.sChecked = e.target.checked;
}
this.$forceUpdate(); // change前,维持现有状态
e.shiftKey = this.eventShiftKey;
this.__emit('change', {
target: {
...props,
checked: e.target.checked,
},
stopPropagation() {
e.stopPropagation();
},
preventDefault() {
e.preventDefault();
},
nativeEvent: e,
});
this.eventShiftKey = false;
// fix https://github.com/vueComponent/ant-design-vue/issues/3047
if ('checked' in props) {
this.$refs.input.checked = props.checked;
}
},
onClick(e) {
this.__emit('click', e);
// onChange没能获取到shiftKey,使用onClick hack
this.eventShiftKey = e.shiftKey;
},
},
render() {
const {
prefixCls,
name,
id,
type,
disabled,
readOnly,
tabIndex,
autoFocus,
value,
...others
} = getOptionProps(this);
const attrs = getAttrs(this);
const globalProps = Object.keys({ ...others, ...attrs }).reduce((prev, key) => {
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
prev[key] = others[key];
}
return prev;
}, {});
const { sChecked } = this;
const classString = classNames(prefixCls, {
[`${prefixCls}-checked`]: sChecked,
[`${prefixCls}-disabled`]: disabled,
});
return (
<span class={classString}>
<input
name={name}
id={id}
type={type}
readOnly={readOnly}
disabled={disabled}
tabIndex={tabIndex}
class={`${prefixCls}-input`}
checked={!!sChecked}
autoFocus={autoFocus}
ref="input"
value={value}
{...{
attrs: globalProps,
on: {
...getListeners(this),
change: this.handleChange,
click: this.onClick,
},
}}
/>
<span class={`${prefixCls}-inner`} />
</span>
);
},
};