u-radio-group.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <view class="u-radio-group u-clearfix" :class="uFromData.inputAlign == 'right' ? 'flex-end' : ''">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. import Emitter from "../../libs/util/emitter.js";
  8. /**
  9. * radioRroup 单选框父组件
  10. * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio使用
  11. * @tutorial https://www.uviewui.com/components/radio.html
  12. * @property {Boolean} disabled 是否禁用所有radio(默认false)
  13. * @property {String Number} size 组件整体的大小,单位rpx(默认40)
  14. * @property {String} active-color 选中时的颜色,应用到所有子Radio组件(默认#2979ff)
  15. * @property {String Number} icon-size 图标大小,单位rpx(默认20)
  16. * @property {String} shape 外观形状,shape-方形,circle-圆形(默认circle)
  17. * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox(默认false)
  18. * @property {String} width 宽度,需带单位
  19. * @property {Boolean} wrap 是否每个radio都换行(默认false)
  20. * @event {Function} change 任一个radio状态发生变化时触发
  21. * @example <u-radio-group v-model="value"></u-radio-group>
  22. */
  23. export default {
  24. name: "u-radio-group",
  25. emits: ["update:modelValue", "input", "change"],
  26. mixins: [Emitter],
  27. props: {
  28. // 匹配某一个radio组件,如果某个radio的name值等于此值,那么这个radio就被会选中
  29. value: {
  30. type: [String, Number, Array, Boolean],
  31. default: ""
  32. },
  33. modelValue: {
  34. type: [String, Number, Array, Boolean],
  35. default: ""
  36. },
  37. // 是否禁用所有单选框
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. // 选中状态下的颜色
  43. activeColor: {
  44. type: String,
  45. default: "#2979ff"
  46. },
  47. // 组件的整体大小
  48. size: {
  49. type: [String, Number],
  50. default: 34
  51. },
  52. // 是否禁止点击提示语选中复选框
  53. labelDisabled: {
  54. type: Boolean,
  55. default: false
  56. },
  57. // 形状,square为方形,circle为圆型
  58. shape: {
  59. type: String,
  60. default: "circle"
  61. },
  62. // 图标的大小,单位rpx
  63. iconSize: {
  64. type: [String, Number],
  65. default: 20
  66. },
  67. // 每个checkbox占u-checkbox-group的宽度
  68. width: {
  69. type: [String, Number],
  70. default: "auto"
  71. },
  72. // 是否每个checkbox都换行
  73. wrap: {
  74. type: Boolean,
  75. default: false
  76. }
  77. },
  78. data() {
  79. return {
  80. uFromData: {
  81. inputAlign: "left"
  82. }
  83. };
  84. },
  85. created() {
  86. // 如果将children定义在data中,在微信小程序会造成循环引用而报错
  87. this.children = [];
  88. },
  89. mounted() {
  90. // 支付宝、头条小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环应用
  91. let parent = this.$u.$parent.call(this, "u-form");
  92. if (parent) {
  93. Object.keys(this.uFromData).map(key => {
  94. this.uFromData[key] = parent[key];
  95. });
  96. }
  97. },
  98. watch: {
  99. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  100. parentData() {
  101. if (this.children.length) {
  102. this.children.map(child => {
  103. // 判断子组件(u-radio)如果有updateParentData方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  104. typeof child.updateParentData == "function" && child.updateParentData();
  105. });
  106. }
  107. }
  108. },
  109. computed: {
  110. valueCom() {
  111. // #ifdef VUE2
  112. return this.value;
  113. // #endif
  114. // #ifdef VUE3
  115. return this.modelValue;
  116. // #endif
  117. },
  118. // 这里computed的变量,都是子组件u-radio需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
  119. // 所以需要手动通知子组件,这里返回一个parentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-radio-group)
  120. // 拉取父组件新的变化后的参数
  121. parentData() {
  122. return [
  123. this.value,
  124. this.disabled,
  125. this.activeColor,
  126. this.size,
  127. this.labelDisabled,
  128. this.shape,
  129. this.iconSize,
  130. this.width,
  131. this.wrap,
  132. this.modelValue
  133. ];
  134. }
  135. },
  136. methods: {
  137. // 该方法有子组件radio调用,当一个radio被选中的时候,给父组件设置value值(props传递的value)
  138. setValue(val) {
  139. // 通过子组件传递过来的val值(此被选中的子组件内部已将parentValue设置等于val的值),将其他
  140. // u-radio设置未选中的状态
  141. this.children.map(child => {
  142. if (child.parentData.value != val) child.parentData.value = "";
  143. });
  144. // 通过emit事件,设置父组件通过v-model双向绑定的值
  145. this.$emit("input", val);
  146. this.$emit("update:modelValue", val);
  147. this.$emit("change", val);
  148. // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
  149. // 由于头条小程序执行迟钝,故需要用几十毫秒的延时
  150. setTimeout(() => {
  151. // 将当前的值发送到 u-form-item 进行校验
  152. this.dispatch("u-form-item", "onFieldChange", val);
  153. }, 60);
  154. }
  155. }
  156. };
  157. </script>
  158. <style lang="scss" scoped>
  159. @import "../../libs/css/style.components.scss";
  160. .u-radio-group {
  161. /* #ifndef MP || APP-NVUE */
  162. display: inline-flex;
  163. flex-wrap: wrap;
  164. /* #endif */
  165. }
  166. .u-radio-group.flex-end {
  167. /* #ifndef APP-NVUE */
  168. display: inline-flex;
  169. justify-content: flex-end;
  170. flex-wrap: wrap;
  171. /* #endif */
  172. }
  173. </style>