iview/src/components/switch/switch.vue

73 lines
2 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<span :class="wrapClasses" @click="toggle">
<span :class="innerClasses">
2017-03-02 11:19:00 +08:00
<slot name="open" v-if="currentValue"></slot>
<slot name="close" v-if="!currentValue"></slot>
2016-09-09 14:29:19 +08:00
</span>
</span>
</template>
<script>
import { oneOf } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
2016-09-09 14:29:19 +08:00
const prefixCls = 'ivu-switch';
export default {
name: 'Switch',
mixins: [ Emitter ],
2016-09-09 14:29:19 +08:00
props: {
2017-03-02 11:19:00 +08:00
value: {
2016-09-09 14:29:19 +08:00
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
size: {
validator (value) {
2017-08-25 10:53:52 +08:00
return oneOf(value, ['large', 'small', 'default']);
2016-09-09 14:29:19 +08:00
}
}
},
2017-03-02 11:19:00 +08:00
data () {
return {
currentValue: this.value
2017-03-02 11:50:02 +08:00
};
2017-03-02 11:19:00 +08:00
},
2016-09-09 14:29:19 +08:00
computed: {
wrapClasses () {
return [
`${prefixCls}`,
{
2017-03-02 11:19:00 +08:00
[`${prefixCls}-checked`]: this.currentValue,
2016-09-09 14:29:19 +08:00
[`${prefixCls}-disabled`]: this.disabled,
[`${prefixCls}-${this.size}`]: !!this.size
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
innerClasses () {
return `${prefixCls}-inner`;
}
},
methods: {
toggle () {
if (this.disabled) {
return false;
}
2017-03-02 11:19:00 +08:00
const checked = !this.currentValue;
this.currentValue = checked;
this.$emit('input', checked);
this.$emit('on-change', checked);
this.dispatch('FormItem', 'on-form-change', checked);
2017-03-02 11:19:00 +08:00
}
},
watch: {
value (val) {
this.currentValue = val;
2016-09-09 14:29:19 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>