u-radio.vue 7.6 KB

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