u-radio.vue 7.9 KB

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