You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
2.7 KiB
115 lines
2.7 KiB
7 years ago
|
|
||
7 years ago
|
import { switchPropTypes } from './PropTypes'
|
||
|
import BaseMixin from '../_util/BaseMixin'
|
||
7 years ago
|
import { hasProp, getOptionProps, getComponentFromProp } from '../_util/props-util'
|
||
7 years ago
|
|
||
|
// function noop () {
|
||
|
// }
|
||
|
export default {
|
||
|
name: 'vc-switch',
|
||
|
mixins: [BaseMixin],
|
||
|
model: {
|
||
|
prop: 'checked',
|
||
|
event: 'change',
|
||
|
},
|
||
|
props: {
|
||
|
...switchPropTypes,
|
||
|
prefixCls: switchPropTypes.prefixCls.def('rc-switch'),
|
||
|
// onChange: switchPropTypes.onChange.def(noop),
|
||
|
// onClick: switchPropTypes.onClick.def(noop),
|
||
|
},
|
||
|
data () {
|
||
|
let checked = false
|
||
|
if (hasProp(this, 'checked')) {
|
||
|
checked = !!this.checked
|
||
|
} else {
|
||
|
checked = !!this.defaultChecked
|
||
|
}
|
||
|
return {
|
||
|
stateChecked: checked,
|
||
|
}
|
||
|
},
|
||
7 years ago
|
mounted () {
|
||
7 years ago
|
this.$nextTick(() => {
|
||
|
const { autoFocus, disabled } = this
|
||
|
if (autoFocus && !disabled) {
|
||
|
this.focus()
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
watch: {
|
||
|
checked (val) {
|
||
|
this.stateChecked = val
|
||
|
},
|
||
|
},
|
||
|
methods: {
|
||
|
setChecked (checked) {
|
||
|
if (this.disabled) {
|
||
|
return
|
||
|
}
|
||
|
if (!hasProp(this, 'checked')) {
|
||
|
this.stateChecked = checked
|
||
|
}
|
||
|
this.$emit('change', checked)
|
||
|
},
|
||
|
toggle () {
|
||
|
const checked = !this.stateChecked
|
||
|
this.setChecked(checked)
|
||
|
this.$emit('click', checked)
|
||
|
},
|
||
|
handleKeyDown (e) {
|
||
|
if (e.keyCode === 37) { // Left
|
||
|
this.setChecked(false)
|
||
|
} else if (e.keyCode === 39) { // Right
|
||
|
this.setChecked(true)
|
||
|
} else if (e.keyCode === 32 || e.keyCode === 13) { // Space, Enter
|
||
|
this.toggle()
|
||
|
}
|
||
|
},
|
||
|
handleMouseUp (e) {
|
||
7 years ago
|
if (this.$refs.refSwitchNode) {
|
||
|
this.$refs.refSwitchNode.blur()
|
||
7 years ago
|
}
|
||
7 years ago
|
this.$emit('mouseup', e)
|
||
7 years ago
|
},
|
||
|
focus () {
|
||
7 years ago
|
this.$refs.refSwitchNode.focus()
|
||
7 years ago
|
},
|
||
|
blur () {
|
||
7 years ago
|
this.$refs.refSwitchNode.blur()
|
||
7 years ago
|
},
|
||
|
},
|
||
|
render () {
|
||
|
const { prefixCls, disabled, tabIndex, ...restProps } = getOptionProps(this)
|
||
|
const checked = this.stateChecked
|
||
|
const switchTabIndex = disabled ? -1 : (tabIndex || 0)
|
||
|
const switchClassName = {
|
||
|
[prefixCls]: true,
|
||
|
[`${prefixCls}-checked`]: checked,
|
||
|
[`${prefixCls}-disabled`]: disabled,
|
||
|
}
|
||
7 years ago
|
const spanProps = {
|
||
|
props: { ...restProps },
|
||
|
on: {
|
||
|
...this.$listeners,
|
||
|
keydown: this.handleKeyDown,
|
||
|
click: this.toggle,
|
||
|
mouseup: this.handleMouseUp,
|
||
|
},
|
||
|
attrs: {
|
||
|
tabIndex: switchTabIndex,
|
||
|
},
|
||
|
class: switchClassName,
|
||
|
ref: 'refSwitchNode',
|
||
|
}
|
||
7 years ago
|
return (
|
||
7 years ago
|
<span {...spanProps}>
|
||
7 years ago
|
<span class={`${prefixCls}-inner`}>
|
||
7 years ago
|
{checked ? getComponentFromProp(this, 'checkedChildren') : getComponentFromProp(this, 'unCheckedChildren')}
|
||
7 years ago
|
</span>
|
||
|
</span>
|
||
|
)
|
||
|
},
|
||
|
}
|
||
7 years ago
|
|