element/packages/switch/src/component.vue

166 lines
4.7 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div
class="el-switch"
2018-01-29 09:54:38 +00:00
:class="{ 'is-disabled': switchDisabled, 'is-checked': checked }"
role="switch"
:aria-checked="checked"
2018-01-29 09:54:38 +00:00
:aria-disabled="switchDisabled"
@click="switchValue"
>
<input
class="el-switch__input"
type="checkbox"
@change="handleChange"
ref="input"
:name="name"
2017-10-19 07:54:01 +00:00
:true-value="activeValue"
:false-value="inactiveValue"
2018-01-29 09:54:38 +00:00
:disabled="switchDisabled"
@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-19 07:54:01 +00:00
v-if="inactiveIconClass || inactiveText">
<i :class="[inactiveIconClass]" v-if="inactiveIconClass"></i>
<span v-if="!inactiveIconClass && inactiveText" :aria-hidden="checked">{{ inactiveText }}</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-19 07:54:01 +00:00
v-if="activeIconClass || activeText">
<i :class="[activeIconClass]" v-if="activeIconClass"></i>
<span v-if="!activeIconClass && activeText" :aria-hidden="!checked">{{ activeText }}</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],
2018-01-29 09:54:38 +00:00
inject: {
elForm: {
default: ''
}
},
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-19 07:54:01 +00:00
activeIconClass: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-19 07:54:01 +00:00
inactiveIconClass: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-19 07:54:01 +00:00
activeText: String,
inactiveText: String,
activeColor: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-19 07:54:01 +00:00
inactiveColor: {
2016-07-27 06:15:02 +00:00
type: String,
default: ''
},
2017-10-19 07:54:01 +00:00
activeValue: {
2017-04-23 06:43:50 +00:00
type: [Boolean, String, Number],
2017-04-23 06:28:29 +00:00
default: true
},
2017-10-19 07:54:01 +00:00
inactiveValue: {
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-19 07:54:01 +00:00
if (!~[this.activeValue, this.inactiveValue].indexOf(this.value)) {
this.$emit('input', this.inactiveValue);
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-19 07:54:01 +00:00
return this.value === this.activeValue;
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)` : '';
2018-01-29 09:54:38 +00:00
},
switchDisabled() {
return this.disabled || (this.elForm || {}).disabled;
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-19 07:54:01 +00:00
if (this.activeColor || this.inactiveColor) {
2016-11-18 08:32:00 +00:00
this.setBackgroundColor();
2016-07-27 06:15:02 +00:00
}
}
},
methods: {
handleChange(event) {
2017-10-19 07:54:01 +00:00
this.$emit('input', !this.checked ? this.activeValue : this.inactiveValue);
this.$emit('change', !this.checked ? this.activeValue : this.inactiveValue);
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-19 07:54:01 +00:00
let newColor = this.checked ? this.activeColor : this.inactiveColor;
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: {
2017-10-19 07:54:01 +00:00
'on-color': 'on-color is renamed to active-color.',
'off-color': 'off-color is renamed to inactive-color.',
'on-text': 'on-text is renamed to active-text.',
'off-text': 'off-text is renamed to inactive-text.',
'on-value': 'on-value is renamed to active-value.',
'off-value': 'off-value is renamed to inactive-value.',
'on-icon-class': 'on-icon-class is renamed to active-icon-class.',
'off-icon-class': 'off-icon-class is renamed to inactive-icon-class.'
2017-10-18 10:31:03 +00:00
}
};
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-19 07:54:01 +00:00
if (this.activeColor || this.inactiveColor) {
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>