forked from N3-components/N3-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n3Switch.vue
83 lines (79 loc) · 1.98 KB
/
n3Switch.vue
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<template>
<div class="inline">
<div :class="classObj" @click="toggle">
<div class="{{prefixCls}}-switch-container {{prefixCls}}-switch-on-primary {{prefixCls}}-switch-off-default">
<span class="{{prefixCls}}-switch-handle-on {{prefixCls}}-switch-primary" >{{ontext}}</span>
<span class="{{prefixCls}}-switch-label" > </span>
<span class="{{prefixCls}}-switch-handle-off {{prefixCls}}-switch-default" >{{offtext}}</span>
</div>
</div>
<validate
:name="name"
:rules="rules"
:valid-status.sync="validStatus"
:custom-validate="customValidate"
:value="value"
:results.sync="validateResults">
</validate>
<div>
</template>
<script>
import type from './utils/type'
import valMixin from './valMixin'
import validate from './validate'
export default {
mixins: [valMixin],
props: {
value: {
type: Boolean,
twoway: true
},
disabled: {
type: Boolean,
default: false
},
ontext: {
type: String,
default: 'ON'
},
offtext: {
type: String,
default: 'OFF'
},
onChange: {
type: Function
},
prefixCls: {
type: String,
default: 'n3'
}
},
components: {
validate
},
computed: {
classObj () {
let {prefixCls, value, disabled} = this
let klass = {}
klass[prefixCls + '-switch'] = true
klass[prefixCls + '-switch-wrapper'] = true
klass[prefixCls + '-switch-on'] = value
klass[prefixCls + '-switch-off'] = !value
klass[prefixCls + '-switch-id-switch-check-on'] = value && disabled
klass[prefixCls + '-switch-id-switch-check-off'] = !(value && disabled)
klass[prefixCls + '-switch-animate'] = true
klass[prefixCls + '-switch-id-switch-check-disabled'] = disabled
return klass
}
},
methods: {
toggle () {
if (this.disabled) return
this.value = !this.value
if (type.isFunction(this.onChange)) {
this.onChange(this.value)
}
}
}
}
</script>