u-radio-group.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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} active-color 选中时的颜色,应用到所有子Radio组件(默认#2979ff)
  14. * @event {Function} change 任一个radio状态发生变化时触发
  15. * @property {String} width 宽度,需带单位
  16. * @property {Boolean} wrap 是否每个radio都换行(默认false)
  17. * @example <u-radio-group v-model="value"></u-radio-group>
  18. */
  19. export default {
  20. name: "u-radio-group",
  21. mixins: [Emitter],
  22. props: {
  23. // 是否禁用所有单选框
  24. disabled: {
  25. type: Boolean,
  26. default: false
  27. },
  28. // 匹配某一个radio组件,如果某个radio的name值等于此值,那么这个radio就被会选中
  29. value: {
  30. type: [String, Number],
  31. default: ''
  32. },
  33. // 选中状态下的颜色
  34. activeColor: {
  35. type: String,
  36. default: '#2979ff'
  37. },
  38. // 组件的整体大小
  39. size: {
  40. type: [String, Number],
  41. default: 34
  42. },
  43. // 每个checkbox占u-checkbox-group的宽度
  44. width: {
  45. type: String,
  46. default: 'auto'
  47. },
  48. // 是否每个checkbox都换行
  49. wrap: {
  50. type: Boolean,
  51. default: false
  52. }
  53. },
  54. provide() {
  55. return {
  56. radioGroup: this
  57. }
  58. },
  59. methods: {
  60. // 该方法有子组件radio调用,当一个radio被选中的时候,给父组件设置value值(props传递的value)
  61. setValue(val) {
  62. // 通过emit事件,设置父组件通过v-model双向绑定的值
  63. this.$emit('input', val);
  64. // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
  65. this.$nextTick(function() {
  66. this.$emit('change', this.value);
  67. // 发出事件,用于在表单组件中嵌入checkbox的情况,进行验证
  68. // 将当前的值发送到 u-form-item 进行校验
  69. this.dispatch('u-form-item', 'on-form-change', this.value);
  70. });
  71. }
  72. }
  73. }
  74. </script>
  75. <style lang="scss" scoped>
  76. .u-radio-group {
  77. /* #ifndef MP */
  78. display: inline-flex;
  79. flex-wrap: wrap;
  80. /* #endif */
  81. }
  82. </style>