uni-swipe-action-item.vue 10.5 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4
<template>
	<!-- 在微信小程序 app vue端 h5 使用wxs 实现-->
	<!-- #ifdef APP-VUE || MP-WEIXIN || H5 -->
	<view class="uni-swipe">
5
		<!--  #ifdef MP-WEIXIN || VUE3 -->
study夏羽's avatar
update  
study夏羽 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
		<view class="uni-swipe_box" :change:prop="wxsswipe.showWatch" :prop="is_show" :data-threshold="threshold"
			:data-disabled="disabled" @touchstart="wxsswipe.touchstart" @touchmove="wxsswipe.touchmove"
			@touchend="wxsswipe.touchend">
			<!-- #endif -->
			<!--  #ifndef MP-WEIXIN || VUE3 -->
			<view class="uni-swipe_box" :change:prop="renderswipe.showWatch" :prop="is_show" :data-threshold="threshold"
				:data-disabled="disabled+''" @touchstart="renderswipe.touchstart" @touchmove="renderswipe.touchmove"
				@touchend="renderswipe.touchend">
				<!-- #endif -->
				<!-- 在微信小程序 app vue端 h5 使用wxs 实现-->
				<view class="uni-swipe_button-group button-group--left">
					<slot name="left">
						<view v-for="(item,index) in leftOptions" :key="index" :style="{
					  backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
					}" class="uni-swipe_button button-hock" @touchstart="appTouchStart"
							@touchend="appTouchEnd($event,index,item,'left')"
							@click.stop="onClickForPC(index,item,'left')">
							<text class="uni-swipe_button-text"
								:style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
						</view>
					</slot>
				</view>
				<view class="uni-swipe_text--center">
					<slot></slot>
				</view>
				<view class="uni-swipe_button-group button-group--right">
					<slot name="right">
						<view v-for="(item,index) in rightOptions" :key="index" :style="{
					  backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
					}" class="uni-swipe_button button-hock" @touchstart="appTouchStart"
							@touchend="appTouchEnd($event,index,item,'right')"
							@click.stop="onClickForPC(index,item,'right')"><text class="uni-swipe_button-text"
								:style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
						</view>
					</slot>
				</view>
			</view>
		</view>
44
		<!-- #endif -->
study夏羽's avatar
update  
study夏羽 已提交
45 46 47 48
		<!-- app nvue端 使用 bindingx -->
		<!-- #ifdef APP-NVUE -->
		<view ref="selector-box--hock" class="uni-swipe" @horizontalpan="touchstart" @touchend="touchend">
			<view ref='selector-left-button--hock' class="uni-swipe_button-group button-group--left">
DCloud_JSON's avatar
DCloud_JSON 已提交
49
				<slot name="left">
study夏羽's avatar
update  
study夏羽 已提交
50 51 52 53 54
					<view v-for="(item,index) in leftOptions" :key="index" :style="{
				  backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
				}" class="uni-swipe_button button-hock" @click.stop="onClick(index,item,'left')"><text
							class="uni-swipe_button-text"
							:style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF', fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
55
					</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
56
				</slot>
57
			</view>
study夏羽's avatar
update  
study夏羽 已提交
58
			<view ref='selector-right-button--hock' class="uni-swipe_button-group button-group--right">
DCloud_JSON's avatar
DCloud_JSON 已提交
59
				<slot name="right">
study夏羽's avatar
update  
study夏羽 已提交
60 61 62 63 64
					<view v-for="(item,index) in rightOptions" :key="index" :style="{
				  backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD'
				}" class="uni-swipe_button button-hock" @click.stop="onClick(index,item,'right')"><text
							class="uni-swipe_button-text"
							:style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'}">{{ item.text }}</text>
65
					</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
66 67
				</slot>
			</view>
study夏羽's avatar
update  
study夏羽 已提交
68 69 70
			<view ref='selector-content--hock' class="uni-swipe_box">
				<slot></slot>
			</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
71
		</view>
study夏羽's avatar
update  
study夏羽 已提交
72 73 74 75 76 77 78 79 80
		<!-- #endif -->
		<!-- 其他平台使用 js ,长列表性能可能会有影响-->
		<!-- #ifdef MP-ALIPAY || MP-BAIDU || MP-TOUTIAO || MP-QQ -->
		<view class="uni-swipe">
			<view class="uni-swipe_box" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"
				:style="{transform:moveLeft}" :class="{ani:ani}">
				<view class="uni-swipe_button-group button-group--left" :class="[elClass]">
					<slot name="left">
						<view v-for="(item,index) in leftOptions" :key="index" :style="{
