fix switch

pull/165/head
tangjinzhou 2018-02-27 15:28:30 +08:00
parent 166fab0dfe
commit ab89e57a53
2 changed files with 28 additions and 49 deletions

View File

@ -1,6 +1,6 @@
<script> <script>
import PropTypes from '../_util/vue-types' import PropTypes from '../_util/vue-types'
import { getOptionProps } from '../_util/props-util' import { getOptionProps, getComponentFromProp } from '../_util/props-util'
import VcSwitch from '../vc-switch' import VcSwitch from '../vc-switch'
export default { export default {
@ -22,23 +22,12 @@ export default {
autoFocus: PropTypes.bool, autoFocus: PropTypes.bool,
loading: PropTypes.bool, loading: PropTypes.bool,
}, },
monted () {
this.$nextTick(() => {
this.refSwitchNode = this.$refs.refSwitchNode
})
},
methods: { methods: {
focus () { focus () {
this.refSwitchNode.focus() this.$refs.refSwitchNode.focus()
}, },
blur () { blur () {
this.refSwitchNode.blur() this.$refs.refSwitchNode.blur()
},
getChildren () {
return {
checkedChildren: this.$slots.checkedChildren || null,
unCheckedChildren: this.$slots.unCheckedChildren || null,
}
}, },
}, },
@ -57,17 +46,12 @@ export default {
class: classes, class: classes,
ref: 'refSwitchNode', ref: 'refSwitchNode',
} }
const children = this.getChildren()
return ( return (
<VcSwitch <VcSwitch
{...switchProps} {...switchProps}
> >
{children.checkedChildren ? ( <template slot='checkedChildren'>{getComponentFromProp(this, 'checkedChildren')}</template>
<template slot='checkedChildren'>{children.checkedChildren}</template> <template slot='unCheckedChildren'>{getComponentFromProp(this, 'unCheckedChildren')}</template>
) : null}
{children.unCheckedChildren ? (
<template slot='unCheckedChildren'>{children.unCheckedChildren}</template>
) : null}
</VcSwitch> </VcSwitch>
) )
}, },

View File

@ -1,7 +1,7 @@
<script> <script>
import { switchPropTypes } from './PropTypes' import { switchPropTypes } from './PropTypes'
import BaseMixin from '../_util/BaseMixin' import BaseMixin from '../_util/BaseMixin'
import { hasProp, filterEmpty, getOptionProps } from '../_util/props-util' import { hasProp, getOptionProps, getComponentFromProp } from '../_util/props-util'
// function noop () { // function noop () {
// } // }
@ -32,13 +32,12 @@ export default {
stateChecked: checked, stateChecked: checked,
} }
}, },
monted () { mounted () {
this.$nextTick(() => { this.$nextTick(() => {
const { autoFocus, disabled } = this const { autoFocus, disabled } = this
if (autoFocus && !disabled) { if (autoFocus && !disabled) {
this.focus() this.focus()
} }
this.refSwitchNode = this.$refs.refSwitchNode
}) })
}, },
watch: { watch: {
@ -71,25 +70,16 @@ export default {
} }
}, },
handleMouseUp (e) { handleMouseUp (e) {
if (this.refSwitchNode) { if (this.$refs.refSwitchNode) {
this.refSwitchNode.blur() this.$refs.refSwitchNode.blur()
} }
this.$emit('mouseUp', e) this.$emit('mouseup', e)
}, },
focus () { focus () {
this.refSwitchNode.focus() this.$refs.refSwitchNode.focus()
}, },
blur () { blur () {
this.refSwitchNode.blur() this.$refs.refSwitchNode.blur()
},
getChildren (slotName) {
if (hasProp(this, slotName)) {
return this[slotName]
}
if (this.$slots && this.$slots[slotName]) {
return filterEmpty(this.$slots[slotName])
}
return null
}, },
}, },
render () { render () {
@ -101,19 +91,24 @@ export default {
[`${prefixCls}-checked`]: checked, [`${prefixCls}-checked`]: checked,
[`${prefixCls}-disabled`]: disabled, [`${prefixCls}-disabled`]: disabled,
} }
const childrenType = checked ? 'checkedChildren' : 'unCheckedChildren' const spanProps = {
props: { ...restProps },
on: {
...this.$listeners,
keydown: this.handleKeyDown,
click: this.toggle,
mouseup: this.handleMouseUp,
},
attrs: {
tabIndex: switchTabIndex,
},
class: switchClassName,
ref: 'refSwitchNode',
}
return ( return (
<span <span {...spanProps}>
{...restProps}
class={switchClassName}
tabIndex={switchTabIndex}
ref='refSwitchNode'
onKeydown={this.handleKeyDown}
onClick={this.toggle}
onMouseup={this.handleMouseUp}
>
<span class={`${prefixCls}-inner`}> <span class={`${prefixCls}-inner`}>
{this.getChildren(childrenType)} {checked ? getComponentFromProp(this, 'checkedChildren') : getComponentFromProp(this, 'unCheckedChildren')}
</span> </span>
</span> </span>
) )