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.
 
 
 

106 regels
3.0 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: 'square'
  56. },
  57. // 图标的大小,单位rpx
  58. iconSize: {
  59. type: [String, Number],
  60. default: 20
  61. },
  62. // 每个checkbox占u-checkbox-group的宽度
  63. width: {
  64. type: String,
  65. default: 'auto'
  66. },
  67. // 是否每个checkbox都换行
  68. wrap: {
  69. type: Boolean,
  70. default: false
  71. }
  72. },
  73. provide() {
  74. return {
  75. radioGroup: this
  76. }
  77. },
  78. methods: {
  79. // 该方法有子组件radio调用,当一个radio被选中的时候,给父组件设置value值(props传递的value)
  80. setValue(val) {
  81. // 通过emit事件,设置父组件通过v-model双向绑定的值
  82. this.$emit('input', val);
  83. // 等待下一个周期再执行,因为this.$emit('input')作用于父组件,再反馈到子组件内部,需要时间
  84. this.$nextTick(function() {
  85. this.$emit('change', val);
  86. // 发出事件,用于在表单组件中嵌入radio的情况,进行验证
  87. // 将当前的值发送到 u-form-item 进行校验
  88. this.dispatch('u-form-item', 'on-form-change', val);
  89. });
  90. }
  91. }
  92. }
  93. </script>
  94. <style lang="scss" scoped>
  95. @import "../../libs/css/style.components.scss";
  96. .u-radio-group {
  97. /* #ifndef MP */
  98. display: inline-flex;
  99. flex-wrap: wrap;
  100. /* #endif */
  101. }
  102. </style>