index.vue 4.5 KB

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