iview/src/components/radio/radio.vue

133 lines
3.9 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"
2017-09-19 16:45:02 +08:00
:name="name"
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-08-24 17:50:30 +08:00
import { findComponentUpward, oneOf } 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: {
type: [String, Number, Boolean],
default: false
},
trueValue: {
type: [String, Number, Boolean],
default: true
},
falseValue: {
type: [String, Number, Boolean],
2016-09-09 14:29:19 +08:00
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
2017-08-24 17:50:30 +08:00
},
size: {
validator (value) {
return oneOf(value, ['small', 'large', 'default']);
}
2017-09-19 16:45:02 +08:00
},
name: {
type: String
2016-09-09 14:29:19 +08:00
}
},
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,
2017-08-24 17:50:30 +08:00
[`${prefixCls}-wrapper-disabled`]: this.disabled,
[`${prefixCls}-${this.size}`]: !!this.size
2016-09-09 14:29:19 +08:00
}
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();
2017-04-27 18:04:51 +08:00
} else {
this.parent.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;
let value = checked ? this.trueValue : this.falseValue;
this.$emit('input', value);
2016-09-09 14:29:19 +08:00
2017-03-27 10:48:18 +08:00
if (this.group && this.label !== undefined) {
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', value);
this.dispatch('FormItem', 'on-form-change', value);
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 === this.trueValue;
2016-09-09 14:29:19 +08:00
}
},
watch: {
value (val) {
if (val !== this.trueValue && val !== this.falseValue) {
throw 'Value should be trueValue or falseValue.';
}
2017-03-01 17:01:22 +08:00
this.updateValue();
2016-09-09 14:29:19 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>