Skip to content

Commit 2f54f1e

Browse files
committed
refactor: change CSwitch logic, added watcher in case of lacking model.
1 parent fd62620 commit 2f54f1e

File tree

2 files changed

+54
-83
lines changed

2 files changed

+54
-83
lines changed

src/components/Switch/CSwitch.vue

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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"
@@ -23,14 +23,14 @@
2323
<script>
2424
export 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>

src/components/Switch/tests/__snapshots__/CSwitch.spec.js.snap

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`CSwitch .vue renders correctly 1`] = `
4-
<label
5-
class="switch switch-secondary form-check-label"
6-
>
7-
<input
8-
class="switch-input form-check-input"
9-
type="checkbox"
10-
value=""
11-
/>
12-
13-
<span
14-
class="switch-slider"
15-
/>
16-
</label>
17-
`;
18-
19-
exports[`CSwitch .vue renders correctly 2`] = `
20-
<label
21-
class="switch switch-label switch-lg switch-3d switch-outline-info-alt form-check-label"
22-
>
23-
<input
24-
class="switch-input form-check-input"
25-
disabled="disabled"
26-
id="myId"
27-
name="myName"
28-
required="required"
29-
type="radio"
30-
value="checked"
31-
/>
32-
33-
<span
34-
class="switch-slider"
35-
data-checked="dataOn"
36-
data-unchecked="dataOff"
37-
/>
38-
</label>
39-
`;
40-
413
exports[`CSwitch renders correctly 1`] = `
424
<label
435
class="switch switch-secondary form-check-label"

0 commit comments

Comments
 (0)