show-action-sheet.uvue 6.5 KB
Newer Older
1 2 3
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;">
4
  <!-- #endif -->
W
微调  
wanganxp 已提交
5 6 7 8 9 10 11 12 13
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-list">
      <radio-group @change="radioChange">
        <radio class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items" :key="item.value"
          :class="index < items.length - 1 ? 'uni-list-cell-line': ''" :value="item.value" :checked="index === current">
          {{item.name}}
        </radio>
      </radio-group>
Y
yurj26 已提交
14
    </view>
W
微调  
wanganxp 已提交
15 16
    <view class="uni-list">
      <view class="uni-list-cell uni-list-cell-pd">
17 18 19 20 21
        <view class="uni-list-cell-db">自定义 titleColor</view>
        <switch :checked="titleColorCustom" @change="titleColorChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">自定义 itemColor</view>
W
微调  
wanganxp 已提交
22 23 24
        <switch :checked="itemColorCustom" @change="itemColorChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
25
        <view class="uni-list-cell-db">超长文本和空文本 item</view>
W
微调  
wanganxp 已提交
26 27 28
        <switch :checked="itemContentLarge" @change="itemContentLargeChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
29
        <view class="uni-list-cell-db">超过6个 item</view>
W
微调  
wanganxp 已提交
30 31
        <switch :checked="itemNumLargeSelect" @change="itemNumLargeChange" />
      </view>
32 33 34 35 36 37 38 39 40 41 42 43
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">自定义 cancelText</view>
        <switch :checked="cancelTextCustom" @change="cancelTextChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">自定义 cancelColor</view>
        <switch :checked="cancelColorCustom" @change="cancelColorChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">自定义 backgroundColor</view>
        <switch :checked="backgroundColorCustom" @change="backgroundColorChange" />
      </view>
W
微调  
wanganxp 已提交
44 45 46
    </view>
    <view class="uni-padding-wrap">
      <view class="uni-btn-v">
47
        <button class="uni-btn-v" type="default" @tap="showActionSheet" id="btn-action-sheet-show">弹出action
DCloud-WZF's avatar
DCloud-WZF 已提交
48
          sheet</button>
W
微调  
wanganxp 已提交
49 50
      </view>
    </view>
51 52 53
  </view>
  <!-- #ifdef APP -->
  </scroll-view>
54
  <!-- #endif -->
Y
yurj26 已提交
55
</template>
Y
yurj26 已提交
56
<script lang="uts">
W
微调  
wanganxp 已提交
57 58 59 60 61 62 63 64
  type ItemType = {
    value : string,
    name : string,
  }
  export default {
    data() {
      return {
        title: 'action-sheet',
65
        titleColorCustom: false,
W
微调  
wanganxp 已提交
66 67
        itemColorCustom: false,
        itemContentLarge: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
68
        itemNumLargeSelect: false,
69 70 71
        cancelTextCustom: false,
        cancelColorCustom: false,
        backgroundColorCustom: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
72
        showErrorToast: true,
W
微调  
wanganxp 已提交
73 74 75
        items: [{
          value: '标题',
          name: '有标题'
Y
yurj26 已提交
76
        },
W
微调  
wanganxp 已提交
77 78 79
        {
          value: '',
          name: '无标题'
Y
yurj26 已提交
80
        },
W
微调  
wanganxp 已提交
81 82 83 84 85 86 87 88
        {
          value: '超长标题测试内容,测试超过显示最大范围之后的样式-超长标题测试内容,测试超过显示最大范围之后的样式',
          name: '超长标题'
        }
        ] as ItemType[],
        current: 0,
      }
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
89 90 91 92 93 94 95 96 97 98
    onLoad() {
      uni.showActionSheet({
        title: "onLoad 调用示例,请手动取消",
        itemList: ['item1', 'item2'],
      })
    },
    methods: {
      //自动化测试例专用
      jest_getWindowInfo() : GetWindowInfoResult {
        return uni.getWindowInfo();
杜庆泉's avatar
杜庆泉 已提交
99
      },
H
hdx 已提交
100
      radioChange(e : UniRadioGroupChangeEvent) {
W
微调  
wanganxp 已提交
101 102 103 104 105 106 107
        for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].value === e.detail.value) {
            this.current = i;
            break;
          }
        }
      },
