Skip to content

Commit f56cff5

Browse files
committed
refactor: CFormRadio, CSwitch: change radio checked prop mechanism
1 parent 69cf317 commit f56cff5

File tree

4 files changed

+12
-45
lines changed

4 files changed

+12
-45
lines changed

src/components/Form/CFormCheckbox.vue

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ export default {
7575
watch: {
7676
checked: {
7777
immediate: true,
78-
handler () {
79-
this.state = this.getCheckState()
78+
handler (val) {
79+
this.state = val
8080
}
8181
}
8282
},
@@ -124,19 +124,10 @@ export default {
124124
// }
125125
},
126126
methods: {
127-
getCheckState () {
128-
return this.checked
129-
},
130-
131127
onChange (e) {
132128
this.state = e.target.checked
133-
this.$emit('update:checked', this.getValue(e), e)
134-
},
135-
136-
getValue (e) {
137-
return e.target.checked
129+
this.$emit('update:checked', e.target.checked, e)
138130
}
139-
140131
},
141132
}
142133
</script>

src/components/Form/CFormRadio.vue

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
<script>
22
import CFormCheckbox from './CFormCheckbox'
3-
import { formRadioProps as props } from './formProps'
43
54
export default {
65
name: 'CFormRadio',
7-
extends: Object.assign({}, CFormCheckbox, { props }),
8-
type: 'radio',
9-
methods: {
10-
getCheckState () {
11-
return this.checked === this.value
12-
},
13-
getValue () {
14-
return this.value
15-
}
16-
}
6+
extends: CFormCheckbox,
7+
type: 'radio'
178
}
189
</script>

src/components/Form/formProps.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export const formSelectProps = Object.assign(
7575
})
7676

7777
// Html props: id, disabled, required
78-
export const formRadioProps = Object.assign(
78+
export const formCheckboxProps = Object.assign(
7979
{}, formGroupAlwaysSharedProps, universalProps, {
80-
checked: [Boolean, String, Number],
80+
checked: Boolean,
8181
value: {
8282
type: [String, Number, Boolean],
8383
default: undefined
@@ -86,8 +86,3 @@ export const formRadioProps = Object.assign(
8686
inline: Boolean
8787
})
8888

89-
// Html props: id, disabled, required
90-
export const formCheckboxProps = Object.assign(
91-
{}, formRadioProps, {
92-
checked: Boolean
93-
})

src/components/Switch/CSwitch.vue

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export default {
5757
watch: {
5858
checked: {
5959
immediate: true,
60-
handler () {
61-
this.state = this.getCheckState()
60+
handler (val) {
61+
this.state = val
6262
}
6363
}
6464
},
@@ -78,19 +78,9 @@ export default {
7878
}
7979
},
8080
methods: {
81-
getCheckState () {
82-
if (this.type === 'radio') {
83-
return this.checked === this.value
84-
} else {
85-
return this.checked
86-
}
87-
},
88-
onChange (event) {
89-
this.state = event.target.checked
90-
this.$emit('update:checked', this.getValue(event.target.checked), event)
91-
},
92-
getValue (checked) {
93-
return this.type === 'radio' ? this.value : checked
81+
onChange (e) {
82+
this.state = e.target.checked
83+
this.$emit('update:checked', e.target.checked, e)
9484
}
9585
}
9686
}

0 commit comments

Comments
 (0)