checkbox.uvue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <script>
  2. type ItemType = {
  3. value : string
  4. name : string
  5. checked : boolean
  6. }
  7. export default {
  8. data() {
  9. return {
  10. items: [
  11. {
  12. value: 'CHN',
  13. name: '中国',
  14. checked: true,
  15. },
  16. {
  17. value: 'USA',
  18. name: '美国',
  19. checked: false,
  20. },
  21. {
  22. value: 'BRA',
  23. name: '巴西',
  24. checked: false,
  25. },
  26. {
  27. value: 'JPN',
  28. name: '日本',
  29. checked: false,
  30. },
  31. {
  32. value: 'ENG',
  33. name: '英国',
  34. checked: false,
  35. },
  36. {
  37. value: 'FRA',
  38. name: '法国',
  39. checked: false,
  40. },
  41. ] as ItemType[],
  42. testEvent: false,
  43. text: '未选中',
  44. wrapText: 'uni-app x,终极跨平台方案\nuts,大一统语言',
  45. value: [] as string[],
  46. disabled: true,
  47. checked: true,
  48. color: '#007aff',
  49. iconColor: '#211cfe',
  50. foreColor: '#ff0000',
  51. // 组件属性 autotest
  52. checked_boolean: false,
  53. disabled_boolean: false,
  54. color_input: "#007aff",
  55. backgroundColor_input: "#ffffff",
  56. borderColor_input: "#d1d1d1",
  57. activeBackgroundColor_input: "#ffffff",
  58. activeBorderColor_input: "#d1d1d1",
  59. iconColor_input: "#007aff",
  60. foreColor_input: '#ff0000'
  61. }
  62. },
  63. methods: {
  64. checkboxChange: function (e : UniCheckboxGroupChangeEvent) {
  65. // 自动化测试
  66. if ((e.target?.tagName ?? '') == 'CHECKBOX-GROUP' && e.type === 'change') {
  67. this.testEvent = true
  68. }
  69. const selectedNames : string[] = []
  70. this.items.forEach((item) => {
  71. if (e.detail.value.includes(item.value)) {
  72. selectedNames.push(item.name)
  73. }
  74. })
  75. uni.showToast({
  76. icon: 'none',
  77. title: '当前选中:' + selectedNames.join(','),
  78. })
  79. },
  80. testChange: function (e : UniCheckboxGroupChangeEvent) {
  81. this.value = e.detail.value
  82. },
  83. checkbox_click() { console.log("组件被点击时触发") },
  84. checkbox_touchstart() { console.log("手指触摸动作开始") },
  85. checkbox_touchmove() { console.log("手指触摸后移动") },
  86. checkbox_touchcancel() { console.log("手指触摸动作被打断,如来电提醒,弹窗") },
  87. checkbox_touchend() { console.log("手指触摸动作结束") },
  88. checkbox_tap() { console.log("手指触摸后马上离开") },
  89. checkbox_longpress() { console.log("如果一个组件被绑定了 longpress 事件,那么当用户长按这个组件时,该事件将会被触发。") },
  90. change_checked_boolean(checked : boolean) { this.checked_boolean = checked },
  91. change_disabled_boolean(checked : boolean) { this.disabled_boolean = checked },
  92. confirm_color_input(value : string) { this.color_input = value },
  93. confirm_backgroundColor_input(value : string) { this.backgroundColor_input = value },
  94. confirm_borderColor_input(value : string) { this.borderColor_input = value },
  95. confirm_activeBackgroundColor_input(value : string) { this.activeBackgroundColor_input = value },
  96. confirm_activeBorderColor_input(value : string) { this.activeBorderColor_input = value },
  97. confirm_iconColor_input(value : string) { this.iconColor_input = value },
  98. confirm_foreColor_input(value : string) { this.foreColor_input = value }
  99. }
  100. }
  101. </script>
  102. <template>
  103. <view class="main">
  104. <checkbox :disabled="disabled_boolean" :checked="checked_boolean" :color="color_input" :iconColor="iconColor_input"
  105. :foreColor="foreColor_input" :backgroundColor="backgroundColor_input" :borderColor="borderColor_input"
  106. :activeBackgroundColor="activeBackgroundColor_input" :activeBorderColor="activeBorderColor_input"
  107. @click="checkbox_click" @touchstart="checkbox_touchstart" @touchmove="checkbox_touchmove"
  108. @touchcancel="checkbox_touchcancel" @touchend="checkbox_touchend" @tap="checkbox_tap"
  109. @longpress="checkbox_longpress"><text>uni-app-x</text></checkbox>
  110. </view>
  111. <scroll-view style="flex: 1">
  112. <view class="content">
  113. <page-head title="组件属性"></page-head>
  114. <boolean-data :defaultValue="false" title="当前是否选中,可用来设置默认选中" @change="change_checked_boolean"></boolean-data>
  115. <boolean-data :defaultValue="false" title="是否禁用" @change="change_disabled_boolean"></boolean-data>
  116. <input-data defaultValue="#007aff" title="checkbox的颜色" type="text" @confirm="confirm_color_input"></input-data>
  117. <input-data defaultValue="#ffffff" title="checkbox默认的背景颜色" type="text"
  118. @confirm="confirm_backgroundColor_input"></input-data>
  119. <input-data defaultValue="#d1d1d1" title="checkbox默认的边框颜色" type="text"
  120. @confirm="confirm_borderColor_input"></input-data>
  121. <input-data defaultValue="#ffffff" title="checkbox选中时的背景颜色" type="text"
  122. @confirm="confirm_activeBackgroundColor_input"></input-data>
  123. <input-data defaultValue="#d1d1d1" title="checkbox选中时的边框颜色" type="text"
  124. @confirm="confirm_activeBorderColor_input"></input-data>
  125. <input-data defaultValue="#007aff" title="iconColor: checkbox的图标颜色,优先级大于color属性" type="text"
  126. @confirm="confirm_iconColor_input"></input-data>
  127. <input-data defaultValue="#ff0000" title="foreColor: checkbox的图标颜色,优先级大于color属性" type="text"
  128. @confirm="confirm_foreColor_input"></input-data>
  129. </view>
  130. <view>
  131. <page-head title="默认及使用"></page-head>
  132. <view class="uni-padding-wrap uni-common-mt">
  133. <view class="uni-title uni-common-mt">
  134. <text class="uni-title-text"> 默认样式 </text>
  135. </view>
  136. <view>
  137. <checkbox-group class="uni-flex uni-row checkbox-group" @change="testChange" style="flex-wrap: wrap">
  138. <checkbox value="cb" :checked="checked" :color="color" :iconColor="iconColor" :foreColor="foreColor"
  139. style="margin-right: 15px" class="checkbox cb">选中
  140. </checkbox>
  141. <checkbox value="cb1" style="margin-right: 15px" class="checkbox cb1">{{ text }}</checkbox>
  142. <checkbox value="cb2" :disabled="disabled" class="checkbox cb2">禁用</checkbox>
  143. <checkbox value="cb3" style="margin-top: 10px" class="checkbox cb3">
  144. {{ wrapText }}
  145. </checkbox>
  146. </checkbox-group>
  147. </view>
  148. <view class="uni-title uni-common-mt">
  149. <text class="uni-title-text"> 不同颜色和尺寸的checkbox </text>
  150. </view>
  151. <view>
  152. <checkbox-group class="uni-flex uni-row checkbox-group">
  153. <checkbox value="cb1" :checked="true" color="#FFCC33" style="transform: scale(0.7); margin-right: 15px"
  154. class="checkbox">选中
  155. </checkbox>
  156. <checkbox value="cb" color="#FFCC33" style="transform: scale(0.7)" class="checkbox">未选中</checkbox>
  157. </checkbox-group>
  158. </view>
  159. <view class="uni-title uni-common-mt">
  160. <text class="uni-title-text"> 两端对齐样式测试 </text>
  161. </view>
  162. <view>
  163. <checkbox-group class="uni-flex uni-row checkbox-group">
  164. <checkbox class="justify-test">justify-content样式测试</checkbox>
  165. </checkbox-group>
  166. </view>
  167. </view>
  168. <view class="uni-padding-wrap">
  169. <view class="uni-title uni-common-mt">
  170. <text class="uni-title-text"> 推荐展示样式 </text>
  171. </view>
  172. </view>
  173. <view class="uni-list uni-common-pl">
  174. <checkbox-group @change="checkboxChange" class="checkbox-group" id="trigger-change">
  175. <checkbox class="uni-list-cell uni-list-cell-pd checkbox" v-for="(item, index) in items" :key="item.value"
  176. :value="item.value" :checked="item.checked" :class="[
  177. index < items.length - 1 ? 'uni-list-cell-line' : '',
  178. 'checkbox-item-' + index,
  179. ]">
  180. {{ item.name }}
  181. </checkbox>
  182. </checkbox-group>
  183. </view>
  184. </view>
  185. </scroll-view>
  186. </template>
  187. <style>
  188. .main {
  189. max-height: 250px;
  190. padding: 5px 0;
  191. border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  192. flex-direction: row;
  193. justify-content: center;
  194. }
  195. .main .list-item {
  196. width: 100%;
  197. height: 100px;
  198. border: 1px solid #666;
  199. }
  200. .uni-list-cell {
  201. justify-content: flex-start;
  202. }
  203. .justify-test {
  204. width: 100%;
  205. justify-content: space-between;
  206. }
  207. </style>