radio.uvue 7.5 KB

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