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.

u-radio.vue 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. }
  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)
  145. classes.push("u-radio__icon-wrap--disabled--checked");
  146. // 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
  147. return classes.join(" ");
  148. },
  149. radioStyle() {
  150. let style = {};
  151. if (this.parentData.width) {
  152. style.width = this.$u.addUnit(this.parentData.width);
  153. // #ifdef MP
  154. // 各家小程序因为它们特殊的编译结构,使用float布局
  155. style.float = "left";
  156. // #endif
  157. // #ifndef MP
  158. // H5和APP使用flex布局
  159. style.flex = `0 0 ${this.$u.addUnit(this.parentData.width)}`;
  160. // #endif
  161. }
  162. if (this.parentData.wrap) {
  163. style.width = "100%";
  164. // #ifndef MP
  165. // H5和APP使用flex布局,将宽度设置100%,即可自动换行
  166. style.flex = "0 0 100%";
  167. // #endif
  168. }
  169. return style;
  170. }
  171. },
  172. methods: {
  173. updateParentData() {
  174. this.getParentData("u-radio-group");
  175. },
  176. onClickLabel() {
  177. if (!this.elLabelDisabled && !this.elDisabled) {
  178. this.setRadioCheckedStatus();
  179. }
  180. },
  181. toggle() {
  182. if (!this.elDisabled) {
  183. this.setRadioCheckedStatus();
  184. }
  185. },
  186. emitEvent() {
  187. // u-radio的name不等于父组件的v-model的值时(意味着未选中),才发出事件,避免多次点击触发事件
  188. if (this.parentData.value != this.name) this.$emit("change", this.name);
  189. },
  190. // 改变组件选中状态
  191. // 这里的改变的依据是,更改本组件的parentData.value值为本组件的name值,同时通过父组件遍历所有u-radio实例
  192. // 将本组件外的其他u-radio的parentData.value都设置为空(由computed计算后,都被取消选中状态),因而只剩下一个为选中状态
  193. setRadioCheckedStatus() {
  194. this.emitEvent();
  195. if (this.parent) {
  196. this.parent.setValue(this.name);
  197. this.parentData.value = this.name;
  198. }
  199. }
  200. }
  201. };
  202. </script>
  203. <style lang="scss" scoped>
  204. @import "../../libs/css/style.components.scss";
  205. .u-radio {
  206. /* #ifndef APP-NVUE */
  207. display: inline-flex;
  208. /* #endif */
  209. align-items: center;
  210. overflow: hidden;
  211. user-select: none;
  212. line-height: 1.8;
  213. &__icon-wrap {
  214. color: $u-content-color;
  215. @include vue-flex;
  216. flex: none;
  217. align-items: center;
  218. justify-content: center;
  219. box-sizing: border-box;
  220. width: 42rpx;
  221. height: 42rpx;
  222. color: transparent;
  223. text-align: center;
  224. transition-property: color, border-color, background-color;
  225. font-size: 20px;
  226. border: 1px solid #c8c9cc;
  227. transition-duration: 0.2s;
  228. /* #ifdef MP-TOUTIAO */
  229. // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
  230. &__icon {
  231. line-height: 0;
  232. }
  233. /* #endif */
  234. &--circle {
  235. border-radius: 100%;
  236. }
  237. &--square {
  238. border-radius: 3px;
  239. }
  240. &--checked {
  241. color: #fff;
  242. background-color: #2979ff;
  243. border-color: #2979ff;
  244. }
  245. &--disabled {
  246. background-color: #ebedf0;
  247. border-color: #c8c9cc;
  248. }
  249. &--disabled--checked {
  250. color: #c8c9cc !important;
  251. }
  252. }
  253. &__label {
  254. word-wrap: break-word;
  255. margin-left: 10rpx;
  256. margin-right: 24rpx;
  257. color: $u-content-color;
  258. font-size: 30rpx;
  259. &--disabled {
  260. color: #c8c9cc;
  261. }
  262. }
  263. }
  264. </style>