element/packages/switch/src/component.vue

143 lines
3.6 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div
class="el-switch"
:class="{ 'is-disabled': disabled, 'is-checked': checked }"
role="switch"
:aria-checked="checked"
:aria-disabled="disabled"
@click="switchValue"
>
<input
class="el-switch__input"
type="checkbox"
@change="handleChange"
ref="input"
:name="name"
2017-04-23 06:28:29 +00:00
:true-value="onValue"
:false-value="offValue"
:disabled="disabled"
@keydown.enter="switchValue"
>
2017-09-13 07:51:01 +00:00
<span
:class="['el-switch__label', 'el-switch__label--left', !checked ? 'is-active' : '']"
v-if="offIconClass || offText">
<i :class="[offIconClass]" v-if="offIconClass"></i>
<span v-if="!offIconClass && offText" :aria-hidden="checked">{{ offText }}</span>
2017-09-13 07:51:01 +00:00
</span>
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
<span class="el-switch__button" :style="{ transform }"></span>
2016-07-27 06:15:02 +00:00
</span>
2017-09-13 07:51:01 +00:00
<span
:class="['el-switch__label', 'el-switch__label--right', checked ? 'is-active' : '']"
v-if="onIconClass || onText">
<i :class="[onIconClass]" v-if="onIconClass"></i>
<span v-if="!onIconClass && onText" :aria-hidden="!checked">{{ onText }}</span>
2017-09-13 07:51:01 +00:00
</span>
</div>
2016-07-27 06:15:02 +00:00
</template>
<script>
import Focus from 'element-ui/src/mixins/focus';
2016-07-27 06:15:02 +00:00
export default {
name: 'ElSwitch',
mixins: [Focus('input')],
2016-07-27 06:15:02 +00:00
props: {
value: {
2017-04-23 06:43:50 +00:00
type: [Boolean, String, Number],
2017-07-10 03:59:08 +00:00
default: false
2016-07-27 06:15:02 +00:00
},
disabled: {
type: Boolean,
default: false
},
width: {
type: Number,
default: 0
},
onIconClass: {
type: String,
default: ''
},
offIconClass: {
type: String,
default: ''
},
2017-09-13 07:51:01 +00:00
onText: String,
offText: String,
2016-07-27 06:15:02 +00:00
onColor: {
type: String,
default: ''
},
offColor: {
type: String,
default: ''
},
2017-04-23 06:28:29 +00:00
onValue: {
2017-04-23 06:43:50 +00:00
type: [Boolean, String, Number],
2017-04-23 06:28:29 +00:00
default: true
},
offValue: {
2017-04-23 06:43:50 +00:00
type: [Boolean, String, Number],
2017-04-23 06:28:29 +00:00
default: false
},
2016-07-27 06:15:02 +00:00
name: {
type: String,
default: ''
}
},
2016-08-04 11:56:54 +00:00
data() {
return {
coreWidth: this.width
2016-08-04 11:56:54 +00:00
};
},
2017-04-23 06:28:29 +00:00
created() {
if (!~[this.onValue, this.offValue].indexOf(this.value)) {
2017-06-03 12:55:17 +00:00
this.$emit('input', this.offValue);
2017-04-23 06:28:29 +00:00
}
},
2016-07-27 06:15:02 +00:00
computed: {
2017-04-23 06:43:50 +00:00
checked() {
return this.value === this.onValue;
},
transform() {
2017-09-13 07:51:01 +00:00
return this.checked ? `translate3d(${ this.coreWidth - 20 }px, 0, 0)` : '';
2016-07-27 06:15:02 +00:00
}
},
watch: {
checked() {
2017-08-01 06:57:07 +00:00
this.$refs.input.checked = this.checked;
2016-07-27 06:15:02 +00:00
if (this.onColor || this.offColor) {
2016-11-18 08:32:00 +00:00
this.setBackgroundColor();
2016-07-27 06:15:02 +00:00
}
}
},
methods: {
handleChange(event) {
this.$emit('input', !this.checked ? this.onValue : this.offValue);
this.$emit('change', !this.checked ? this.onValue : this.offValue);
this.$nextTick(() => {
// set input's checked property
// in case parent refuses to change component's value
this.$refs.input.checked = this.checked;
});
2016-07-27 06:15:02 +00:00
},
2016-11-18 08:32:00 +00:00
setBackgroundColor() {
2017-04-23 06:43:50 +00:00
let newColor = this.checked ? this.onColor : this.offColor;
2016-11-18 08:32:00 +00:00
this.$refs.core.style.borderColor = newColor;
this.$refs.core.style.backgroundColor = newColor;
},
switchValue() {
this.$refs.input.click();
2016-07-27 06:15:02 +00:00
}
},
2016-08-04 11:56:54 +00:00
mounted() {
2016-10-19 06:53:32 +00:00
/* istanbul ignore if */
2017-09-13 07:51:01 +00:00
this.coreWidth = this.width || 40;
if (this.onColor || this.offColor) {
2016-11-18 08:32:00 +00:00
this.setBackgroundColor();
2016-07-27 06:15:02 +00:00
}
this.$refs.input.checked = this.checked;
2016-07-27 06:15:02 +00:00
}
};
</script>