DCloud_JSON's avatar
DCloud_JSON 已提交
81 82
					  backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD',
					  fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'
83
					}" class="uni-swipe_button button-hock" @touchstart="appTouchStart"
study夏羽's avatar
update  
study夏羽 已提交
84 85 86 87 88 89 90 91 92
							@touchend="appTouchEnd($event,index,item,'left')"><text class="uni-swipe_button-text"
								:style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',}">{{ item.text }}</text>
						</view>
					</slot>
				</view>
				<slot></slot>
				<view class="uni-swipe_button-group button-group--right" :class="[elClass]">
					<slot name="right">
						<view v-for="(item,index) in rightOptions" :key="index" :style="{
DCloud_JSON's avatar
DCloud_JSON 已提交
93 94
					  backgroundColor: item.style && item.style.backgroundColor ? item.style.backgroundColor : '#C7C6CD',
					  fontSize: item.style && item.style.fontSize ? item.style.fontSize : '16px'
95
					}" @touchstart="appTouchStart" @touchend="appTouchEnd($event,index,item,'right')"
study夏羽's avatar
update  
study夏羽 已提交
96 97 98 99 100
							class="uni-swipe_button button-hock"><text class="uni-swipe_button-text"
								:style="{color: item.style && item.style.color ? item.style.color : '#FFFFFF',}">{{ item.text }}</text>
						</view>
					</slot>
				</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
101 102
			</view>
		</view>
study夏羽's avatar
update  
study夏羽 已提交
103 104
		<!-- #endif -->

DCloud_JSON's avatar
DCloud_JSON 已提交
105
</template>
106 107 108 109 110
<script src="./wx.wxs" module="wxsswipe" lang="wxs"></script>

<script module="renderswipe" lang="renderjs">
	import render from './render.js'
	export default {
study夏羽's avatar
update  
study夏羽 已提交
111
		mounted(e, ins, owner) {
112 113
			this.state = {}
		},
study夏羽's avatar
update  
study夏羽 已提交
114 115 116
		methods: {
			showWatch(newVal, oldVal, ownerInstance, instance) {
				render.showWatch(newVal, oldVal, ownerInstance, instance, this)
117
			},
study夏羽's avatar
update  
study夏羽 已提交
118 119
			touchstart(e, ownerInstance) {
				render.touchstart(e, ownerInstance, this)
120
			},
study夏羽's avatar
update  
study夏羽 已提交
121 122
			touchmove(e, ownerInstance) {
				render.touchmove(e, ownerInstance, this)
123
			},
study夏羽's avatar
update  
study夏羽 已提交
124 125
			touchend(e, ownerInstance) {
				render.touchend(e, ownerInstance, this)
126 127 128 129
			}
		}
	}
</script>
DCloud_JSON's avatar
DCloud_JSON 已提交
130 131 132
<script>
	import mpwxs from './mpwxs'
	import bindingx from './bindingx.js'
133
	import mpother from './mpother'
