u-radio.vue 7.7 KB

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