2017-10-27 14:04:48 +08:00
|
|
|
<template>
|
|
|
|
<div :class="`${prefixCls}`">
|
2017-11-02 12:07:20 +08:00
|
|
|
<Radio v-for="item in options" :key="item.value" :checked="item.value === stateValue"
|
2017-10-27 14:04:48 +08:00
|
|
|
:value="item.value" :disabled="item.disabled" @change="handleChange">{{item.label}}</Radio>
|
|
|
|
<slot v-if="options.length === 0"></slot>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import Radio from './Radio.vue'
|
|
|
|
export default {
|
|
|
|
name: 'RadioGroup',
|
|
|
|
props: {
|
|
|
|
prefixCls: {
|
|
|
|
default: 'ant-radio-group',
|
|
|
|
type: String,
|
|
|
|
},
|
2017-11-02 12:07:20 +08:00
|
|
|
defaultValue: {
|
|
|
|
default: undefined,
|
|
|
|
type: [String, Number],
|
|
|
|
},
|
|
|
|
value: {
|
|
|
|
default: undefined,
|
|
|
|
type: [String, Number],
|
|
|
|
},
|
2017-10-27 14:04:48 +08:00
|
|
|
size: {
|
|
|
|
default: 'default',
|
|
|
|
validator (value) {
|
|
|
|
return ['large', 'default', 'small'].includes(value)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
default: () => [],
|
|
|
|
type: Array,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data () {
|
2017-11-02 12:07:20 +08:00
|
|
|
const { value, defaultValue } = this
|
2017-10-27 14:04:48 +08:00
|
|
|
return {
|
2017-11-02 12:07:20 +08:00
|
|
|
stateValue: value || defaultValue,
|
2017-10-27 14:04:48 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
|
|
|
|
},
|
|
|
|
created () {
|
|
|
|
this.setChildRadio(this.$slots.default)
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleChange (event) {
|
|
|
|
if (this.disabled) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const target = event.target
|
2017-11-02 12:07:20 +08:00
|
|
|
const { value: targetValue } = target
|
|
|
|
if (this.value === undefined) {
|
|
|
|
this.stateValue = targetValue
|
|
|
|
}
|
|
|
|
this.$emit('input', targetValue)
|
2017-11-03 18:46:18 +08:00
|
|
|
this.$emit('change', event)
|
2017-10-27 14:04:48 +08:00
|
|
|
},
|
|
|
|
setChildRadio (children = []) {
|
2017-11-02 12:07:20 +08:00
|
|
|
const { options, $slots, stateValue } = this
|
2017-10-27 14:04:48 +08:00
|
|
|
if (options.length === 0 && $slots.default) {
|
|
|
|
children.forEach(({ componentOptions = {}, children: newChildren }) => {
|
2017-10-27 14:56:23 +08:00
|
|
|
const { Ctor, propsData } = componentOptions
|
|
|
|
if (Ctor && Ctor.options.name === 'Radio') {
|
2017-10-27 14:04:48 +08:00
|
|
|
propsData.isGroup = true
|
|
|
|
propsData.onGroupChange = this.handleChange
|
2017-11-02 12:07:20 +08:00
|
|
|
propsData.checked = propsData.value === stateValue
|
2017-10-27 14:04:48 +08:00
|
|
|
} else {
|
|
|
|
this.setChildRadio(newChildren)
|
|
|
|
}
|
|
|
|
}, this)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
beforeUpdate () {
|
|
|
|
this.setChildRadio(this.$slots.default)
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value (val) {
|
2017-11-02 12:07:20 +08:00
|
|
|
this.stateValue = val
|
2017-10-27 14:04:48 +08:00
|
|
|
this.setChildRadio(this.$slots.default)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
Radio,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
</script>
|