123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <template>
- <view class="tn-radio-class tn-radio" :style="[radioStyle]">
- <view
- class="tn-radio__icon-wrap"
- :class="[iconClass]"
- :style=[iconStyle]
- @tap="toggle"
- >
- <view class="tn-icon-success tn-radio__icon-wrap__icon"></view>
- </view>
-
- <view
- class="tn-radio__label"
- :style="{
- fontSize: labelSize ? labelSize + 'rpx' : ''
- }"
- @tap="onClickLabel"
- >
- <slot></slot>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'tn-radio',
- props: {
- // radio名称
- name: {
- type: [String, Number],
- default: ''
- },
- // 是否禁用
- disabled: {
- type: Boolean,
- default: false
- },
- // 禁用点击标签进行选择
- disabledLabel: {
- type: Boolean,
- default: false
- },
- // 选择框的形状 square 方形 circle 圆形
- shape: {
- type: String,
- default: ''
- },
- // 选中时的颜色
- activeColor: {
- type: String,
- default: ''
- },
- // 组件尺寸
- size: {
- type: Number,
- default: 0
- },
- // 图标大小
- iconSize: {
- type: Number,
- default: 0
- },
- // label字体大小
- labelSize: {
- type: Number,
- default: 0
- }
- },
- computed: {
- // 禁用,父组件会覆盖子组件的状态
- isDisabled() {
- return this.disabled ? this.disabled : (this.parentData.disabled ? this.parentData.disabled : false)
- },
- // 禁用label点击,父组件会覆盖子组件的状态
- isDisabledLabel() {
- return this.disabledLabel ? this.disabledLabel : (this.parentData.disabledLabel ? this.parentData.disabledLabel : false)
- },
- // 组件尺寸
- elSize() {
- return this.size ? this.size : (this.parentData.size ? this.parentData.size : 34)
- },
- // 组件选中时的颜色
- elActiveColor() {
- return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : '#01BEFF')
- },
- // 组件形状
- elShape() {
- return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle')
- },
- iconClass() {
- let clazz = ''
- clazz += (' tn-radio__icon-wrap--' + this.elShape)
-
- if (this.parentData.value === this.name) clazz += ' tn-radio__icon-wrap--checked'
- if (this.isDisabled) clazz += ' tn-radio__icon-wrap--disabled'
- if (this.parentData.value === this.name && this.isDisabled) clazz += ' tn-radio__icon-wrap--disabled--checked'
-
- return clazz
- },
- iconStyle() {
- // 当前radio的name等于parent的value才认为时选中
- let style = {}
- if (this.elActiveColor && this.parentData.value === this.name && !this.isDisabled) {
- style.borderColor = this.elActiveColor
- style.backgroundColor = this.elActiveColor
- }
-
- style.color = this.parentData.value === this.name ? '#FFFFFF' : 'transparent'
-
- style.width = this.elSize + 'rpx'
- style.height = style.width
-
- style.fontSize = (this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 20)) + 'rpx'
-
- return style
- },
- radioStyle() {
- let style = {}
- if (this.parent && this.parentData.width) {
- // #ifdef MP
- // 各家小程序因为它们特殊的编译结构,使用float布局
- style.float = 'left';
- // #endif
- // #ifndef MP
- // H5和APP使用flex布局
- style.flex = `0 0 ${this.parentData.width}`;
- // #endif
- }
- if(this.parent && this.parentData.wrap) {
- style.width = '100%';
- // #ifndef MP
- // H5和APP使用flex布局,将宽度设置100%,即可自动换行
- style.flex = '0 0 100%';
- // #endif
- }
-
- return style
- }
- },
- data() {
- return {
- // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.xxx的形式
- // 故只能使用如此方法
- parentData: {
- value: null,
- disabled: null,
- disabledLabel: null,
- shape: null,
- activeColor: null,
- size: null,
- width: null,
- wrap: null,
- iconSize: null,
- }
- }
- },
- created() {
- // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
- this.updateParentData()
- this.parent.children.push(this)
- },
- methods: {
- updateParentData() {
- this.getParentData('tn-radio-group')
- },
- onClickLabel() {
- if (!this.isDisabledLabel && !this.isDisabled) {
- this.setRadioCheckedStatus()
- }
- },
- toggle() {
- if (!this.isDisabled) {
- this.setRadioCheckedStatus()
- }
- },
- emitEvent() {
- // tn-radio的name不等于父组件的v-model的值时(意味着未选中),才发出事件,避免多次点击触发事件
- if (this.parentData.value !== this.name) this.$emit('change', this.name)
- },
- // 改变选中的状态
- // 更改本组件的parentData.value的值为本组件的name值,同时通过父组件遍历所有的radio实例
- // 将本组件外的其他radio的parentData.value都设置为空
- setRadioCheckedStatus() {
- this.emitEvent()
- if (this.parent) {
- this.parent.setValue(this.name)
- this.parentData.value = this.name
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
-
- .tn-radio {
- /* #ifndef APP-NVUE */
- display: inline-flex;
- /* #endif */
- align-items: center;
- overflow: hidden;
- user-select: none;
- line-height: 1.8;
-
- &__icon-wrap {
- color: $tn-font-color;
- display: flex;
- flex-direction: row;
- flex: none;
- align-items: center;
- justify-content: center;
- box-sizing: border-box;
- width: 42rpx;
- height: 42rpx;
- color: transparent;
- text-align: center;
- transition-property: color, border-color, background-color;
- font-size: 20rpx;
- border: 1rpx solid $tn-font-sub-color;
- transition-duration: 0.2s;
-
- /* #ifdef MP-TOUTIAO */
- // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
- &__icon {
- line-height: 0;
- }
- /* #endif */
-
- &--circle {
- border-radius: 100%;
- }
-
- &--square {
- border-radius: 6rpx;
- }
-
- &--checked {
- color: #FFFFFF;
- background-color: $tn-main-color;
- border-color: $tn-main-color;
- }
-
- &--disabled {
- background-color: $tn-font-holder-color;
- border-color: $tn-font-sub-color;
- }
-
- &--disabled--checked {
- color: $tn-font-sub-color !important;
- }
- }
-
- &__label {
- word-wrap: break-word;
- margin-left: 10rpx;
- margin-right: 24rpx;
- color: $tn-font-color;
- font-size: 30rpx;
-
- &--disabled {
- color: $tn-font-sub-color;
- }
- }
- }
- </style>
|