show-action-sheet.uvue 5.2 KB
Newer Older
1 2 3 4
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;">
  <!-- #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 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    <view class="uni-list">
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">自定义itemColor</view>
        <switch :checked="itemColorCustom" @change="itemColorChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">超长文本和空文本item</view>
        <switch :checked="itemContentLarge" @change="itemContentLargeChange" />
      </view>
      <view class="uni-list-cell uni-list-cell-pd">
        <view class="uni-list-cell-db">超过6个item</view>
        <switch :checked="itemNumLargeSelect" @change="itemNumLargeChange" />
      </view>
    </view>
    <view class="uni-padding-wrap">
      <view class="uni-btn-v">
DCloud-WZF's avatar
DCloud-WZF 已提交
31 32
        <button class="uni-btn-v" type="default" @tap="actionSheetTap" id="btn-action-sheet-show">弹出action
          sheet</button>
W
微调  
wanganxp 已提交
33 34
      </view>
    </view>
35 36 37 38
  </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
Y
yurj26 已提交
39
</template>
Y
yurj26 已提交
40
<script lang="uts">
W
微调  
wanganxp 已提交
41 42 43 44 45 46 47 48 49 50
  type ItemType = {
    value : string,
    name : string,
  }
  export default {
    data() {
      return {
        title: 'action-sheet',
        itemColorCustom: false,
        itemContentLarge: false,
DCloud-WZF's avatar
DCloud-WZF 已提交
51 52
        itemNumLargeSelect: false,
        showErrorToast: true,
W
微调  
wanganxp 已提交
53 54 55
        items: [{
          value: '标题',
          name: '有标题'
Y
yurj26 已提交
56
        },
W
微调  
wanganxp 已提交
57 58 59
        {
          value: '',
          name: '无标题'
Y
yurj26 已提交
60
        },
W
微调  
wanganxp 已提交
61 62 63 64 65 66 67 68
        {
          value: '超长标题测试内容,测试超过显示最大范围之后的样式-超长标题测试内容,测试超过显示最大范围之后的样式',
          name: '超长标题'
        }
        ] as ItemType[],
        current: 0,
      }
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
69 70 71 72 73 74 75 76 77 78
    onLoad() {
      uni.showActionSheet({
        title: "onLoad 调用示例,请手动取消",
        itemList: ['item1', 'item2'],
      })
    },
    methods: {
      //自动化测试例专用
      jest_getWindowInfo() : GetWindowInfoResult {
        return uni.getWindowInfo();
杜庆泉's avatar
杜庆泉 已提交
79
      },
H
hdx 已提交
80
      radioChange(e : UniRadioGroupChangeEvent) {
W
微调  
wanganxp 已提交
81 82 83 84 85 86 87
        for (let i = 0; i < this.items.length; i++) {
          if (this.items[i].value === e.detail.value) {
            this.current = i;
            break;
          }
        }
      },
H
hdx 已提交
88
      itemContentLargeChange: function (e : UniSwitchChangeEvent) {
W
微调  
wanganxp 已提交
89 90
        this.itemContentLarge = e.detail.value
      },
H
hdx 已提交
91
      itemColorChange: function (e : UniSwitchChangeEvent) {
W
微调  
wanganxp 已提交
92 93
        this.itemColorCustom = e.detail.value
      },
H
hdx 已提交
94
      itemNumLargeChange: function (e : UniSwitchChangeEvent) {
W
微调  
wanganxp 已提交
95 96 97 98 99 100 101 102
        this.itemNumLargeSelect = e.detail.value
      },
      actionSheetTap() {

        let itemInfo = ['item1', 'item2', 'item3', 'item4']

        if (this.itemContentLarge) {
          itemInfo = ['两个黄鹂鸣翠柳,一行白鹭上青天。窗含西岭千秋雪,门泊东吴万里船', '水光潋滟晴方好,山色空蒙雨亦奇。 欲把西湖比西子,淡妆浓抹总相宜', '']
DCloud-WZF's avatar
DCloud-WZF 已提交
103 104
        }

杜庆泉's avatar
杜庆泉 已提交
105
        if (this.itemNumLargeSelect) {
W
微调  
wanganxp 已提交
106
          // 大量选项测试,不能超过6个元素 https://uniapp.dcloud.net.cn/api/ui/prompt.html#showactionsheet
DCloud-WZF's avatar
DCloud-WZF 已提交
107
          itemInfo = []
W
微调  
wanganxp 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
          for (var i = 1; i <= 10; i++) {
            itemInfo.push('两个黄鹂鸣翠柳,一行白鹭上青天');
          }
        }

        const that = this
        if (this.itemColorCustom) {
          uni.showActionSheet({
            title: this.items[this.current].value,
            itemList: itemInfo,
            itemColor: "#ff00ff",
            success: (e) => {
              console.log(e.tapIndex);
              uni.showToast({
                title: "点击了第" + e.tapIndex + "个选项",
                icon: "none"
              })
            },
DCloud-WZF's avatar
DCloud-WZF 已提交
126 127 128 129 130 131
            fail: (e) => {
              if (this.showErrorToast) {
                uni.showToast({
                  title: e.errMsg,
                  icon: "none"
                })
132
              }
W
微调  
wanganxp 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
              console.log(e);
            }
          })
        } else {
          uni.showActionSheet({
            title: this.items[this.current].value,
            itemList: itemInfo,
            success: (e) => {
              console.log(e.tapIndex);
              uni.showToast({
                title: "点击了第" + e.tapIndex + "个选项",
                icon: "none"
              })
Y
yurj26 已提交
146
            },
W
微调  
wanganxp 已提交
147
            fail: (e) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
148 149 150 151 152 153 154
              console.log(e);
              if (this.showErrorToast) {
                uni.showToast({
                  title: e.errMsg,
                  icon: "none"
                })
              }
Y
yurj26 已提交
155
            }
W
微调  
wanganxp 已提交
156
          })
Y
yurj26 已提交
157
        }
W
微调  
wanganxp 已提交
158
      },
Y
yurj26 已提交
159
    }
W
微调  
wanganxp 已提交
160 161
  }
</script>