Switch add new prop: beforeChange

This commit is contained in:
梁灏 2019-09-05 14:22:07 +08:00
parent 485db11ee2
commit ae2031aea1
2 changed files with 38 additions and 8 deletions

View file

@ -1,6 +1,6 @@
<template> <template>
<div> <div>
<i-switch v-model="m1" :loading="loading"> <i-switch v-model="m1" :loading="loading" @on-change="change" :before-change="beforeChange">
<span slot="open"></span> <span slot="open"></span>
<span slot="close"></span> <span slot="close"></span>
</i-switch> </i-switch>
@ -49,6 +49,20 @@
methods: { methods: {
change (status) { change (status) {
console.log(status) console.log(status)
},
beforeChange () {
return new Promise((resolve, reject) => {
this.$Modal.confirm({
title: '切换确认',
content: '您确认要切换开关状态吗?',
onOk: () => {
resolve();
},
onCancel: () => {
reject();
}
});
});
} }
} }
} }

View file

@ -59,7 +59,8 @@
}, },
falseColor: { falseColor: {
type: String type: String
} },
beforeChange: Function
}, },
data () { data () {
return { return {
@ -96,18 +97,33 @@
} }
}, },
methods: { methods: {
toggle (event) { handleToggle () {
event.preventDefault();
if (this.disabled || this.loading) {
return false;
}
const checked = this.currentValue === this.trueValue ? this.falseValue : this.trueValue; const checked = this.currentValue === this.trueValue ? this.falseValue : this.trueValue;
this.currentValue = checked; this.currentValue = checked;
this.$emit('input', checked); this.$emit('input', checked);
this.$emit('on-change', checked); this.$emit('on-change', checked);
this.dispatch('FormItem', 'on-form-change', checked); this.dispatch('FormItem', 'on-form-change', checked);
},
toggle (event) {
event.preventDefault();
if (this.disabled || this.loading) {
return false;
}
if (!this.beforeChange) {
return this.handleToggle();
}
const before = this.beforeChange();
if (before && before.then) {
before.then(() => {
this.handleToggle();
});
} else {
this.handleToggle();
}
} }
}, },
watch: { watch: {