show-action-sheet.uvue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1;">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-list">
  8. <radio-group @change="radioChange">
  9. <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
  10. :class="index < items.length - 1 ? 'uni-list-cell-line': ''" :value="item.value" :checked="index === current">
  11. {{item.name}}
  12. </radio>
  13. </radio-group>
  14. </view>
  15. <view class="uni-list">
  16. <view class="uni-list-cell uni-list-cell-pd">
  17. <view class="uni-list-cell-db">自定义 titleColor</view>
  18. <switch :checked="titleColorCustom" @change="titleColorChange" />
  19. </view>
  20. <view class="uni-list-cell uni-list-cell-pd">
  21. <view class="uni-list-cell-db">自定义 itemColor</view>
  22. <switch :checked="itemColorCustom" @change="itemColorChange" />
  23. </view>
  24. <view class="uni-list-cell uni-list-cell-pd">
  25. <view class="uni-list-cell-db">超长文本和空文本 item</view>
  26. <switch :checked="itemContentLarge" @change="itemContentLargeChange" />
  27. </view>
  28. <!-- #ifndef MP -->
  29. <view class="uni-list-cell uni-list-cell-pd">
  30. <view class="uni-list-cell-db">超过6个 item</view>
  31. <switch :checked="itemNumLargeSelect" @change="itemNumLargeChange" />
  32. </view>
  33. <view class="uni-list-cell uni-list-cell-pd">
  34. <view class="uni-list-cell-db">自定义 cancelText</view>
  35. <switch :checked="cancelTextCustom" @change="cancelTextChange" />
  36. </view>
  37. <view class="uni-list-cell uni-list-cell-pd">
  38. <view class="uni-list-cell-db">自定义 cancelColor</view>
  39. <switch :checked="cancelColorCustom" @change="cancelColorChange" />
  40. </view>
  41. <view class="uni-list-cell uni-list-cell-pd">
  42. <view class="uni-list-cell-db">自定义 backgroundColor</view>
  43. <switch :checked="backgroundColorCustom" @change="backgroundColorChange" />
  44. </view>
  45. <!-- #endif -->
  46. </view>
  47. <view class="uni-padding-wrap">
  48. <view class="uni-btn-v">
  49. <button class="uni-btn-v" type="default" @tap="showActionSheet" id="btn-action-sheet-show">弹出actionSheet</button>
  50. <button class="uni-btn-v uni-btn" type="default" @tap="showActionSheetAndShowAgainInCallback" id="btn-action-sheet-show">showActionSheet 并在回调中再次 showActionSheet</button>
  51. </view>
  52. </view>
  53. </view>
  54. <!-- #ifdef APP -->
  55. </scroll-view>
  56. <!-- #endif -->
  57. </template>
  58. <script lang="uts">
  59. import { state, setLifeCycleNum } from '@/store/index.uts'
  60. type ItemType = {
  61. value : string,
  62. name : string,
  63. }
  64. export default {
  65. data() {
  66. return {
  67. title: 'action-sheet',
  68. titleColorCustom: false,
  69. itemColorCustom: false,
  70. itemContentLarge: false,
  71. itemNumLargeSelect: false,
  72. cancelTextCustom: false,
  73. cancelColorCustom: false,
  74. backgroundColorCustom: false,
  75. showErrorToast: true,
  76. items: [{
  77. value: '标题',
  78. name: '有标题'
  79. },
  80. {
  81. value: '',
  82. name: '无标题'
  83. },
  84. {
  85. value: '超长标题测试内容,测试超过显示最大范围之后的样式-超长标题测试内容,测试超过显示最大范围之后的样式',
  86. name: '超长标题'
  87. }
  88. ] as ItemType[],
  89. current: 0,
  90. // #ifdef APP
  91. originTheme: null as string | null,
  92. // #endif
  93. }
  94. },
  95. onLoad() {
  96. uni.showActionSheet({
  97. title: "onLoad 调用示例,请手动取消",
  98. itemList: ['item1', 'item2'],
  99. fail: (res) => {
  100. // 自动化测试
  101. setLifeCycleNum(state.lifeCycleNum + 1)
  102. console.log('onLoad showActionSheet fail', res)
  103. },
  104. complete: (res) => {
  105. // 自动化测试
  106. setLifeCycleNum(state.lifeCycleNum + 1)
  107. console.log('onLoad showActionSheet complete', res)
  108. }
  109. })
  110. // #ifdef APP
  111. this.originTheme = uni.getSystemInfoSync().appTheme
  112. // #endif
  113. },
  114. methods: {
  115. //自动化测试例专用
  116. // #ifdef APP
  117. setThemeAuto() {
  118. uni.setAppTheme({
  119. theme: 'auto'
  120. })
  121. },
  122. resetTheme() {
  123. const originTheme = this.originTheme
  124. if(originTheme != null){
  125. uni.setAppTheme({
  126. theme: originTheme
  127. })
  128. }
  129. },
  130. // #endif
  131. radioChange(e : UniRadioGroupChangeEvent) {
  132. for (let i = 0; i < this.items.length; i++) {
  133. if (this.items[i].value === e.detail.value) {
  134. this.current = i;
  135. break;
  136. }
  137. }
  138. },
  139. titleColorChange(e : UniSwitchChangeEvent) {
  140. this.titleColorCustom = e.detail.value
  141. },
  142. itemContentLargeChange: function (e : UniSwitchChangeEvent) {
  143. this.itemContentLarge = e.detail.value
  144. },
  145. itemColorChange: function (e : UniSwitchChangeEvent) {
  146. this.itemColorCustom = e.detail.value
  147. },
  148. itemNumLargeChange: function (e : UniSwitchChangeEvent) {
  149. this.itemNumLargeSelect = e.detail.value
  150. },
  151. cancelTextChange: function (e : UniSwitchChangeEvent) {
  152. this.cancelTextCustom = e.detail.value
  153. },
  154. cancelColorChange: function (e : UniSwitchChangeEvent) {
  155. this.cancelColorCustom = e.detail.value
  156. },
  157. backgroundColorChange: function (e : UniSwitchChangeEvent) {
  158. this.backgroundColorCustom = e.detail.value
  159. },
  160. showActionSheet() {
  161. const options: ShowActionSheetOptions = {
  162. title: this.items[this.current].value,
  163. itemList: ['item1', 'item2', 'item3', 'item4'],
  164. success: (res) => {
  165. console.log(res.tapIndex);
  166. uni.showToast({
  167. title: "点击了第" + res.tapIndex + "个选项",
  168. icon: "none"
  169. })
  170. },
  171. fail: (error) => {
  172. if (this.showErrorToast) {
  173. uni.showToast({
  174. title: error.errMsg,
  175. icon: "none"
  176. })
  177. }
  178. console.log(error);
  179. }
  180. }
  181. if (this.itemContentLarge) {
  182. options.itemList = ['两个黄鹂鸣翠柳,一行白鹭上青天。窗含西岭千秋雪,门泊东吴万里船', '水光潋滟晴方好,山色空蒙雨亦奇。 欲把西湖比西子,淡妆浓抹总相宜', '']
  183. }
  184. if (this.itemNumLargeSelect) {
  185. // 大量选项测试,不能超过6个元素 https://uniapp.dcloud.net.cn/api/ui/prompt.html#showactionsheet
  186. const arr: string[] = []
  187. for(let i = 0; i < 10; i++){
  188. arr.push(`两个黄鹂鸣翠柳,一行白鹭上青天 ${i+1}`)
  189. }
  190. options.itemList = arr
  191. }
  192. if(this.titleColorCustom){
  193. options.titleColor = '#007AFF'
  194. }
  195. if(this.itemColorCustom){
  196. options.itemColor = '#ff00ff'
  197. }
  198. if(this.cancelTextCustom){
  199. options.cancelText = 'custom cancel'
  200. }
  201. if(this.cancelColorCustom){
  202. options.cancelColor = '#007AFF'
  203. }
  204. if(this.backgroundColorCustom){
  205. options.backgroundColor = '#ccc'
  206. }
  207. uni.showActionSheet(options)
  208. },
  209. showActionSheetAndShowAgainInCallback(){
  210. uni.showActionSheet({
  211. title: '第一个',
  212. itemList: ['1','2','3'],
  213. complete(){
  214. uni.showActionSheet({
  215. title: '第二个',
  216. itemList: ['a','b','c'],
  217. complete: (res) => {
  218. console.log('showActionSheetAndShowAgainInCallback complete', res)
  219. }
  220. })
  221. }
  222. })
  223. },
  224. // 自动化测试
  225. getLifeCycleNum(){
  226. return state.lifeCycleNum
  227. },
  228. // 自动化测试
  229. // #ifdef WEB
  230. closeWebActionSheet(){
  231. document.querySelector('.uni-action-sheet_dialog__cell').click()
  232. },
  233. // #endif
  234. // 自动化测试
  235. hideActionSheet(){
  236. uni.hideActionSheet()
  237. }
  238. }
  239. }
  240. </script>