Skip to content

Commit c8167d6

Browse files
committed
fix: checkbox add focus blur events & autoFocus
1 parent f62e2cc commit c8167d6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

components/checkbox/Checkbox.jsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ export default {
1717
name: String,
1818
indeterminate: Boolean,
1919
type: PropTypes.string.def('checkbox'),
20+
autoFocus: Boolean,
2021
},
2122
model: {
2223
prop: 'checked',
2324
},
2425
inject: {
2526
checkboxGroupContext: { default: null },
2627
},
28+
mounted () {
29+
this.$nextTick(() => {
30+
if (this.autoFocus) {
31+
this.$refs.input.focus()
32+
}
33+
})
34+
},
2735
data () {
2836
const { checkboxGroupContext, checked, defaultChecked, value } = this
2937
let sChecked
@@ -85,6 +93,12 @@ export default {
8593
blur () {
8694
this.$refs.input.blur()
8795
},
96+
onFocus (e) {
97+
this.$emit('focus', e)
98+
},
99+
onBlur (e) {
100+
this.$emit('blur', e)
101+
},
88102
},
89103
watch: {
90104
checked (val) {
@@ -95,7 +109,11 @@ export default {
95109
},
96110
},
97111
render () {
98-
const { $props: props, checkboxGroupContext, checkboxClass, name, $slots, sChecked } = this
112+
const { $props: props, checkboxGroupContext,
113+
checkboxClass, name, $slots, sChecked,
114+
onFocus,
115+
onBlur,
116+
} = this
99117
const {
100118
prefixCls,
101119
} = props
@@ -119,6 +137,8 @@ export default {
119137
<input name={name} type='checkbox' disabled={disabled}
120138
class={`${prefixCls}-input`} checked={sChecked}
121139
onChange={onChange} ref='input'
140+
onFocus={onFocus}
141+
onBlur={onBlur}
122142
/>
123143
<span class={`${prefixCls}-inner`} />
124144
</span>

0 commit comments

Comments
 (0)