Skip to content

Commit e477f52

Browse files
committed
refactor: vc-checkbox
1 parent 259fc4e commit e477f52

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

components/vc-checkbox/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './src/'
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
2+
import PropTypes from '../../_util/vue-types'
3+
import classNames from 'classnames'
4+
import { getOptionProps, hasProp, initDefaultProps } from '../../_util/props-util'
5+
import BaseMixin from '../../_util/BaseMixin'
6+
7+
export default {
8+
name: 'Checkbox',
9+
mixins: [BaseMixin],
10+
props: initDefaultProps({
11+
prefixCls: PropTypes.string,
12+
name: PropTypes.string,
13+
id: PropTypes.string,
14+
type: PropTypes.string,
15+
defaultChecked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
16+
checked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
17+
disabled: PropTypes.bool,
18+
// onFocus: PropTypes.func,
19+
// onBlur: PropTypes.func,
20+
// onChange: PropTypes.func,
21+
// onClick: PropTypes.func,
22+
// tabIndex: PropTypes.string,
23+
// readOnly: PropTypes.bool,
24+
// autoFocus: PropTypes.bool,
25+
value: PropTypes.any,
26+
}, {
27+
prefixCls: 'rc-checkbox',
28+
type: 'checkbox',
29+
defaultChecked: false,
30+
}),
31+
data () {
32+
const checked = hasProp(this, 'checked') ? this.checked : this.defaultChecked
33+
return {
34+
sChecked: checked,
35+
}
36+
},
37+
watch: {
38+
checked (val) {
39+
this.sChecked = val
40+
},
41+
},
42+
methods: {
43+
focus () {
44+
this.$refs.input.focus()
45+
},
46+
47+
blur () {
48+
this.$refs.input.blur()
49+
},
50+
51+
handleChange (e) {
52+
const props = getOptionProps(this)
53+
if (props.disabled) {
54+
return
55+
}
56+
if (!('checked' in props)) {
57+
this.sChecked = e.target.checked
58+
}
59+
60+
props.onChange({
61+
target: {
62+
...props,
63+
checked: e.target.checked,
64+
},
65+
stopPropagation () {
66+
e.stopPropagation()
67+
},
68+
preventDefault () {
69+
e.preventDefault()
70+
},
71+
nativeEvent: e.nativeEvent,
72+
})
73+
},
74+
},
75+
76+
render () {
77+
const {
78+
prefixCls,
79+
name,
80+
id,
81+
type,
82+
disabled,
83+
readOnly,
84+
tabIndex,
85+
onClick,
86+
onFocus,
87+
onBlur,
88+
autoFocus,
89+
value,
90+
...others
91+
} = this
92+
93+
const globalProps = Object.keys(others).reduce((prev, key) => {
94+
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
95+
prev[key] = others[key]
96+
}
97+
return prev
98+
}, {})
99+
100+
const { checked } = this.state
101+
const classString = classNames(prefixCls, {
102+
[`${prefixCls}-checked`]: checked,
103+
[`${prefixCls}-disabled`]: disabled,
104+
})
105+
106+
return (
107+
<span class={classString}>
108+
<input
109+
name={name}
110+
id={id}
111+
type={type}
112+
readOnly={readOnly}
113+
disabled={disabled}
114+
tabIndex={tabIndex}
115+
class={`${prefixCls}-input`}
116+
checked={!!checked}
117+
onClick={onClick}
118+
onFocus={onFocus}
119+
onBlur={onBlur}
120+
onChange={this.handleChange}
121+
autoFocus={autoFocus}
122+
ref={this.saveInput}
123+
value={value}
124+
{...globalProps}
125+
/>
126+
<span class={`${prefixCls}-inner`} />
127+
</span>
128+
)
129+
},
130+
}

components/vc-checkbox/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Checkbox from './Checkbox'
2+
3+
export default Checkbox

0 commit comments

Comments
 (0)