123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <template>
- <view class="u-radio" :style="[radioStyle]">
- <view class="u-radio__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
- <u-icon
- class="u-radio__icon-wrap__icon"
- name="checkbox-mark"
- :size="elIconSize"
- :color="iconColor"/>
- </view>
- <view class="u-radio__label" @tap="onClickLabel" :style="{
- fontSize: $u.addUnit(labelSize)
- }">
- <slot />
- </view>
- </view>
- </template>
- <script>
-
- export default {
- name: "u-radio",
- props: {
-
- name: {
- type: [String, Number],
- default: ''
- },
-
- shape: {
- type: String,
- default: ''
- },
-
- disabled: {
- type: [String, Boolean],
- default: ''
- },
-
- labelDisabled: {
- type: [String, Boolean],
- default: ''
- },
-
- activeColor: {
- type: String,
- default: ''
- },
-
- iconSize: {
- type: [String, Number],
- default: ''
- },
-
- labelSize: {
- type: [String, Number],
- default: ''
- },
- },
- data() {
- return {
-
-
- parentData: {
- iconSize: null,
- labelDisabled: null,
- disabled: null,
- shape: null,
- activeColor: null,
- size: null,
- width: null,
- height: null,
- value: null,
- wrap: null
- }
- };
- },
- created() {
- this.parent = false;
-
- this.updateParentData();
- this.parent.children.push(this);
- },
- computed: {
-
- elDisabled() {
- return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
- },
-
- elLabelDisabled() {
- return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : false;
- },
-
- elSize() {
- return this.size ? this.size : (this.parentData.size ? this.parentData.size : 34);
- },
-
- elIconSize() {
- return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 20);
- },
-
- elActiveColor() {
- return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : 'primary');
- },
-
- elShape() {
- return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle');
- },
-
- iconStyle() {
- let style = {};
- if (this.elActiveColor && this.parentData.value == this.name && !this.elDisabled) {
- style.borderColor = this.elActiveColor;
- style.backgroundColor = this.elActiveColor;
- }
- style.width = this.$u.addUnit(this.elSize);
- style.height = this.$u.addUnit(this.elSize);
- return style;
- },
- iconColor() {
- return this.name == this.parentData.value ? '#ffffff' : 'transparent';
- },
- iconClass() {
- let classes = [];
- classes.push('u-radio__icon-wrap--' + this.elShape);
- if (this.name == this.parentData.value) classes.push('u-radio__icon-wrap--checked');
- if (this.elDisabled) classes.push('u-radio__icon-wrap--disabled');
- if (this.name == this.parentData.value && this.elDisabled) classes.push(
- 'u-radio__icon-wrap--disabled--checked');
-
- return classes.join(' ');
- },
- radioStyle() {
- let style = {};
- if (this.parentData.width) {
- style.width = this.$u.addUnit(this.parentData.width);
-
-
- style.float = 'left';
-
-
-
- style.flex = `0 0 ${this.$u.addUnit(this.parentData.width)}`;
-
- }
- if (this.parentData.wrap) {
- style.width = '100%';
-
-
- style.flex = '0 0 100%';
-
- }
- return style;
- }
- },
- methods: {
- updateParentData() {
- this.getParentData('u-radio-group');
- },
- onClickLabel() {
- if (!this.elLabelDisabled && !this.elDisabled) {
- this.setRadioCheckedStatus();
- }
- },
- toggle() {
- if (!this.elDisabled) {
- this.setRadioCheckedStatus();
- }
- },
- emitEvent() {
-
- if(this.parentData.value != this.name) this.$emit('change', this.name);
- },
-
-
-
- setRadioCheckedStatus() {
- this.emitEvent();
- if(this.parent) {
- this.parent.setValue(this.name);
- this.parentData.value = this.name;
- }
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "../../libs/css/style.components.scss";
- .u-radio {
- /* #ifndef APP-NVUE */
- display: inline-flex;
- /* #endif */
- align-items: center;
- overflow: hidden;
- user-select: none;
- line-height: 1.8;
-
- &__icon-wrap {
- color: $u-content-color;
- @include vue-flex;
- 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: 20px;
- border: 1px solid #c8c9cc;
- transition-duration: 0.2s;
-
- /* #ifdef MP-TOUTIAO */
- // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
- &__icon {
- line-height: 0;
- }
- /* #endif */
-
- &--circle {
- border-radius: 100%;
- }
-
- &--square {
- border-radius: 3px;
- }
-
- &--checked {
- color: #fff;
- background-color: #2979ff;
- border-color: #2979ff;
- }
-
- &--disabled {
- background-color: #ebedf0;
- border-color: #c8c9cc;
- }
-
- &--disabled--checked {
- color: #c8c9cc !important;
- }
- }
-
- &__label {
- word-wrap: break-word;
- margin-left: 10rpx;
- margin-right: 24rpx;
- color: $u-content-color;
- font-size: 30rpx;
-
- &--disabled {
- color: #c8c9cc;
- }
- }
- }
- </style>
|