switch.vue
1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<template name="zan-switch">
<div
class="zan-switch"
:class="computedClassStr"
:data-checked="checked"
:data-loading="loading"
:data-disabled="disabled"
:data-component-id="componentId"
:style="styles || ''"
@click="_handleZanSwitchChange"
>
<div class="zan-switch__circle">
<div :hidden="!loading" class="zan-switch__loading"></div>
</div>
<div class="zan-switch__bg"></div>
</div>
</template>
<script>
export default {
props: [
'checked',
'disabled',
'loading',
'componentId',
'handleZanSwitchChange',
'styles'
],
computed: {
computedClassStr: function () {
return 'zan-switch--' + (this.checked ? 'on' : 'off') + ' ' + (this.disabled ? 'zan-swtich--disabled' : '')
}
},
methods: {
_handleZanSwitchChange (e) {
const dataset = e.currentTarget.dataset
console.log(dataset)
const checked = !dataset.checked
const loading = dataset.loading
const disabled = dataset.disabled
const componentId = dataset.componentId
if (loading || disabled) return
console.info('[zan:switch:change]', { checked, componentId })
if (this.handleZanSwitchChange) {
this.handleZanSwitchChange({
checked,
componentId
})
} else {
console.warn('页面缺少 handleZanSwitchChange 回调函数')
}
}
}
}
</script>