108 109 110
      titleColorChange(e : UniSwitchChangeEvent) {
        this.titleColorCustom = e.detail.value
      },
H
hdx 已提交
111
      itemContentLargeChange: function (e : UniSwitchChangeEvent) {
W
微调  
wanganxp 已提交
112 113
        this.itemContentLarge = e.detail.value
      },
H
hdx 已提交
114
      itemColorChange: function (e : UniSwitchChangeEvent) {
W
微调  
wanganxp 已提交
115 116
        this.itemColorCustom = e.detail.value
      },
H
hdx 已提交
117
      itemNumLargeChange: function (e : UniSwitchChangeEvent) {
W
微调  
wanganxp 已提交
118 119
        this.itemNumLargeSelect = e.detail.value
      },
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      cancelTextChange: function (e : UniSwitchChangeEvent) {
        this.cancelTextCustom = e.detail.value
      },
      cancelColorChange: function (e : UniSwitchChangeEvent) {
        this.cancelColorCustom = e.detail.value
      },
      backgroundColorChange: function (e : UniSwitchChangeEvent) {
        this.backgroundColorCustom = e.detail.value
      },
      showActionSheet() {
        const options: ShowActionSheetOptions = {
          title: this.items[this.current].value,
          itemList: ['item1', 'item2', 'item3', 'item4'],
          success: (res) => {
            console.log(res.tapIndex);
            uni.showToast({
              title: "点击了第" + res.tapIndex + "个选项",
              icon: "none"
            })
          },
          fail: (error) => {
            if (this.showErrorToast) {
              uni.showToast({
                title: error.errMsg,
                icon: "none"
              })
            }
            console.log(error);
          }
        }
W
微调  
wanganxp 已提交
150
        if (this.itemContentLarge) {
151
          options.itemList = ['两个黄鹂鸣翠柳,一行白鹭上青天。窗含西岭千秋雪,门泊东吴万里船', '水光潋滟晴方好,山色空蒙雨亦奇。 欲把西湖比西子,淡妆浓抹总相宜', '']
DCloud-WZF's avatar
DCloud-WZF 已提交
152
        }
杜庆泉's avatar
杜庆泉 已提交
153
        if (this.itemNumLargeSelect) {
W
微调  
wanganxp 已提交
154
          // 大量选项测试,不能超过6个元素 https://uniapp.dcloud.net.cn/api/ui/prompt.html#showactionsheet
155 156 157
          const arr: string[] = []
          for(let i = 0; i < 10; i++){
            arr.push(`两个黄鹂鸣翠柳,一行白鹭上青天 ${i+1}`)
W
微调  
wanganxp 已提交
158
          }
159
          options.itemList = arr
W
微调  
wanganxp 已提交
160
        }
161 162 163 164 165 166 167 168
        if(this.titleColorCustom){
          options.titleColor = '#007AFF'
        }
        if(this.itemColorCustom){
          options.itemColor = '#ff00ff'
        }
        if(this.cancelTextCustom){
          options.cancelText = 'custom cancel'
Y
yurj26 已提交
169
        }
170 171 172 173 174 175 176
        if(this.cancelColorCustom){
          options.cancelColor = '#007AFF'
        }
        if(this.backgroundColorCustom){
          options.backgroundColor = '#ccc'
        }
        uni.showActionSheet(options)
W
微调  
wanganxp 已提交
177
      },
178 179 180 181
      // 自动化测试
      hideActionSheet(){
        uni.hideActionSheet()
      }
Y
yurj26 已提交
182
    }
W
微调  
wanganxp 已提交
183
  }
184
</script>