AI销管
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

129 righe
4.4 KiB

  1. <template>
  2. <view class="u-radio-group u-clearfix">
  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. mixins: [Emitter],
  26. props: {
  27. // 是否禁用所有单选框
  28. disabled: {
  29. type: Boolean,
  30. default: false
  31. },
  32. // 匹配某一个radio组件,如果某个radio的name值等于此值,那么这个radio就被会选中
  33. value: {
  34. type: [String, Number],
  35. default: ''
  36. },
  37. // 选中状态下的颜色
  38. activeColor: {
  39. type: String,
  40. default: '#2979ff'
  41. },
  42. // 组件的整体大小
  43. size: {
  44. type: [String, Number],
  45. default: 34
  46. },
  47. // 是否禁止点击提示语选中复选框
  48. labelDisabled: {
  49. type: Boolean,
  50. default: false
  51. },
  52. // 形状,square为方形,circle为原型
  53. shape: {
  54. type: String,
  55. default: 'circle'
  56. },
  57. // 图标的大小,单位rpx
  58. iconSize: {
  59. type: [String, Number],
  60. default: 20
  61. },
  62. // 每个checkbox占u-checkbox-group的宽度
  63. width: {
  64. type: [String, Number],
  65. default: 'auto'
  66. },
  67. // 是否每个checkbox都换行
  68. wrap: {
  69. type: Boolean,
  70. default: false
  71. }
  72. },
  73. created() {
  74. // 如果将children定义在data中,在微信小程序会造成循环引用而报错
  75. this.children = [];
  76. },
  77. watch: {
  78. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  79. parentData() {
  80. if(this.children.length) {
  81. this.children.map(child => {
  82. // 判断子组件(u-radio)如果有updateParentData方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  83. typeof(child.updateParentData) == 'function' && child.updateParentData();
  84. })
  85. }
  86. },
  87. },
  88. computed: {
  89. // 这里computed的变量,都是子组件u-radio需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
  90. // 所以需要手动通知子组件,这里返回一个parentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-radio-group)
  91. // 拉取父组件新的变化后的参数
  92. parentData() {
  93. return [this.value, this.disabled, this.activeColor, this.size, this.labelDisabled, this.shape, this.iconSize, this.width, this.wrap];
  94. }
  95. },
  96. methods: {
  97. // 该方法有子组件radio调用,当一个radio被选中的时候,给父组件设置value值(props传递的value)
  98. setValue(val) {
  99. // 通过子组件传递过来的val值(此被选中的子组件内部已将parentValue设置等于val的值),将其他
  100. // u-radio设置未选中的状态
  101. this.children.map(child => {
  102. if(child.parentData.value != val) child.parentData.value = '';
  103. })
  104. // 通过emit事件,设置父组件通过v-model双向绑定的值
  105. this.$emit('input', val);
  106. this.$emit('change', val);
  107. // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
  108. // 由于头条小程序执行迟钝,故需要用几十毫秒的延时
  109. setTimeout(() => {
  110. // 将当前的值发送到 u-form-item 进行校验
  111. this.dispatch('u-form-item', 'on-form-change', val);
  112. }, 60)
  113. }
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. @import "../../libs/css/style.components.scss";
  119. .u-radio-group {
  120. /* #ifndef MP || APP-NVUE */
  121. display: inline-flex;
  122. flex-wrap: wrap;
  123. /* #endif */
  124. }
  125. </style>