Skip to content

Commit b079872

Browse files
committed
refactor: CFormCheckbox, CSwitch: delete trueValue and falseValue props
1 parent dee537a commit b079872

File tree

6 files changed

+20
-93
lines changed

6 files changed

+20
-93
lines changed

src/components/Form/CFormCheckbox.vue

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,7 @@ export default {
6464
// default: undefined
6565
// },
6666
// custom: Boolean,
67-
// inline: Boolean,
68-
69-
// trueValue: {
70-
// type: [String, Number],
71-
// default: undefined
72-
// },
73-
// falseValue: {
74-
// type: [String, Number],
75-
// default: undefined
76-
// },
67+
// inline: Boolean
7768
// },
7869
type: 'checkbox',
7970
data () {
@@ -134,11 +125,7 @@ export default {
134125
},
135126
methods: {
136127
getCheckState () {
137-
if (typeof this.checked === 'boolean') {
138-
return this.checked
139-
} else {
140-
return this.checked === this.trueValue ? true : false
141-
}
128+
return this.checked
142129
},
143130
144131
onChange (e) {
@@ -147,11 +134,7 @@ export default {
147134
},
148135
149136
getValue (e) {
150-
if (e.target.checked) {
151-
return this.trueValue !== undefined ? this.trueValue : true
152-
} else {
153-
return this.falseValue !== undefined ? this.falseValue : false
154-
}
137+
return e.target.checked
155138
}
156139
157140
},

src/components/Form/formProps.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,5 @@ export const formRadioProps = Object.assign(
8989
// Html props: id, disabled, required
9090
export const formCheckboxProps = Object.assign(
9191
{}, formRadioProps, {
92-
trueValue: {
93-
type: [String, Number],
94-
default: undefined
95-
},
96-
falseValue: {
97-
type: [String, Number],
98-
default: undefined
99-
}
92+
checked: Boolean
10093
})

src/components/Form/tests/CFormCheckbox.spec.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ const customWrapper = mount(Component, {
1313
label: 'label',
1414
id: 'some_id',
1515
wasValidated: true,
16-
checked: 'yes',
17-
trueValue: 'yes',
18-
falseValue: 'no',
16+
checked: true,
1917
value: 'value',
2018
validFeedback: 'input is valid',
2119
invalidFeedback: 'input is invalid',
@@ -40,10 +38,6 @@ describe(ComponentName, () => {
4038
it('renders correctly', () => {
4139
expect(customWrapper.element).toMatchSnapshot()
4240
})
43-
it('changes state correctly', () => {
44-
customWrapper.setProps({ checked: 'no' })
45-
expect(customWrapper.vm.state).toBe(false)
46-
})
4741
it('emmits correct values on check', () => {
4842
wrapper.find('input').setChecked()
4943
wrapper.find('input').setChecked(false)
@@ -52,14 +46,6 @@ describe(ComponentName, () => {
5246
})
5347
expect(emittedValues).toEqual([true, false])
5448
})
55-
it('emmits correct values on check when trueValue and falseValue are set', () => {
56-
customWrapper.find('input').setChecked()
57-
customWrapper.find('input').setChecked(false)
58-
const emittedValues = customWrapper.emitted()['update:checked'].map(event => {
59-
return event[0]
60-
})
61-
expect(emittedValues).toEqual(['yes', 'no'])
62-
})
6349
it('generates safe id when no id is passed', () => {
6450
expect(autoIdWrapper.vm.safeId.includes('_safe_id_')).toBe(true)
6551
})

src/components/Switch/CSwitch.vue

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<input
44
v-bind="$attrs"
55
:type="type"
6-
:checked="isChecked"
6+
:checked="state"
77
:value="value"
88
class="c-switch-input form-check-input"
9-
@change="toggle"
9+
@change="onChange"
1010
>
1111
<span
1212
:data-checked="labelOn"
@@ -36,16 +36,11 @@ export default {
3636
type: String,
3737
validator: value => ['','3d', 'pill'].includes(value)
3838
},
39-
checked: {
40-
type: [Boolean, String, Number],
41-
default: false
42-
},
39+
checked: Boolean,
4340
value: {
4441
type: [String, Number, Boolean],
4542
default: undefined
4643
},
47-
trueValue: [String, Number],
48-
falseValue: [String, Number],
4944
labelOn: String,
5045
labelOff: String,
5146
type: {
@@ -56,14 +51,14 @@ export default {
5651
},
5752
data () {
5853
return {
59-
isChecked: undefined
54+
state: undefined
6055
}
6156
},
6257
watch: {
6358
checked: {
6459
immediate: true,
6560
handler () {
66-
this.isChecked = this.getCheckState()
61+
this.state = this.getCheckState()
6762
}
6863
}
6964
},
@@ -86,24 +81,16 @@ export default {
8681
getCheckState () {
8782
if (this.type === 'radio') {
8883
return this.checked === this.value
89-
} else if (typeof this.checked === 'boolean') {
90-
return this.checked
9184
} else {
92-
return this.checked === this.trueValue ? true : false
85+
return this.checked
9386
}
9487
},
95-
toggle (event) {
96-
this.isChecked = event.target.checked
88+
onChange (event) {
89+
this.state = event.target.checked
9790
this.$emit('update:checked', this.getValue(event.target.checked), event)
9891
},
9992
getValue (checked) {
100-
if (this.type === 'radio') {
101-
return this.value
102-
} else if (checked) {
103-
return this.trueValue !== undefined ? this.trueValue : true
104-
} else {
105-
return this.falseValue !== undefined ? this.falseValue : false
106-
}
93+
return this.type === 'radio' ? this.value : checked
10794
}
10895
}
10996
}

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

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ const customWrapper = mount(Component, {
2323
shape:'3d',
2424
name:'myName',
2525
id:'myId',
26-
checked: 'yes',
26+
checked: true,
2727
value: 'checked',
2828
required: true,
29-
trueValue: 'yes',
30-
falseValue: 'no',
3129
dataOn: 'dataOn',
3230
dataOff: 'dataOff'
3331
}
@@ -43,25 +41,10 @@ describe(ComponentName, () => {
4341
it('renders correctly', () => {
4442
expect(customWrapper.element).toMatchSnapshot()
4543
})
46-
it('correctly changes state when checked prop changes', () => {
47-
customWrapper.setProps({ checked: 'no' })
48-
expect(customWrapper.vm.isChecked).toBe(false)
49-
50-
customWrapper.setProps({ checked: 'yes' })
51-
expect(customWrapper.vm.isChecked).toBe(true)
52-
})
5344
it('emits events correctly when checked', () => {
5445
const updateEvents = () => customWrapper.emitted()['update:checked']
5546
const lastEmittedUpdateValue = () => updateEvents().slice(-1)[0][0]
5647

57-
customWrapper.find('input').setChecked(false)
58-
expect(lastEmittedUpdateValue()).toBe('no')
59-
60-
customWrapper.find('input').setChecked(true)
61-
expect(lastEmittedUpdateValue()).toBe('yes')
62-
63-
customWrapper.setProps({ trueValue: undefined, falseValue: undefined})
64-
6548
customWrapper.find('input').setChecked(false)
6649
expect(lastEmittedUpdateValue()).toBe(false)
6750

src/components/index.d.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ declare class formSharedProps extends Vue {
175175
description: string
176176
}
177177

178-
export declare class CFormCheckbox extends CFormRadio {
179-
trueValue: [string, number]
180-
falseValue: [string, number]
181-
}
182-
183178
export declare class CFormFile extends formSharedProps {
184179
label: string
185180
wasValidated: boolean
@@ -232,12 +227,14 @@ export declare class CFormRadio extends formSharedProps {
232227

233228
checked: [boolean, string, number]
234229
value: [string, number, boolean]
235-
trueValue: [string, number]
236-
falseValue: [string, number]
237230
custom: boolean
238231
inline: boolean
239232
}
240233

234+
export declare class CFormCheckbox extends CFormRadio {
235+
checked: boolean
236+
}
237+
241238
export declare class CFormSelect extends formSharedProps {
242239
appendHtml: string
243240
prependHtml: string
@@ -523,10 +520,8 @@ export declare class CSwitch extends Vue {
523520
outline: [boolean, string]
524521
size: string
525522
shape: string
526-
checked: [boolean, string, number]
523+
checked: boolean
527524
value: [string, number, boolean]
528-
trueValue: [string, number]
529-
falseValue: [string, number]
530525
labelOn: string
531526
labelOff: string
532527
type: string

0 commit comments

Comments
 (0)