iview/src/components/radio/radio-group.vue

84 lines
2.3 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<div :class="classes">
<slot></slot>
</div>
</template>
<script>
2017-03-15 17:44:19 +08:00
import { oneOf, findComponentsDownward } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
2016-09-09 14:29:19 +08:00
const prefixCls = 'ivu-radio-group';
export default {
name: 'RadioGroup',
mixins: [ Emitter ],
2016-09-09 14:29:19 +08:00
props: {
2017-03-01 17:01:22 +08:00
value: {
2016-09-09 14:29:19 +08:00
type: [String, Number],
default: ''
},
size: {
validator (value) {
2017-08-24 17:50:30 +08:00
return oneOf(value, ['small', 'large', 'default']);
2016-09-09 14:29:19 +08:00
}
},
type: {
validator (value) {
return oneOf(value, ['button']);
}
},
vertical: {
type: Boolean,
default: false
2016-09-09 14:29:19 +08:00
}
},
2017-03-01 17:01:22 +08:00
data () {
return {
2017-03-15 17:44:19 +08:00
currentValue: this.value,
childrens: []
2017-03-01 17:58:40 +08:00
};
2017-03-01 17:01:22 +08:00
},
2016-09-09 14:29:19 +08:00
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-${this.size}`]: !!this.size,
2017-08-24 17:50:30 +08:00
[`ivu-radio-${this.size}`]: !!this.size,
[`${prefixCls}-${this.type}`]: !!this.type,
[`${prefixCls}-vertical`]: this.vertical
2016-09-09 14:29:19 +08:00
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
}
},
2017-03-01 17:01:22 +08:00
mounted () {
this.updateValue();
2016-09-09 14:29:19 +08:00
},
methods: {
2017-03-01 17:01:22 +08:00
updateValue () {
const value = this.value;
2017-03-15 17:44:19 +08:00
this.childrens = findComponentsDownward(this, 'Radio');
if (this.childrens) {
this.childrens.forEach(child => {
child.currentValue = value == child.label;
child.group = true;
});
}
2016-09-09 14:29:19 +08:00
},
change (data) {
2017-03-01 17:01:22 +08:00
this.currentValue = data.value;
this.updateValue();
this.$emit('input', data.value);
2016-09-09 14:29:19 +08:00
this.$emit('on-change', data.value);
this.dispatch('FormItem', 'on-form-change', data.value);
2016-09-09 14:29:19 +08:00
}
},
watch: {
2017-03-01 17:01:22 +08:00
value () {
this.updateValue();
2016-09-09 14:29:19 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>