Skip to content

Commit c528eea

Browse files
committed
fix(radio): radio group support v-model when using group
1 parent f6d4cf6 commit c528eea

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/components/radio/radio-group.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
2121
export default {
2222
name: COMPONENT_NAME,
23+
provide() {
24+
return {
25+
radioGroup: this
26+
}
27+
},
2328
props: {
2429
value: [String, Number],
2530
options: {

src/components/radio/radio.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const EVENT_INPUT = 'input'
1818
1919
export default {
2020
name: COMPONENT_NAME,
21+
inject: {
22+
radioGroup: {
23+
default: null
24+
}
25+
},
2126
props: {
2227
value: [String, Number],
2328
option: {
@@ -38,6 +43,21 @@ export default {
3843
radioValue: this.value
3944
}
4045
},
46+
created() {
47+
const radioGroup = this.radioGroup
48+
if (radioGroup) {
49+
this.radioValue = radioGroup.radioValue
50+
this._cancelWatchGroup = this.$watch(() => {
51+
return radioGroup.radioValue
52+
}, (newValue) => {
53+
this.radioValue = newValue
54+
})
55+
}
56+
},
57+
beforeDestroy() {
58+
this._cancelWatchGroup && this._cancelWatchGroup()
59+
this._cancelWatchGroup = null
60+
},
4161
watch: {
4262
value(newV) {
4363
this.radioValue = newV
@@ -47,6 +67,9 @@ export default {
4767
newV = Number(newV)
4868
}
4969
this.$emit(EVENT_INPUT, newV)
70+
if (this.radioGroup) {
71+
this.radioGroup.radioValue = newV
72+
}
5073
}
5174
},
5275
computed: {

0 commit comments

Comments
 (0)