AI销管
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

272 lines
7.8 KiB

  1. <template>
  2. <view class="u-radio" :style="[radioStyle]">
  3. <view class="u-radio__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
  4. <u-icon
  5. class="u-radio__icon-wrap__icon"
  6. name="checkbox-mark"
  7. :size="elIconSize"
  8. :color="iconColor"/>
  9. </view>
  10. <view class="u-radio__label" @tap="onClickLabel" :style="{
  11. fontSize: $u.addUnit(labelSize)
  12. }">
  13. <slot />
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. /**
  19. * radio 单选框
  20. * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio-group使用
  21. * @tutorial https://www.uviewui.com/components/radio.html
  22. * @property {String Number} icon-size 图标大小,单位rpx(默认24)
  23. * @property {String Number} label-size label字体大小,单位rpx(默认28)
  24. * @property {String Number} name radio组件的标示符
  25. * @property {String} shape 形状,见上方说明(默认circle)
  26. * @property {Boolean} disabled 是否禁用(默认false)
  27. * @property {Boolean} label-disabled 点击文本是否可以操作radio(默认true)
  28. * @property {String} active-color 选中时的颜色,如设置parent的active-color将失效
  29. * @event {Function} change 某个radio状态发生变化时触发(选中状态)
  30. * @example <u-radio :label-disabled="false">门掩黄昏,无计留春住</u-radio>
  31. */
  32. export default {
  33. name: "u-radio",
  34. props: {
  35. // radio的名称
  36. name: {
  37. type: [String, Number],
  38. default: ''
  39. },
  40. // 形状,square为方形,circle为原型
  41. shape: {
  42. type: String,
  43. default: ''
  44. },
  45. // 是否禁用
  46. disabled: {
  47. type: [String, Boolean],
  48. default: ''
  49. },
  50. // 是否禁止点击提示语选中复选框
  51. labelDisabled: {
  52. type: [String, Boolean],
  53. default: ''
  54. },
  55. // 选中状态下的颜色,如设置此值,将会覆盖parent的activeColor值
  56. activeColor: {
  57. type: String,
  58. default: ''
  59. },
  60. // 图标的大小,单位rpx
  61. iconSize: {
  62. type: [String, Number],
  63. default: ''
  64. },
  65. // label的字体大小,rpx单位
  66. labelSize: {
  67. type: [String, Number],
  68. default: ''
  69. },
  70. },
  71. data() {
  72. return {
  73. // 父组件的默认值,因为头条小程序不支持在computed中使用this.parent.shape的形式
  74. // 故只能使用如此方法
  75. parentData: {
  76. iconSize: null,
  77. labelDisabled: null,
  78. disabled: null,
  79. shape: null,
  80. activeColor: null,
  81. size: null,
  82. width: null,
  83. height: null,
  84. value: null,
  85. wrap: null
  86. }
  87. };
  88. },
  89. created() {
  90. this.parent = false;
  91. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  92. this.updateParentData();
  93. this.parent.children.push(this);
  94. },
  95. computed: {
  96. // 是否禁用,如果父组件u-raios-group禁用的话,将会忽略子组件的配置
  97. elDisabled() {
  98. return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
  99. },
  100. // 是否禁用label点击
  101. elLabelDisabled() {
  102. return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled : false;
  103. },
  104. // 组件尺寸,对应size的值,默认值为34rpx
  105. elSize() {
  106. return this.size ? this.size : (this.parentData.size ? this.parentData.size : 34);
  107. },
  108. // 组件的勾选图标的尺寸,默认20
  109. elIconSize() {
  110. return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 20);
  111. },
  112. // 组件选中激活时的颜色
  113. elActiveColor() {
  114. return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : 'primary');
  115. },
  116. // 组件的形状
  117. elShape() {
  118. return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle');
  119. },
  120. // 设置radio的状态,要求radio的name等于parent的value时才为选中状态
  121. iconStyle() {
  122. let style = {};
  123. if (this.elActiveColor && this.parentData.value == this.name && !this.elDisabled) {
  124. style.borderColor = this.elActiveColor;
  125. style.backgroundColor = this.elActiveColor;
  126. }
  127. style.width = this.$u.addUnit(this.elSize);
  128. style.height = this.$u.addUnit(this.elSize);
  129. return style;
  130. },
  131. iconColor() {
  132. return this.name == this.parentData.value ? '#ffffff' : 'transparent';
  133. },
  134. iconClass() {
  135. let classes = [];
  136. classes.push('u-radio__icon-wrap--' + this.elShape);
  137. if (this.name == this.parentData.value) classes.push('u-radio__icon-wrap--checked');
  138. if (this.elDisabled) classes.push('u-radio__icon-wrap--disabled');
  139. if (this.name == this.parentData.value && this.elDisabled) classes.push(
  140. 'u-radio__icon-wrap--disabled--checked');
  141. // 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  142. return classes.join(' ');
  143. },
  144. radioStyle() {
  145. let style = {};
  146. if (this.parentData.width) {
  147. style.width = this.$u.addUnit(this.parentData.width);
  148. // #ifdef MP
  149. // 各家小程序因为它们特殊的编译结构,使用float布局
  150. style.float = 'left';
  151. // #endif
  152. // #ifndef MP
  153. // H5和APP使用flex布局
  154. style.flex = `0 0 ${this.$u.addUnit(this.parentData.width)}`;
  155. // #endif
  156. }
  157. if (this.parentData.wrap) {
  158. style.width = '100%';
  159. // #ifndef MP
  160. // H5和APP使用flex布局,将宽度设置100%,即可自动换行
  161. style.flex = '0 0 100%';
  162. // #endif
  163. }
  164. return style;
  165. }
  166. },
  167. methods: {
  168. updateParentData() {
  169. this.getParentData('u-radio-group');
  170. },
  171. onClickLabel() {
  172. if (!this.elLabelDisabled && !this.elDisabled) {
  173. this.setRadioCheckedStatus();
  174. }
  175. },
  176. toggle() {
  177. if (!this.elDisabled) {
  178. this.setRadioCheckedStatus();
  179. }
  180. },
  181. emitEvent() {
  182. // u-radio的name不等于父组件的v-model的值时(意味着未选中),才发出事件,避免多次点击触发事件
  183. if(this.parentData.value != this.name) this.$emit('change', this.name);
  184. },
  185. // 改变组件选中状态
  186. // 这里的改变的依据是,更改本组件的parentData.value值为本组件的name值,同时通过父组件遍历所有u-radio实例
  187. // 将本组件外的其他u-radio的parentData.value都设置为空(由computed计算后,都被取消选中状态),因而只剩下一个为选中状态
  188. setRadioCheckedStatus() {
  189. this.emitEvent();
  190. if(this.parent) {
  191. this.parent.setValue(this.name);
  192. this.parentData.value = this.name;
  193. }
  194. }
  195. }
  196. };
  197. </script>
  198. <style lang="scss" scoped>
  199. @import "../../libs/css/style.components.scss";
  200. .u-radio {
  201. /* #ifndef APP-NVUE */
  202. display: inline-flex;
  203. /* #endif */
  204. align-items: center;
  205. overflow: hidden;
  206. user-select: none;
  207. line-height: 1.8;
  208. &__icon-wrap {
  209. color: $u-content-color;
  210. @include vue-flex;
  211. flex: none;
  212. align-items: center;
  213. justify-content: center;
  214. box-sizing: border-box;
  215. width: 42rpx;
  216. height: 42rpx;
  217. color: transparent;
  218. text-align: center;
  219. transition-property: color, border-color, background-color;
  220. font-size: 20px;
  221. border: 1px solid #c8c9cc;
  222. transition-duration: 0.2s;
  223. /* #ifdef MP-TOUTIAO */
  224. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  225. &__icon {
  226. line-height: 0;
  227. }
  228. /* #endif */
  229. &--circle {
  230. border-radius: 100%;
  231. }
  232. &--square {
  233. border-radius: 3px;
  234. }
  235. &--checked {
  236. color: #fff;
  237. background-color: #2979ff;
  238. border-color: #2979ff;
  239. }
  240. &--disabled {
  241. background-color: #ebedf0;
  242. border-color: #c8c9cc;
  243. }
  244. &--disabled--checked {
  245. color: #c8c9cc !important;
  246. }
  247. }
  248. &__label {
  249. word-wrap: break-word;
  250. margin-left: 10rpx;
  251. margin-right: 24rpx;
  252. color: $u-content-color;
  253. font-size: 30rpx;
  254. &--disabled {
  255. color: #c8c9cc;
  256. }
  257. }
  258. }
  259. </style>