2018-03-19 02:16:27 +00:00
|
|
|
|
2018-02-27 04:14:29 +00:00
|
|
|
import { switchPropTypes } from './PropTypes'
|
|
|
|
import BaseMixin from '../_util/BaseMixin'
|
2018-02-27 07:28:30 +00:00
|
|
|
import { hasProp, getOptionProps, getComponentFromProp } from '../_util/props-util'
|
2018-02-27 04:14:29 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
},
|
2018-02-27 07:28:30 +00:00
|
|
|
mounted () {
|
2018-02-27 04:14:29 +00:00
|
|
|
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) {
|
2018-02-27 07:28:30 +00:00
|
|
|
if (this.$refs.refSwitchNode) {
|
|
|
|
this.$refs.refSwitchNode.blur()
|
2018-02-27 04:14:29 +00:00
|
|
|
}
|
2018-02-27 07:28:30 +00:00
|
|
|
this.$emit('mouseup', e)
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
|
|
|
focus () {
|
2018-02-27 07:28:30 +00:00
|
|
|
this.$refs.refSwitchNode.focus()
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
|
|
|
blur () {
|
2018-02-27 07:28:30 +00:00
|
|
|
this.$refs.refSwitchNode.blur()
|
2018-02-27 04:14:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
render () {
|
2018-10-31 11:28:53 +00:00
|
|
|
const { prefixCls, disabled, loadingIcon, ...restProps } = getOptionProps(this)
|
2018-02-27 04:14:29 +00:00
|
|
|
const checked = this.stateChecked
|
|
|
|
const switchClassName = {
|
|
|
|
[prefixCls]: true,
|
|
|
|
[`${prefixCls}-checked`]: checked,
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
}
|
2018-02-27 07:28:30 +00:00
|
|
|
const spanProps = {
|
|
|
|
props: { ...restProps },
|
|
|
|
on: {
|
|
|
|
...this.$listeners,
|
|
|
|
keydown: this.handleKeyDown,
|
|
|
|
click: this.toggle,
|
|
|
|
mouseup: this.handleMouseUp,
|
|
|
|
},
|
|
|
|
attrs: {
|
2018-10-31 11:28:53 +00:00
|
|
|
type: 'button',
|
|
|
|
role: 'switch',
|
|
|
|
'aria-checked': checked,
|
|
|
|
disabled,
|
2018-02-27 07:28:30 +00:00
|
|
|
},
|
|
|
|
class: switchClassName,
|
|
|
|
ref: 'refSwitchNode',
|
|
|
|
}
|
2018-02-27 04:14:29 +00:00
|
|
|
return (
|
2018-10-31 11:28:53 +00:00
|
|
|
<button {...spanProps}>
|
|
|
|
{loadingIcon}
|
2018-02-27 04:14:29 +00:00
|
|
|
<span class={`${prefixCls}-inner`}>
|
2018-02-27 07:28:30 +00:00
|
|
|
{checked ? getComponentFromProp(this, 'checkedChildren') : getComponentFromProp(this, 'unCheckedChildren')}
|
2018-02-27 04:14:29 +00:00
|
|
|
</span>
|
2018-10-31 11:28:53 +00:00
|
|
|
</button>
|
2018-02-27 04:14:29 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 02:16:27 +00:00
|
|
|
|