You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.5 KiB

2 months ago
  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';
  11. import { mpMixin } from '../../libs/mixin/mpMixin';
  12. import { mixin } from '../../libs/mixin/mixin';
  13. /**
  14. * radioRroup 单选框父组件
  15. * @description 单选框用于有一个选择用户只能选择其中一个的场景搭配u-radio使用
  16. * @tutorial https://ijry.github.io/uview-plus/components/radio.html
  17. * @property {String | Number | Boolean} value 绑定的值
  18. * @property {Boolean} disabled 是否禁用所有radio默认 false
  19. * @property {String} shape 外观形状shape-方形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. // #ifdef VUE3
  46. return [this.modelValue, this.disabled, this.inactiveColor, this.activeColor, this.size, this.labelDisabled, this.shape,
  47. this.iconSize, this.borderBottom, this.placement
  48. ]
  49. // #endif
  50. // #ifdef VUE2
  51. return [this.value, this.disabled, this.inactiveColor, this.activeColor, this.size, this.labelDisabled, this.shape,
  52. this.iconSize, this.borderBottom, this.placement
  53. ]
  54. // #endif
  55. },
  56. bemClass() {
  57. // this.bem为一个computed变量,在mixin中
  58. return this.bem('radio-group', ['placement'])
  59. },
  60. },
  61. watch: {
  62. // 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
  63. parentData() {
  64. if (this.children.length) {
  65. this.children.map(child => {
  66. // 判断子组件(u-radio)如果有init方法的话,就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
  67. typeof(child.init) === 'function' && child.init()
  68. })
  69. }
  70. },
  71. },
  72. data() {
  73. return {
  74. }
  75. },
  76. created() {
  77. this.children = []
  78. },
  79. // #ifdef VUE3
  80. emits: ['update:modelValue', 'change'],
  81. // #endif
  82. methods: {
  83. // 将其他的radio设置为未选中的状态
  84. unCheckedOther(childInstance) {
  85. this.children.map(child => {
  86. // 所有子radio中,被操作组件实例的checked的值无需修改
  87. if (childInstance !== child) {
  88. child.checked = false
  89. }
  90. })
  91. const {
  92. name
  93. } = childInstance
  94. // 通过emit事件,设置父组件通过v-model双向绑定的值
  95. // #ifdef VUE3
  96. this.$emit("update:modelValue", name);
  97. // #endif
  98. // #ifdef VUE2
  99. this.$emit("input", name);
  100. // #endif
  101. // 发出事件
  102. this.$emit('change', name)
  103. },
  104. }
  105. }
  106. </script>
  107. <style lang="scss" scoped>
  108. @import "../../libs/css/components.scss";
  109. .u-radio-group {
  110. flex: 1;
  111. &--row {
  112. /* #ifndef APP-NVUE */
  113. display: flex;
  114. /* #endif */
  115. flex-flow: row wrap;
  116. }
  117. &--column {
  118. @include flex(column);
  119. }
  120. }
  121. </style>