u-radio.vue 7.9 KB

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