u-radio-group.vue 4.8 KB

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