2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
2017-09-29 07:58:07 +00:00
|
|
|
<div
|
|
|
|
class="el-switch"
|
|
|
|
:class="{ 'is-disabled': disabled, 'is-checked': checked }"
|
|
|
|
role="switch"
|
|
|
|
:aria-checked="checked"
|
|
|
|
:aria-disabled="disabled"
|
|
|
|
@click="switchValue"
|
|
|
|
>
|
2016-11-13 03:45:53 +00:00
|
|
|
<input
|
|
|
|
class="el-switch__input"
|
|
|
|
type="checkbox"
|
|
|
|
@change="handleChange"
|
2017-05-19 17:20:04 +00:00
|
|
|
ref="input"
|
2016-11-13 03:45:53 +00:00
|
|
|
:name="name"
|
2017-04-23 06:28:29 +00:00
|
|
|
:true-value="onValue"
|
|
|
|
:false-value="offValue"
|
2017-09-29 07:58:07 +00:00
|
|
|
: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>
|
2017-09-29 07:58:07 +00:00
|
|
|
<span v-if="!offIconClass && offText" :aria-hidden="checked">{{ offText }}</span>
|
2017-09-13 07:51:01 +00:00
|
|
|
</span>
|
2016-11-13 03:45:53 +00:00
|
|
|
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
|
2017-03-28 07:06:34 +00:00
|
|
|
<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>
|
2017-09-29 07:58:07 +00:00
|
|
|
<span v-if="!onIconClass && onText" :aria-hidden="!checked">{{ onText }}</span>
|
2017-09-13 07:51:01 +00:00
|
|
|
</span>
|
2017-09-29 07:58:07 +00:00
|
|
|
</div>
|
2016-07-27 06:15:02 +00:00
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
export default {
|
2016-12-31 15:33:51 +00:00
|
|
|
name: 'ElSwitch',
|
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 {
|
2017-03-28 07:06:34 +00:00
|
|
|
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;
|
|
|
|
},
|
2017-03-28 07:06:34 +00:00
|
|
|
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: {
|
2017-05-19 17:20:04 +00:00
|
|
|
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: {
|
2016-11-13 03:45:53 +00:00
|
|
|
handleChange(event) {
|
2017-05-19 17:20:04 +00:00
|
|
|
this.$emit('input', !this.checked ? this.onValue : this.offValue);
|
2017-08-04 15:41:31 +00:00
|
|
|
this.$emit('change', !this.checked ? this.onValue : this.offValue);
|
2017-05-19 17:20:04 +00:00
|
|
|
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;
|
2017-09-29 07:58:07 +00:00
|
|
|
},
|
|
|
|
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;
|
2016-12-08 07:23:21 +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
|
|
|
}
|
2017-05-19 17:20:04 +00:00
|
|
|
this.$refs.input.checked = this.checked;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|