<template>
<view class="u-radio" :style="[radioStyle]">
<view class="u-radio__icon-wrap" @tap="toggle">
<u-icon
:class="iconClass"
name="checkbox-mark"
:size="iconSize"
:color="iconColor"
class="u-radio__icon"
:style="[iconStyle]"
/>
</view>
<view
class="u-label-class u-radio__label"
@tap="onClickLabel"
:style="{
fontSize: labelSize + 'rpx'
}"
>
<slot />
</view>
</view>
</template>
<script>
export default {
name: 'u-radio',
props: {
name: {
type: [String, Number],
default: ''
},
shape: {
type: String,
default: 'circle'
},
disabled: {
type: Boolean,
default: false
},
labelDisabled: {
type: Boolean,
default: false
},
activeColor: {
type: String,
default: ''
},
iconSize: {
type: [String, Number],
default: 14
},
labelSize: {
type: [String, Number],
default: 14
}
},
inject: ['radioGroup'],
data() {
return {
parentDisabled: false
}
},
created() {
this.parentDisabled = this.radioGroup.disabled
},
computed: {
iconStyle() {
let style = {}
if (
this.radioActiveColor &&
this.name == this.radioGroup.value &&
!this.disabled &&
!this.parentDisabled
) {
style.borderColor = this.radioActiveColor
style.backgroundColor = this.radioActiveColor
}
style.width = this.radioGroup.size + 'rpx'
style.height = this.radioGroup.size + 'rpx'
return style
},
iconColor() {
return this.name == this.radioGroup.value ? '#ffffff' : 'transparent'
},
iconClass() {
let classs = []
classs.push('u-radio__icon--' + this.shape)
if (this.name == this.radioGroup.value)
classs.push('u-radio__icon--checked')
if (this.disabled || this.parentDisabled)
classs.push('u-radio__icon--disabled')
if (
this.name == this.radioGroup.value &&
(this.disabled || this.parentDisabled)
)
classs.push('u-radio__icon--disabled--checked')
return classs
},
radioActiveColor() {
return this.activeColor ? this.activeColor : this.radioGroup.activeColor
},
radioStyle() {
let style = {}
if (this.radioGroup.width) {
style.width = this.radioGroup.width
style.float = 'left'
style.flex = `0 0 ${this.radioGroup.width}`
}
if (this.radioGroup.wrap) {
style.width = '100%'
style.flex = '0 0 100%'
}
return style
}
},
methods: {
onClickLabel() {
if (!this.disabled && !this.labelDisabled && !this.parentDisabled) {
this.radioGroup.setValue(this.name)
this.emitEvent()
}
},
toggle() {
if (!this.disabled && !this.parentDisabled) {
this.radioGroup.setValue(this.name)
this.emitEvent()
}
},
emitEvent() {
this.$emit('change', this.name)
}
}
}
</script>
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
.u-radio {
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
overflow: hidden;
-webkit-user-select: none;
user-select: none;
line-height: 1.8;
}
.u-radio__icon-wrap,
.u-radio__label {
color: $u-content-color;
}
.u-radio__icon-wrap {
-webkit-flex: none;
flex: none;
}
.u-radio__icon {
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
box-sizing: border-box;
width: 21rpx;
height: 21rpx;
color: transparent;
text-align: center;
transition-property: color, border-color, background-color;
font-size: 10px;
border: 1px solid #c8c9cc;
transition-duration: 0.2s;
}
.u-radio__icon--circle {
border-radius: 100%;
}
.u-radio__icon--square {
border-radius: 3px;
}
.u-radio__icon--checked {
color: #fff;
background-color: #2979ff;
border-color: #2979ff;
}
.u-radio__icon--disabled {
background-color: #ebedf0;
border-color: #c8c9cc;
}
.u-radio__icon--disabled--checked {
color: #c8c9cc !important;
}
.u-radio__label {
word-wrap: break-word;
margin-left: 5rpx;
margin-right: 12rpx;
color: $u-content-color;
font-size: 15rpx;
}
.u-radio__label--disabled {
color: #c8c9cc;
}
.u-radio__label:empty {
margin: 0;
}
</style>