iview/src/components/radio/radio.vue

108 lines
3 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
<label :class="wrapClasses">
<span :class="radioClasses">
<span :class="innerClasses"></span>
<input
type="radio"
:class="inputClasses"
:disabled="disabled"
2017-03-01 17:01:22 +08:00
:checked="currentValue"
2016-09-09 14:29:19 +08:00
@change="change">
2017-03-01 17:01:22 +08:00
</span><slot>{{ label }}</slot>
2016-09-09 14:29:19 +08:00
</label>
</template>
<script>
2017-03-15 17:44:19 +08:00
import { findComponentUpward } from '../../utils/assist';
import Emitter from '../../mixins/emitter';
2016-09-09 14:29:19 +08:00
const prefixCls = 'ivu-radio';
export default {
2017-03-01 17:01:22 +08:00
name: 'Radio',
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: Boolean,
default: false
},
2017-03-01 17:01:22 +08:00
label: {
type: [String, Number]
},
2016-09-09 14:29:19 +08:00
disabled: {
type: Boolean,
default: false
}
},
data () {
return {
2017-03-01 17:01:22 +08:00
currentValue: this.value,
2017-03-15 17:44:19 +08:00
group: false,
parent: findComponentUpward(this, 'RadioGroup')
2016-12-25 22:49:42 +08:00
};
2016-09-09 14:29:19 +08:00
},
computed: {
wrapClasses () {
return [
`${prefixCls}-wrapper`,
{
[`${prefixCls}-group-item`]: this.group,
2017-03-01 17:01:22 +08:00
[`${prefixCls}-wrapper-checked`]: this.currentValue,
2016-09-09 14:29:19 +08:00
[`${prefixCls}-wrapper-disabled`]: this.disabled
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
radioClasses () {
return [
`${prefixCls}`,
{
2017-03-01 17:01:22 +08:00
[`${prefixCls}-checked`]: this.currentValue,
2016-09-09 14:29:19 +08:00
[`${prefixCls}-disabled`]: this.disabled
}
2016-12-25 22:49:42 +08:00
];
2016-09-09 14:29:19 +08:00
},
innerClasses () {
return `${prefixCls}-inner`;
},
inputClasses () {
return `${prefixCls}-input`;
}
},
2017-03-01 17:01:22 +08:00
mounted () {
2017-03-15 17:44:19 +08:00
if (this.parent) this.group = true;
2016-09-09 14:29:19 +08:00
if (!this.group) {
2017-03-01 17:01:22 +08:00
this.updateValue();
2016-09-09 14:29:19 +08:00
}
},
methods: {
change (event) {
if (this.disabled) {
return false;
}
2017-03-01 17:01:22 +08:00
const checked = event.target.checked;
this.currentValue = checked;
this.$emit('input', checked);
2016-09-09 14:29:19 +08:00
2017-03-01 17:01:22 +08:00
if (this.group && this.label) {
2017-03-15 17:44:19 +08:00
this.parent.change({
2017-03-01 17:01:22 +08:00
value: this.label,
checked: this.value
2016-09-09 14:29:19 +08:00
});
}
2017-03-01 20:33:07 +08:00
if (!this.group) {
this.$emit('on-change', checked);
this.dispatch('FormItem', 'on-form-change', checked);
2017-03-01 20:33:07 +08:00
}
2016-09-09 14:29:19 +08:00
},
2017-03-01 17:01:22 +08:00
updateValue () {
this.currentValue = this.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>