iview/src/components/radio/radio.vue

172 lines
5.3 KiB
Vue
Raw Normal View History

2016-09-09 14:29:19 +08:00
<template>
2018-01-21 01:22:22 +01:00
<label :class="wrapClasses">
2016-09-09 14:29:19 +08:00
<span :class="radioClasses">
2018-01-21 01:22:22 +01:00
<span :class="innerClasses"></span>
2016-09-09 14:29:19 +08:00
<input
type="radio"
:class="inputClasses"
:disabled="disabled"
2017-03-01 17:01:22 +08:00
:checked="currentValue"
2018-01-20 23:57:42 +01:00
:name="groupName"
2018-01-18 13:48:22 +01:00
@change="change"
2018-01-20 23:57:42 +01:00
@focus="onFocus"
@blur="onBlur">
</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']);
2018-06-28 14:18:20 +08:00
},
default () {
2018-08-07 16:35:27 +08:00
return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
2017-08-24 17:50:30 +08:00
}
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,
2018-01-20 23:57:42 +01:00
groupName: this.name,
parent: findComponentUpward(this, 'RadioGroup'),
focusWrapper: false,
focusInner: false
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,
2018-01-20 23:57:42 +01:00
[`${prefixCls}-${this.size}`]: !!this.size,
[`${prefixCls}-focus`]: this.focusWrapper
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 () {
2018-01-20 23:57:42 +01:00
return [
`${prefixCls}-inner`,
{
[`${prefixCls}-focus`]: this.focusInner
}
];
2016-09-09 14:29:19 +08:00
},
inputClasses () {
return `${prefixCls}-input`;
}
},
2017-03-01 17:01:22 +08:00
mounted () {
2018-01-20 23:57:42 +01:00
if (this.parent) {
this.group = true;
if (this.name && this.name !== this.parent.name) {
2018-01-21 01:26:05 +01:00
/* eslint-disable no-console */
2018-01-20 23:57:42 +01:00
if (console.warn) {
console.warn('[iview] Name does not match Radio Group name.');
}
2018-01-21 01:26:05 +01:00
/* eslint-enable no-console */
2018-01-20 23:57:42 +01:00
} else {
this.groupName = this.parent.name;
}
}
if (this.group) {
2017-04-27 18:04:51 +08:00
this.parent.updateValue();
2018-01-20 23:57:42 +01:00
} else {
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;
2018-01-20 23:57:42 +01:00
const value = checked ? this.trueValue : this.falseValue;
this.$emit('input', value);
2016-09-09 14:29:19 +08:00
2018-01-20 23:57:42 +01:00
if (this.group) {
if (this.label !== undefined) {
this.parent.change({
value: this.label,
checked: this.value
});
}
} else {
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;
2018-01-20 23:57:42 +01:00
},
onBlur () {
this.focusWrapper = false;
this.focusInner = false;
},
onFocus () {
if (this.group && this.parent.type === 'button') {
this.focusWrapper = true;
} else {
this.focusInner = true;
}
2016-09-09 14:29:19 +08:00
}
},
watch: {
value (val) {
2018-01-20 23:57:42 +01:00
if (val === this.trueValue || val === this.falseValue) {
this.updateValue();
} else {
throw 'Value should be trueValue or falseValue.';
}
2016-09-09 14:29:19 +08:00
}
}
2016-12-25 22:49:42 +08:00
};
</script>