element/packages/switch/src/component.vue

158 lines
4.4 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-10-18 10:31:03 +00:00
:true-value="trueValue"
:false-value="falseValue"
: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' : '']"
2017-10-18 10:31:03 +00:00
v-if="falseIconClass || falseText">
<i :class="[falseIconClass]" v-if="falseIconClass"></i>
<span v-if="!falseIconClass && falseText" :aria-hidden="checked">{{ falseText }}</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' : '']"
2017-10-18 10:31:03 +00:00
v-if="trueIconClass || trueText">
<i :class="[trueIconClass]" v-if="trueIconClass"></i>
<span v-if="!trueIconClass && trueText" :aria-hidden="!checked">{{ trueText }}</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';
2017-10-18 10:31:03 +00:00
import Migrating from 'element-ui/src/mixins/migrating';
2016-07-27 06:15:02 +00:00
export default {
name: 'ElSwitch',
2017-10-18 10:31:03 +00:00
mixins: [Focus('input'), Migrating],
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
},
2017-10-18 10:31:03 +00:00
trueIconClass: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-18 10:31:03 +00:00
falseIconClass: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-18 10:31:03 +00:00
trueText: String,
falseText: String,
trueColor: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-18 10:31:03 +00:00
falseColor: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-18 10:31:03 +00:00
trueValue: {
2017-04-23 06:43:50 +00:00
type: [Boolean, String, Number],
2017-04-23 06:28:29 +00:00
default: true
},
2017-10-18 10:31:03 +00:00
falseValue: {
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() {
2017-10-18 10:31:03 +00:00
if (!~[this.trueValue, this.falseValue].indexOf(this.value)) {
this.$emit('input', this.falseValue);
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() {
2017-10-18 10:31:03 +00:00
return this.value === this.trueValue;
2017-04-23 06:43:50 +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: {
checked() {
2017-08-01 06:57:07 +00:00
this.$refs.input.checked = this.checked;
2017-10-18 10:31:03 +00:00
if (this.trueColor || this.falseColor) {
2016-11-18 08:32:00 +00:00
this.setBackgroundColor();
2016-07-27 06:15:02 +00:00
}
}
},
methods: {
handleChange(event) {
2017-10-18 10:31:03 +00:00
this.$emit('input', !this.checked ? this.trueValue : this.falseValue);
this.$emit('change', !this.checked ? this.trueValue : this.falseValue);
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-10-18 10:31:03 +00:00
let newColor = this.checked ? this.trueColor : this.falseColor;
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();
2017-10-18 10:31:03 +00:00
},
getMigratingConfig() {
return {
props: {
'on-color': 'on-color is renamed to true-color.',
'off-color': 'off-color is renamed to false-color.',
'on-text': 'on-text is renamed to true-text.',
'off-text': 'off-text is renamed to false-text.',
'on-value': 'on-value is renamed to true-value.',
'off-value': 'off-value is renamed to false-value.',
'on-icon-class': 'on-icon-class is renamed to true-icon-class.',
'off-icon-class': 'off-icon-class is renamed to false-icon-class.'
}
};
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;
2017-10-18 10:31:03 +00:00
if (this.trueColor || this.falseColor) {
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>