Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

u-radio.vue 6.6 KiB

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