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.
116 lines
2.7 KiB
116 lines
2.7 KiB
|
|
import { switchPropTypes } from './PropTypes'
|
|
import BaseMixin from '../_util/BaseMixin'
|
|
import { hasProp, getOptionProps, getComponentFromProp } from '../_util/props-util'
|
|
|
|
// 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,
|
|
}
|
|
},
|
|
mounted () {
|
|
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)
|
|
}
|
|
},
|
|
handleMouseUp (e) {
|
|
if (this.$refs.refSwitchNode) {
|
|
this.$refs.refSwitchNode.blur()
|
|
}
|
|
this.$emit('mouseup', e)
|
|
},
|
|
focus () {
|
|
this.$refs.refSwitchNode.focus()
|
|
},
|
|
blur () {
|
|
this.$refs.refSwitchNode.blur()
|
|
},
|
|
},
|
|
render () {
|
|
const { prefixCls, disabled, loadingIcon, ...restProps } = getOptionProps(this)
|
|
const checked = this.stateChecked
|
|
const switchClassName = {
|
|
[prefixCls]: true,
|
|
[`${prefixCls}-checked`]: checked,
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
}
|
|
const spanProps = {
|
|
props: { ...restProps },
|
|
on: {
|
|
...this.$listeners,
|
|
keydown: this.handleKeyDown,
|
|
click: this.toggle,
|
|
mouseup: this.handleMouseUp,
|
|
},
|
|
attrs: {
|
|
type: 'button',
|
|
role: 'switch',
|
|
'aria-checked': checked,
|
|
disabled,
|
|
},
|
|
class: switchClassName,
|
|
ref: 'refSwitchNode',
|
|
}
|
|
return (
|
|
<button {...spanProps}>
|
|
{loadingIcon}
|
|
<span class={`${prefixCls}-inner`}>
|
|
{checked ? getComponentFromProp(this, 'checkedChildren') : getComponentFromProp(this, 'unCheckedChildren')}
|
|
</span>
|
|
</button>
|
|
)
|
|
},
|
|
}
|
|
|