element/packages/radio/src/radio-button.vue

92 lines
2.2 KiB
Vue
Raw Normal View History

<template>
<label
class="el-radio-button"
:class="[
size ? 'el-radio-button--' + size : '',
{ 'is-active': value === label },
{ 'is-disabled': isDisabled }
]"
2017-07-26 13:24:23 +00:00
role="radio"
:aria-checked="value === label"
:aria-disabled="isDisabled"
:tabindex="tabIndex"
@keydown.space.stop.prevent="value = label"
>
<input
class="el-radio-button__orig-radio"
:value="label"
type="radio"
v-model="value"
:name="name"
@change="handleChange"
2017-07-26 13:24:23 +00:00
:disabled="isDisabled"
tabindex="-1"
>
<span class="el-radio-button__inner" :style="value === label ? activeStyle : null">
<slot></slot>
<template v-if="!$slots.default">{{label}}</template>
</span>
</label>
</template>
2016-07-27 06:15:02 +00:00
<script>
import Emitter from 'element-ui/src/mixins/emitter';
2016-07-27 06:15:02 +00:00
export default {
name: 'ElRadioButton',
mixins: [Emitter],
2016-07-27 06:15:02 +00:00
props: {
2016-12-02 09:35:54 +00:00
label: {},
2016-07-27 06:15:02 +00:00
disabled: Boolean,
2016-08-23 10:32:28 +00:00
name: String
},
2016-07-27 06:15:02 +00:00
computed: {
value: {
get() {
2016-12-02 09:35:54 +00:00
return this._radioGroup.value;
2016-07-27 06:15:02 +00:00
},
2016-12-02 09:35:54 +00:00
set(value) {
this._radioGroup.$emit('input', value);
2016-07-27 06:15:02 +00:00
}
2016-11-26 09:54:27 +00:00
},
2016-12-02 09:35:54 +00:00
_radioGroup() {
let parent = this.$parent;
while (parent) {
if (parent.$options.componentName !== 'ElRadioGroup') {
parent = parent.$parent;
} else {
return parent;
}
}
return false;
},
2016-11-26 09:54:27 +00:00
activeStyle() {
return {
backgroundColor: this._radioGroup.fill || '',
borderColor: this._radioGroup.fill || '',
boxShadow: this._radioGroup.fill ? `-1px 0 0 0 ${this._radioGroup.fill}` : '',
color: this._radioGroup.textColor || ''
2016-11-26 09:54:27 +00:00
};
2016-12-02 09:35:54 +00:00
},
size() {
return this._radioGroup.size;
},
isDisabled() {
return this.disabled || this._radioGroup.disabled;
2017-07-26 13:24:23 +00:00
},
tabIndex() {
return !this.isDisabled ? (this._radioGroup ? (this.value === this.label ? 0 : -1) : 0) : -1;
2016-07-27 06:15:02 +00:00
}
},
methods: {
handleChange() {
this.$nextTick(() => {
this.dispatch('ElRadioGroup', 'handleChange', this.value);
});
}
2016-07-27 06:15:02 +00:00
}
};
</script>