11<template >
22 <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"
1313 >
1414 <span
1515 :data-checked =" dataOn"
2323<script >
2424export default {
2525 name: ' CSwitch' ,
26+ inheritAttrs: false ,
2627 model: {
27- prop: ' passedValue ' ,
28+ prop: ' checked ' ,
2829 event : ' change'
2930 },
30- data : function () {
31+ data () {
3132 return {
32- isChecked: null ,
33- passedValue: null
33+ isChecked: ' '
3434 }
3535 },
3636 props: {
@@ -45,68 +45,77 @@ export default {
4545 },
4646 size: {
4747 type: String ,
48- validator : value => [ ' lg' , ' sm' ].indexOf (value) !== - 1
48+ validator : value => [' ' , ' lg' , ' sm' ].indexOf (value) !== - 1
4949 },
5050 shape: {
5151 type: String ,
52- validator : value => [ ' 3d' , ' pill' ].indexOf (value) !== - 1
52+ validator : value => [' ' , ' 3d' , ' pill' ].indexOf (value) !== - 1
5353 },
5454 id: String ,
5555 name: String ,
56- checked: Boolean ,
56+ checked: [ Boolean , String , Number ] ,
5757 disabled: Boolean ,
5858 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+ },
6271 dataOn: String ,
6372 dataOff: String ,
6473 type: {
6574 type: String ,
6675 default: ' checkbox'
6776 }
6877 },
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+ },
6987 computed: {
7088 classList () {
7189 return [
7290 ' switch' ,
73-
7491 this .dataOn || this .dataOff ? ' switch-label' : ' ' ,
7592 this .size ? ` switch-${ this .size } ` : ' ' ,
7693 this .shape ? ` switch-${ this .shape } ` : ' ' ,
7794 ` switch${ this .outline ? ' -outline' : ' ' } -${ this .variant }${ this .outline === ' alt' ? ' -alt' : ' ' } ` ,
7895 ' form-check-label'
7996 ]
80- }
97+ },
8198 },
8299 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+ },
83107 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 );
86110 },
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
91116 else
92- this . passedValue = this .falseValue !== undefined ? this .falseValue : checked
117+ return this .falseValue !== null ? this .falseValue : false
93118 },
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- }
104119 },
105- created () {
106- if (this .$vnode .data .model )
107- this .detectPassedCheck (this .$vnode .data .model .value )
108- else
109- this .isChecked = this .checked
110- }
111120}
112121 </script >
0 commit comments