From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001 From: spring <2396852758@qq.com> Date: 星期三, 28 五月 2025 16:48:52 +0800 Subject: [PATCH] 初始化项目 --- uview-ui/components/u-radio/u-radio.vue | 276 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 276 insertions(+), 0 deletions(-) diff --git a/uview-ui/components/u-radio/u-radio.vue b/uview-ui/components/u-radio/u-radio.vue new file mode 100644 index 0000000..f540309 --- /dev/null +++ b/uview-ui/components/u-radio/u-radio.vue @@ -0,0 +1,276 @@ +<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> + /** + * radio 鍗曢�夋 + * @description 鍗曢�夋鐢ㄤ簬鏈変竴涓�夋嫨锛岀敤鎴峰彧鑳介�夋嫨鍏朵腑涓�涓殑鍦烘櫙銆傛惌閰島-radio-group浣跨敤 + * @tutorial https://www.uviewui.com/components/radio.html + * @property {String Number} icon-size 鍥炬爣澶у皬锛屽崟浣峳px锛堥粯璁�24锛� + * @property {String Number} label-size label瀛椾綋澶у皬锛屽崟浣峳px锛堥粯璁�28锛� + * @property {String Number} name radio缁勪欢鐨勬爣绀虹 + * @property {String} shape 褰㈢姸锛岃涓婃柟璇存槑锛堥粯璁ircle锛� + * @property {Boolean} disabled 鏄惁绂佺敤锛堥粯璁alse锛� + * @property {Boolean} label-disabled 鐐瑰嚮鏂囨湰鏄惁鍙互鎿嶄綔radio锛堥粯璁rue锛� + * @property {String} active-color 閫変腑鏃剁殑棰滆壊锛屽璁剧疆parent鐨刟ctive-color灏嗗け鏁� + * @event {Function} change 鏌愪釜radio鐘舵�佸彂鐢熷彉鍖栨椂瑙﹀彂(閫変腑鐘舵��) + * @example <u-radio :label-disabled="false">闂ㄦ帺榛勬槒锛屾棤璁$暀鏄ヤ綇</u-radio> + */ + export default { + name: "u-radio", + props: { + // radio鐨勫悕绉� + name: { + type: [String, Number], + default: '' + }, + // 褰㈢姸锛宻quare涓烘柟褰紝circle涓哄師鍨� + shape: { + type: String, + default: '' + }, + // 鏄惁绂佺敤 + disabled: { + type: [String, Boolean], + default: '' + }, + // 鏄惁绂佹鐐瑰嚮鎻愮ず璇�変腑澶嶉�夋 + labelDisabled: { + type: [String, Boolean], + default: '' + }, + // 閫変腑鐘舵�佷笅鐨勯鑹诧紝濡傝缃鍊硷紝灏嗕細瑕嗙洊parent鐨刟ctiveColor鍊� + activeColor: { + type: String, + default: '' + }, + // 鍥炬爣鐨勫ぇ灏忥紝鍗曚綅rpx + iconSize: { + type: [String, Number], + default: '' + }, + // label鐨勫瓧浣撳ぇ灏忥紝rpx鍗曚綅 + labelSize: { + type: [String, Number], + default: '' + }, + }, + data() { + return { + // 鐖剁粍浠剁殑榛樿鍊硷紝鍥犱负澶存潯灏忕▼搴忎笉鏀寔鍦╟omputed涓娇鐢╰his.parent.shape鐨勫舰寮� + // 鏁呭彧鑳戒娇鐢ㄥ姝ゆ柟娉� + 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; + // 鏀粯瀹濆皬绋嬪簭涓嶆敮鎸乸rovide/inject锛屾墍浠ヤ娇鐢ㄨ繖涓柟娉曡幏鍙栨暣涓埗缁勪欢锛屽湪created瀹氫箟锛岄伩鍏嶅惊鐜紩鐢� + this.updateParentData(); + this.parent.children.push(this); + }, + computed: { + // 鏄惁绂佺敤锛屽鏋滅埗缁勪欢u-raios-group绂佺敤鐨勮瘽锛屽皢浼氬拷鐣ュ瓙缁勪欢鐨勯厤缃� + elDisabled() { + return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false; + }, + // 鏄惁绂佺敤label鐐瑰嚮 + elLabelDisabled() { + return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : false; + }, + // 缁勪欢灏哄锛屽搴攕ize鐨勫�硷紝榛樿鍊间负34rpx + elSize() { + return this.size ? this.size : (this.parentData.size ? this.parentData.size : 34); + }, + // 缁勪欢鐨勫嬀閫夊浘鏍囩殑灏哄锛岄粯璁�20 + 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'); + }, + // 璁剧疆radio鐨勭姸鎬侊紝瑕佹眰radio鐨刵ame绛変簬parent鐨剉alue鏃舵墠涓洪�変腑鐘舵�� + 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'); + // 鏀粯瀹濆皬绋嬪簭鏃犳硶鍔ㄦ�佺粦瀹氫竴涓暟缁勭被鍚嶏紝鍚﹀垯瑙f瀽鍑烘潵鐨勭粨鏋滀細甯︽湁","锛岃�屽鑷村け鏁� + return classes.join(' '); + }, + radioStyle() { + let style = {}; + if (this.parentData.width) { + style.width = this.$u.addUnit(this.parentData.width); + // #ifdef MP + // 鍚勫灏忕▼搴忓洜涓哄畠浠壒娈婄殑缂栬瘧缁撴瀯锛屼娇鐢╢loat甯冨眬 + style.float = 'left'; + // #endif + // #ifndef MP + // H5鍜孉PP浣跨敤flex甯冨眬 + style.flex = `0 0 ${this.$u.addUnit(this.parentData.width)}`; + // #endif + } + if (this.parentData.wrap) { + style.width = '100%'; + // #ifndef MP + // H5鍜孉PP浣跨敤flex甯冨眬锛屽皢瀹藉害璁剧疆100%锛屽嵆鍙嚜鍔ㄦ崲琛� + style.flex = '0 0 100%'; + // #endif + } + return style; + } + }, + methods: { + updateParentData() { + this.getParentData('u-radio-group'); + }, + onClickLabel() { + if (!this.elLabelDisabled && !this.elDisabled) { + this.setRadioCheckedStatus(); + } + }, + toggle() { + if (!this.elDisabled) { + this.setRadioCheckedStatus(); + } + }, + emitEvent() { + // u-radio鐨刵ame涓嶇瓑浜庣埗缁勪欢鐨剉-model鐨勫�兼椂(鎰忓懗鐫�鏈�変腑)锛屾墠鍙戝嚭浜嬩欢锛岄伩鍏嶅娆$偣鍑昏Е鍙戜簨浠� + if(this.parentData.value != this.name) this.$emit('change', this.name); + }, + // 鏀瑰彉缁勪欢閫変腑鐘舵�� + // 杩欓噷鐨勬敼鍙樼殑渚濇嵁鏄紝鏇存敼鏈粍浠剁殑parentData.value鍊间负鏈粍浠剁殑name鍊硷紝鍚屾椂閫氳繃鐖剁粍浠堕亶鍘嗘墍鏈塽-radio瀹炰緥 + // 灏嗘湰缁勪欢澶栫殑鍏朵粬u-radio鐨刾arentData.value閮借缃负绌�(鐢眂omputed璁$畻鍚庯紝閮借鍙栨秷閫変腑鐘舵��)锛屽洜鑰屽彧鍓╀笅涓�涓负閫変腑鐘舵�� + setRadioCheckedStatus() { + this.emitEvent(); + if(this.parent) { + if (this.parentData.value !== this.name) { + this.parent.setValue(this.name); + this.parentData.value = this.name; + } else { + this.parent.setValue(''); + this.parentData.value = ''; + } + } + } + } + }; +</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: #497bff; + border-color: #497bff; + } + + &--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> -- Gitblit v1.9.3