action-sheet.uvue 3.5 KB
Newer Older
Y
yurj26 已提交
1 2 3
<template>
    <view>
        <page-head :title="title"></page-head>
杜庆泉's avatar
杜庆泉 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
		<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>
		</view>
		<view class="uni-list">
		  <view class="uni-list-cell uni-list-cell-pd">
		    <view class="uni-list-cell-db">自定义itemColor</view>
		    <switch :checked="itemColorCustom" />
		  </view>
		</view>
Y
yurj26 已提交
19 20 21 22 23 24 25 26
        <view class="uni-padding-wrap">
            <view class="uni-btn-v">
                <button class="target" type="default" @tap="actionSheetTap">弹出action sheet</button>
            </view>
        </view>
    </view>
</template>
<script lang="ts">
杜庆泉's avatar
杜庆泉 已提交
27 28 29 30
	type ItemType = {
	    value : string,
	    name : string,
	}
Y
yurj26 已提交
31 32 33 34
    export default {
        data() {
            return {
                title: 'action-sheet',
杜庆泉's avatar
杜庆泉 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
				itemColorCustom:false,
				items: [{
				    value: '标题',
				    name: '有标题'
				},
				{
				    value: '',
				    name: '无标题'
				},
				{
				    value: '超长标题测试内容,测试超过显示最大范围之后的样式-超长标题测试内容,测试超过显示最大范围之后的样式',
				    name: '超长标题'
				}
				] as ItemType[],
				current: 0,
Y
yurj26 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
                // #ifdef H5
                buttonRect: {},
                // #endif
            }
        },
        // #ifdef H5
        onReady() {
            this.getNodeInfo()
            window.addEventListener('resize', this.getNodeInfo)
        },
        beforeDestroy() {
            window.removeEventListener('resize', this.getNodeInfo)
        },
        // #endif
        methods: {
杜庆泉's avatar
杜庆泉 已提交
65 66 67 68 69 70 71 72
			radioChange(e : RadioGroupChangeEvent) {
			    for (let i = 0; i < this.items.length; i++) {
			        if (this.items[i].value === e.detail.value) {
			            this.current = i;
			            break;
			        }
			    }
			},
Y
yurj26 已提交
73
            actionSheetTap() {
杜庆泉's avatar
杜庆泉 已提交
74
				console.log("itemColorCustom  === " + this.itemColorCustom)
Y
yurj26 已提交
75 76
                const that = this
                uni.showActionSheet({
杜庆泉's avatar
杜庆泉 已提交
77
                    title: this.items[this.current].value,
Y
yurj26 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
                    itemList: ['item1', 'item2', 'item3', 'item4'],
                    // #ifdef H5
                    popover: {
                        // 104: navbar + topwindow 高度,暂时 fix createSelectorQuery 在 pc 上获取 top 不准确的 bug
                        top: that.buttonRect.top + 104 + that.buttonRect.height,
                        left: that.buttonRect.left + that.buttonRect.width / 2
                    },
                    // #endif
                    success: (e) => {
                        console.log(e.tapIndex);
                        uni.showToast({
                            title: "点击了第" + e.tapIndex + "个选项",
                            icon: "none"
                        })
                    }
                })
            },
杜庆泉's avatar
杜庆泉 已提交
95
			
Y
yurj26 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108
            // #ifdef H5
            getNodeInfo() {
                uni.createSelectorQuery().select('.target').boundingClientRect().exec((ret) => {
                    const rect = ret[0]
                    if (rect) {
                        this.buttonRect = rect
                    }
                });
            }
            // #endif
        }
    }
</script>