index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="u-demo">
  3. <view class="u-demo-wrap">
  4. <view class="u-demo-title">演示效果</view>
  5. <view class="u-demo-area">
  6. <view class="">
  7. <u-radio-group :shape="shape" :size="size" :width="width" :wrap="wrap" v-model="value" @change="radioGroupChange" :activeColor="activeColor">
  8. <u-radio @change="radioChange" v-for="(item, index) in list" :disabled="item.disabled"
  9. :key="index" :name="item.name"
  10. >{{item.name}}</u-radio>
  11. </u-radio-group>
  12. </view>
  13. <view class="u-demo-result-line">
  14. {{value ? `选中了"${result}"` : '请选择'}}
  15. </view>
  16. </view>
  17. </view>
  18. <view class="u-config-wrap">
  19. <view class="u-config-title u-border-bottom">
  20. 参数配置
  21. </view>
  22. <view class="u-config-item">
  23. <view class="u-item-title">形状</view>
  24. <u-subsection current="1" :list="['方形', '圆形']" @change="shapeChange"></u-subsection>
  25. </view>
  26. <view class="u-config-item">
  27. <view class="u-item-title">整体大小(单位rpx)</view>
  28. <u-subsection current="1" :list="['30', '40', '50']" @change="sizeChange"></u-subsection>
  29. </view>
  30. <view class="u-config-item">
  31. <view class="u-item-title">激活颜色</view>
  32. <u-subsection :list="['primary', 'error', 'warning', 'success', 'info']" @change="activeColorChange"></u-subsection>
  33. </view>
  34. <view class="u-config-item">
  35. <view class="u-item-title">每个占一行</view>
  36. <u-subsection current="1" :list="['是', '否']" @change="wrapChange"></u-subsection>
  37. </view>
  38. <view class="u-config-item">
  39. <view class="u-item-title">每个宽度50%</view>
  40. <u-subsection current="1" :list="['是', '否']" @change="widthChange"></u-subsection>
  41. </view>
  42. <view class="u-config-item">
  43. <view class="u-item-title">默认选中第一个</view>
  44. <u-subsection current="1" :list="['是', '否']" @change="defaultChooseChange"></u-subsection>
  45. </view>
  46. <view class="u-config-item">
  47. <view class="u-item-title">禁用第一个</view>
  48. <u-subsection current="1" :list="['是', '否']" @change="disabledChange"></u-subsection>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. data() {
  56. return {
  57. list: [
  58. {
  59. name: '荔枝',
  60. checked: true,
  61. disabled: false
  62. },
  63. {
  64. name: '香蕉',
  65. checked: false,
  66. disabled: false
  67. },
  68. {
  69. name: '橙子',
  70. checked: false,
  71. disabled: false
  72. },
  73. {
  74. name: '草莓',
  75. checked: false,
  76. disabled: false
  77. }
  78. ],
  79. disabled: false,
  80. result: '荔枝',
  81. shape: 'circle',
  82. value: '荔枝',
  83. activeColor: '#2979ff',
  84. size: 34,
  85. wrap: false,
  86. width: 'auto'
  87. }
  88. },
  89. methods: {
  90. shapeChange(index) {
  91. this.shape = index == 0 ? 'square' : 'circle';
  92. },
  93. sizeChange(index) {
  94. this.size = index == 0 ? 30 : index == 1 ? 40 : 50;
  95. },
  96. defaultChooseChange(index) {
  97. // 特别处理对第一个选的选中的情况,涉及到提示语,选中状态等
  98. // 实际开发中不会存在这些情况,只是演示用
  99. if(index == 0) {
  100. this.result = this.value = this.list[0].name;
  101. } else {
  102. if(this.value == this.list[0].name) {
  103. this.result = this.value = '';
  104. }
  105. }
  106. },
  107. activeColorChange(index) {
  108. // 如果用户尚未勾选任何radio,切换颜色时,默认选中第一个让用户看到效果,因为勾选了才有效果
  109. if(!this.result) this.result = this.value = this.list[0].name;
  110. let theme = index == 0 ? 'primary' : index == 1 ? 'error' : index == 2 ? 'warning' : index == 3 ? 'success' : 'info';
  111. this.activeColor = this.$u.color[theme];
  112. },
  113. disabledChange(index) {
  114. if(index == 0) {
  115. this.list[0].disabled = true;
  116. } else {
  117. this.list[0].disabled = false;
  118. }
  119. },
  120. // 选中某个单选框时,由radio时触发
  121. radioChange(e) {
  122. //console.log(e);
  123. },
  124. // 选中任一radio时,由radio-group触发
  125. radioGroupChange(e) {
  126. this.result = e;
  127. //console.log(e);
  128. },
  129. widthChange(index) {
  130. this.width = index == 0 ? '50%' : '';
  131. },
  132. wrapChange(index) {
  133. this.wrap = !index;
  134. }
  135. }
  136. }
  137. </script>
  138. <style scoped lang="scss">
  139. .u-demo {}
  140. </style>