u-radio-group.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view
  3. class="u-radio-group"
  4. :class="bemClass"
  5. >
  6. <slot></slot>
  7. </view>
  8. </template>
  9. <script>
  10. import props from './props.js';
  11. import mixin from '../../libs/mixin/mixin'
  12. import mpMixin from '../../libs/mixin/mpMixin';
  13. /**
  14. * radioRroup 单选框父组件
  15. * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio使用
  16. * @tutorial https://uview.d3u.cn/components/radio.html
  17. * @property {String | Number | Boolean} value 绑定的值
  18. * @property {Boolean} disabled 是否禁用所有radio(默认 false )
  19. * @property {String} shape 外观形状,square-方形,circle-圆形(默认 circle )
  20. * @property {String} activeColor 选中时的颜色,应用到所有子Radio组件(默认 '#2979ff' )
  21. * @property {String} inactiveColor 未选中的颜色 (默认 '#c8c9cc' )
  22. * @property {String} name 标识符
  23. * @property {String | Number} size 组件整体的大小,单位px(默认 18 )
  24. * @property {String} placement 布局方式,row-横向,column-纵向 (默认 'row' )
  25. * @property {String} label 文本
  26. * @property {String} labelColor label的颜色 (默认 '#303133' )
  27. * @property {String | Number} labelSize label的字体大小,px单位 (默认 14 )
  28. * @property {Boolean} labelDisabled 是否禁止点击文本操作checkbox(默认 false )
  29. * @property {String} iconColor 图标颜色 (默认 '#ffffff' )
  30. * @property {String | Number} iconSize 图标的大小,单位px (默认 12 )
  31. * @property {Boolean} borderBottom placement为row时,是否显示下边框 (默认 false )
  32. * @property {String} iconPlacement 图标与文字的对齐方式 (默认 'left' )
  33. * @property {Object} customStyle 组件的样式,对象形式
  34. * @event {Function} change 任一个radio状态发生变化时触发
  35. * @example <u-radio-group v-model="value"></u-radio-group>
  36. */
  37. export default {
  38. name: 'u-radio-group',
  39. mixins: [mpMixin, mixin, props],
  40. computed: {
  41. // 这里computed的变量,都是子组件u-radio需要用到的,由于头条小程序的兼容性差异,子组件无法实时监听父组件参数的变化
  42. // 所以需要手动通知子组件,这里返回一个parentData变量,供watch监听,在其中去通知每一个子组件重新从父组件(u-radio-group)
  43. // 拉取父组件新的变化后的参数
  44. parentData() {
  45. return [
  46. //#ifdef VUE2
  47. this.value,
  48. // #endif
  49. // #ifdef VUE3
  50. this.modelValue,
  51. // #endif
  52. this.disabled,
  53. this.inactiveColor,
  54. this.activeColor,
  55. this.size,
  56. this.labelDisabled,
  57. this.shape,
  58. this.iconSize,
  59. this.borderBottom,
  60. this.placement,
  61. this.labelColor,
  62. this.activeLabelColor,
  63. this.iconPlacement,
  64. this.plain,
  65. ]
  66. },
  67. bemClass() {
  68. // this.bem为一个computed变量,在mixin中
  69. return this.bem('radio-group', ['placement'])
  70. },
  71. },
  72. watch: {
  73. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  74. parentData() {
  75. if (this.children.length) {
  76. this.children.map(child => {
  77. // 判断子组件(u-radio)如果有init方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  78. if(this.$u.test.func(child.init)){
  79. child.init()
  80. }
  81. })
  82. }
  83. },
  84. },
  85. data() {
  86. return {
  87. }
  88. },
  89. created() {
  90. this.children = []
  91. },
  92. // #ifdef VUE3
  93. emits: ['update:modelValue', 'change'],
  94. // #endif
  95. methods: {
  96. // 将其他的radio设置为未选中的状态
  97. unCheckedOther(childInstance) {
  98. this.children.map(child => {
  99. // 所有子radio中,被操作组件实例的checked的值无需修改
  100. if (childInstance !== child) {
  101. child.checked = false
  102. }
  103. })
  104. const {
  105. name
  106. } = childInstance
  107. // 通过emit事件,设置父组件通过v-model双向绑定的值
  108. // #ifdef VUE2
  109. this.$emit('input', name)
  110. // #endif
  111. // #ifdef VUE3
  112. this.$emit('update:modelValue', name)
  113. // #endif
  114. // 发出事件
  115. this.$emit('change', name)
  116. },
  117. }
  118. }
  119. </script>
  120. <style lang="scss" scoped>
  121. @import "../../libs/css/components.scss";
  122. .u-radio-group {
  123. flex: 1;
  124. &--row {
  125. @include flex;
  126. flex-wrap: wrap;
  127. }
  128. &--column {
  129. @include flex(column);
  130. }
  131. }
  132. </style>