1
1
<template >
2
2
<label :class =" classList" >
3
- <input
4
- :id =" id"
5
- :name =" name"
6
- :type =" type"
7
- :checked =" isChecked"
8
- :disabled =" disabled"
9
- :required =" required"
10
- :value =" value"
11
- class =" switch-input form-check-input"
12
- @change =" toggle"
3
+ <input v-bind = " $attrs "
4
+ :id =" id"
5
+ :name =" name"
6
+ :type =" type"
7
+ :checked =" isChecked"
8
+ :disabled =" disabled"
9
+ :required =" required"
10
+ :value =" value"
11
+ class =" switch-input form-check-input"
12
+ @change =" toggle"
13
13
>
14
14
<span
15
15
:data-checked =" dataOn"
23
23
<script >
24
24
export default {
25
25
name: ' CSwitch' ,
26
+ inheritAttrs: false ,
26
27
model: {
27
- prop: ' passedValue ' ,
28
+ prop: ' checked ' ,
28
29
event : ' change'
29
30
},
30
- data : function () {
31
+ data () {
31
32
return {
32
- isChecked: null ,
33
- passedValue: null
33
+ isChecked: ' '
34
34
}
35
35
},
36
36
props: {
@@ -45,68 +45,77 @@ export default {
45
45
},
46
46
size: {
47
47
type: String ,
48
- validator : value => [ ' lg' , ' sm' ].indexOf (value) !== - 1
48
+ validator : value => [' ' , ' lg' , ' sm' ].indexOf (value) !== - 1
49
49
},
50
50
shape: {
51
51
type: String ,
52
- validator : value => [ ' 3d' , ' pill' ].indexOf (value) !== - 1
52
+ validator : value => [' ' , ' 3d' , ' pill' ].indexOf (value) !== - 1
53
53
},
54
54
id: String ,
55
55
name: String ,
56
- checked: Boolean ,
56
+ checked: [ Boolean , String , Number ] ,
57
57
disabled: Boolean ,
58
58
required: Boolean ,
59
- value: String ,
60
- trueValue: [String , Number , Array , Object ],
61
- falseValue: [String , Number , Array , Object ],
59
+ value: {
60
+ type: [String , Number , Boolean ],
61
+ default: null
62
+ },
63
+ trueValue: {
64
+ type: [String , Number , Boolean ],
65
+ default: null
66
+ },
67
+ falseValue: {
68
+ type: [String , Number , Boolean ],
69
+ default: null
70
+ },
62
71
dataOn: String ,
63
72
dataOff: String ,
64
73
type: {
65
74
type: String ,
66
75
default: ' checkbox'
67
76
}
68
77
},
78
+ created () {
79
+ this .isChecked = this .getCheckState ()
80
+ },
81
+ watch: {
82
+ checked (val , oldVal ) {
83
+ if (val !== oldVal)
84
+ this .isChecked = this .getCheckState ()
85
+ }
86
+ },
69
87
computed: {
70
88
classList () {
71
89
return [
72
90
' switch' ,
73
-
74
91
this .dataOn || this .dataOff ? ' switch-label' : ' ' ,
75
92
this .size ? ` switch-${ this .size } ` : ' ' ,
76
93
this .shape ? ` switch-${ this .shape } ` : ' ' ,
77
94
` switch${ this .outline ? ' -outline' : ' ' } -${ this .variant }${ this .outline === ' alt' ? ' -alt' : ' ' } ` ,
78
95
' form-check-label'
79
96
]
80
- }
97
+ },
81
98
},
82
99
methods: {
100
+ getCheckState () {
101
+ if (this .type === ' radio' )
102
+ return this .checked === this .value
103
+ else
104
+ return typeof this .checked === ' boolean' ? this .checked :
105
+ this .checked === this .trueValue ? true : false
106
+ },
83
107
toggle (event ) {
84
- this .setValues ( event .target .checked )
85
- this .$emit (' change' , this .passedValue , event );
108
+ this .isChecked = event .target .checked
109
+ this .$emit (' change' , this .getValue ( event ) , event );
86
110
},
87
- setValues (checked ) {
88
- this .isChecked = checked
89
- if (checked)
90
- this .passedValue = this .trueValue !== undefined ? this .trueValue : checked
111
+ getValue (e ) {
112
+ if (this .type === ' radio' )
113
+ return this .value
114
+ else if (e .target .checked )
115
+ return this .trueValue !== null ? this .trueValue : true
91
116
else
92
- this . passedValue = this .falseValue !== undefined ? this .falseValue : checked
117
+ return this .falseValue !== null ? this .falseValue : false
93
118
},
94
- detectPassedCheck (modelValue ) {
95
- if (typeof modelValue === ' boolean' )
96
- this .isChecked = modelValue
97
- else if (modelValue === this .falseValue )
98
- this .isChecked = false
99
- else if (modelValue === this .trueValue )
100
- this .isChecked = true
101
- else if (this .type === ' checkbox' )
102
- console .warn (' Value passed to CSwitch component by v-model property is not of boolean type and does not equal trueValue or falseValue property.' )
103
- }
104
119
},
105
- created () {
106
- if (this .$vnode .data .model )
107
- this .detectPassedCheck (this .$vnode .data .model .value )
108
- else
109
- this .isChecked = this .checked
110
- }
111
120
}
112
121
</script >
0 commit comments