ant-design-vue/components/radio/Group.vue

90 lines
1.9 KiB
Vue
Raw Normal View History

2017-10-27 06:04:48 +00:00
<template>
2017-11-07 03:58:47 +00:00
<div :class="classes">
<Radio v-for="item in radioOptions" :key="item.value"
:value="item.value" :disabled="item.disabled" :name="name">{{item.label}}</Radio>
<slot v-if="radioOptions.length === 0"></slot>
2017-10-27 06:04:48 +00:00
</div>
</template>
<script>
import Radio from './Radio.vue'
export default {
name: 'RadioGroup',
props: {
prefixCls: {
default: 'ant-radio-group',
type: String,
},
2017-11-02 04:07:20 +00:00
defaultValue: {
default: undefined,
type: [String, Number],
},
value: {
default: undefined,
type: [String, Number],
},
2017-10-27 06:04:48 +00:00
size: {
default: 'default',
validator (value) {
return ['large', 'default', 'small'].includes(value)
},
},
options: {
default: () => [],
type: Array,
},
2017-11-07 03:58:47 +00:00
disabled: Boolean,
name: String,
2017-10-27 06:04:48 +00:00
},
data () {
2017-11-02 04:07:20 +00:00
const { value, defaultValue } = this
2017-10-27 06:04:48 +00:00
return {
2017-11-02 04:07:20 +00:00
stateValue: value || defaultValue,
2017-10-27 06:04:48 +00:00
}
},
model: {
prop: 'value',
},
2017-11-07 03:58:47 +00:00
provide () {
return {
radioGroupContext: this,
}
2017-10-27 06:04:48 +00:00
},
2017-11-07 03:58:47 +00:00
computed: {
radioOptions () {
const { disabled } = this
return this.options.map(option => {
return typeof option === 'string'
? { label: option, value: option }
: { ...option, disabled: option.disabled === undefined ? disabled : option.disabled }
})
},
classes () {
const { prefixCls, size } = this
return {
[`${prefixCls}`]: true,
[`${prefixCls}-${size}`]: size,
}
},
2017-10-27 06:04:48 +00:00
},
methods: {
handleChange (event) {
const target = event.target
2017-11-02 04:07:20 +00:00
const { value: targetValue } = target
if (this.value === undefined) {
this.stateValue = targetValue
}
this.$emit('input', targetValue)
2017-11-03 10:46:18 +00:00
this.$emit('change', event)
2017-10-27 06:04:48 +00:00
},
},
watch: {
value (val) {
2017-11-02 04:07:20 +00:00
this.stateValue = val
2017-10-27 06:04:48 +00:00
},
},
components: {
Radio,
},
}
</script>