Switch: update change event triggering mechanism (#1002)

pull/1008/head
杨奕 2016-11-13 11:45:53 +08:00 committed by FuryBean
parent f138cbfec8
commit d7fd700c54
3 changed files with 63 additions and 15 deletions

View File

@ -1,15 +1,20 @@
<template> <template>
<div class="el-switch" :class="{ 'is-disabled': disabled, 'el-switch--wide': hasText }"> <label class="el-switch" :class="{ 'is-disabled': disabled, 'el-switch--wide': hasText }">
<div class="el-switch__mask" v-show="disabled"></div> <div class="el-switch__mask" v-show="disabled"></div>
<input class="el-switch__input" type="checkbox" :checked="value" :name="name" :disabled="disabled" style="display: none;"> <input
<span class="el-switch__core" ref="core" @click="handleMiscClick" :style="{ 'width': coreWidth + 'px' }"> class="el-switch__input"
type="checkbox"
@change="handleChange"
v-model="currentValue"
:name="name"
:disabled="disabled">
<span class="el-switch__core" ref="core" :style="{ 'width': coreWidth + 'px' }">
<span class="el-switch__button" :style="buttonStyle"></span> <span class="el-switch__button" :style="buttonStyle"></span>
</span> </span>
<transition name="label-fade"> <transition name="label-fade">
<div <div
class="el-switch__label el-switch__label--left" class="el-switch__label el-switch__label--left"
v-show="value" v-show="value"
@click="handleMiscClick"
:style="{ 'width': coreWidth + 'px' }"> :style="{ 'width': coreWidth + 'px' }">
<i :class="[onIconClass]" v-if="onIconClass"></i> <i :class="[onIconClass]" v-if="onIconClass"></i>
<span v-if="!onIconClass && onText">{{ onText }}</span> <span v-if="!onIconClass && onText">{{ onText }}</span>
@ -19,13 +24,12 @@
<div <div
class="el-switch__label el-switch__label--right" class="el-switch__label el-switch__label--right"
v-show="!value" v-show="!value"
@click="handleMiscClick"
:style="{ 'width': coreWidth + 'px' }"> :style="{ 'width': coreWidth + 'px' }">
<i :class="[offIconClass]" v-if="offIconClass"></i> <i :class="[offIconClass]" v-if="offIconClass"></i>
<span v-if="!offIconClass && offText">{{ offText }}</span> <span v-if="!offIconClass && offText">{{ offText }}</span>
</div> </div>
</transition> </transition>
</div> </label>
</template> </template>
<script> <script>
@ -75,6 +79,7 @@
}, },
data() { data() {
return { return {
currentValue: this.value,
coreWidth: this.width, coreWidth: this.width,
buttonStyle: {} buttonStyle: {}
}; };
@ -87,18 +92,19 @@
}, },
watch: { watch: {
value(val) { value(val) {
this.currentValue = val;
if (this.onColor || this.offColor) { if (this.onColor || this.offColor) {
this.handleCoreColor(); this.handleCoreColor();
} }
this.handleButtonTransform(); this.handleButtonTransform();
this.$emit('change', val); },
currentValue(val) {
this.$emit('input', val);
} }
}, },
methods: { methods: {
handleMiscClick() { handleChange(event) {
if (!this.disabled) { this.$emit('change', event.currentTarget.checked);
this.$emit('input', !this.value);
}
}, },
handleButtonTransform() { handleButtonTransform() {
this.buttonStyle.transform = this.value ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)'; this.buttonStyle.transform = this.value ? `translate(${ this.coreWidth - 20 }px, 2px)` : 'translate(2px, 2px)';

View File

@ -47,6 +47,7 @@
} }
@e input { @e input {
display: none;
&:checked + .el-switch__core { &:checked + .el-switch__core {
border-color: var(--switch-on-color); border-color: var(--switch-on-color);
background-color: var(--switch-on-color); background-color: var(--switch-on-color);

View File

@ -50,12 +50,53 @@ describe('Switch', () => {
const core = vm.$el.querySelector('.el-switch__core'); const core = vm.$el.querySelector('.el-switch__core');
core.click(); core.click();
Vue.nextTick(() => { setTimeout(() => {
expect(vm.value).to.equal(false); expect(vm.value).to.equal(false);
core.click(); core.click();
expect(vm.value).to.equal(true); setTimeout(() => {
done(); expect(vm.value).to.equal(true);
}); done();
}, 10);
}, 10);
});
it('change event', done => {
vm = createVue({
template: `
<div>
<el-switch
v-model="value"
@change="handleChange">
</el-switch>
</div>
`,
mounted() {
setTimeout(() => {
this.value = false;
}, 10);
},
methods: {
handleChange(val) {
this.target = val;
}
},
data() {
return {
target: 1,
value: true
};
}
}, true);
setTimeout(() => {
const core = vm.$el.querySelector('.el-switch__core');
expect(vm.target).to.equal(1);
core.click();
setTimeout(() => {
expect(vm.target).to.equal(true);
done();
}, 10);
}, 50);
}); });
it('disabled switch should not respond to user click', done => { it('disabled switch should not respond to user click', done => {