DCloud_JSON's avatar
DCloud_JSON 已提交
134 135 136 137 138 139 140

	/**
	 * SwipeActionItem 滑动操作子组件
	 * @description 通过滑动触发选项的容器
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=181
	 * @property {Boolean} show = [left|right|none] 	开启关闭组件,auto-close = false 时生效
	 * @property {Boolean} disabled = [true|false] 		是否禁止滑动
141
	 * @property {Boolean} autoClose = [true|false] 	滑动打开当前组件,是否关闭其他组件
DCloud_JSON's avatar
DCloud_JSON 已提交
142 143 144 145 146 147 148 149
	 * @property {Number}  threshold 					滑动缺省值
	 * @property {Array} leftOptions 					左侧选项内容及样式
	 * @property {Array} rgihtOptions 					右侧选项内容及样式
	 * @event {Function} click 							点击选项按钮时触发事件,e = {content,index} ,content(点击内容)、index(下标)
	 * @event {Function} change 						组件打开或关闭时触发,left\right\none
	 */

	export default {
study夏羽's avatar
update  
study夏羽 已提交
150 151
		mixins: [mpwxs, bindingx, mpother],
		emits: ['click', 'change'],
152 153 154 155 156 157
		props: {
			// 控制开关
			show: {
				type: String,
				default: 'none'
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
158 159 160 161 162 163 164 165 166 167 168

			// 禁用
			disabled: {
				type: Boolean,
				default: false
			},

			// 是否自动关闭
			autoClose: {
				type: Boolean,
				default: true
169 170
			},

DCloud_JSON's avatar
DCloud_JSON 已提交
171 172 173 174
			// 滑动缺省距离
			threshold: {
				type: Number,
				default: 20
175
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
			// 左侧按钮内容
			leftOptions: {
				type: Array,
				default () {
					return []
				}
			},

			// 右侧按钮内容
			rightOptions: {
				type: Array,
				default () {
					return []
				}
			}

		},
		// #ifndef VUE3
		// TODO vue2
		destroyed() {
			if (this.__isUnmounted) return
			this.uninstall()
DCloud_JSON's avatar
DCloud_JSON 已提交
199
		},
200 201 202 203 204 205 206 207
		// #endif
		// #ifdef VUE3
		// TODO vue3
		unmounted() {
			this.__isUnmounted = true
			this.uninstall()
		},
		// #endif
study夏羽's avatar
update  
study夏羽 已提交
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		methods: {
			uninstall() {
				if (this.swipeaction) {
					this.swipeaction.children.forEach((item, index) => {
						if (item === this) {
							this.swipeaction.children.splice(index, 1)
						}
					})
				}
			},
			/**
			 * 获取父元素实例
			 */
			getSwipeAction(name = 'uniSwipeAction') {
				let parent = this.$parent;
				let parentName = parent.$options.name;
				while (parentName !== name) {
					parent = parent.$parent;
					if (!parent) return false;
					parentName = parent.$options.name;
				}
				return parent;
			}
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
233 234
	}
</script>
study夏羽's avatar
update  
study夏羽 已提交
235
<style lang="scss">
DCloud_JSON's avatar
DCloud_JSON 已提交
236 237 238 239 240 241 242 243 244 245 246
	.uni-swipe {
		position: relative;
		/* #ifndef APP-NVUE */
		overflow: hidden;
		/* #endif */
	}

	.uni-swipe_box {
		/* #ifndef APP-NVUE */
		display: flex;
		flex-shrink: 0;
247
		// touch-action: none;
DCloud_JSON's avatar
DCloud_JSON 已提交
248 249 250 251 252 253 254
		/* #endif */
		position: relative;
	}

	.uni-swipe_content {
		// border: 1px red solid;
	}
255 256 257 258 259 260 261

	.uni-swipe_text--center {
		width: 100%;
		/* #ifndef APP-NVUE */
		cursor: grab;
		/* #endif */
	}
DCloud_JSON's avatar
DCloud_JSON 已提交
262 263 264 265 266 267 268 269 270

	.uni-swipe_button-group {
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		display: flex;
		/* #endif */
		flex-direction: row;
		position: absolute;
		top: 0;
271 272 273
		bottom: 0;
		/* #ifdef H5 */
		cursor: pointer;
DCloud_JSON's avatar
DCloud_JSON 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		/* #endif */
	}

	.button-group--left {
		left: 0;
		transform: translateX(-100%)
	}

	.button-group--right {
		right: 0;
		transform: translateX(100%)
	}

	.uni-swipe_button {
		/* #ifdef APP-NVUE */
		flex: 1;
		/* #endif */
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 20px;
	}

	.uni-swipe_button-text {
		/* #ifndef APP-NVUE */
		flex-shrink: 0;
		/* #endif */
		font-size: 14px;
	}

	.ani {
		transition-property: transform;
		transition-duration: 0.3s;
		transition-timing-function: cubic-bezier(0.165, 0.84, 0.44, 1);
	}

	/* #ifdef MP-ALIPAY */
	.movable-area {
		/* width: 100%; */
		height: 45px;
	}

	.movable-view {
		display: flex;
		/* justify-content: center; */
		position: relative;
		flex: 1;
		height: 45px;
		z-index: 2;
	}

	.movable-view-button {
		display: flex;
		flex-shrink: 0;
		flex-direction: row;
		height: 100%;
		background: #C0C0C0;
	}

	/* .transition {
		transition: all 0.3s;
	} */

	.movable-view-box {
		flex-shrink: 0;
		height: 100%;
		background-color: #fff;
	}

	/* #endif */
芊里 已提交
347
</style>