Blame view

uni_modules/uview-plus/components/u-action-sheet/u-action-sheet.vue 10.6 KB
a2702f6d   刘淇   巡查计划
1
2
3
4
5
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
  <template>
  	<u-popup
  	    :show="show"
  	    mode="bottom"
  	    @close="closeHandler"
  	    :safeAreaInsetBottom="safeAreaInsetBottom"
  	    :round="round"
  	>
  		<view class="u-action-sheet">
  			<!-- 顶部标题区域 -->
  			<view
  			    class="u-action-sheet__header"
  			    v-if="title"
  			>
  				<text class="u-action-sheet__header__title u-line-1">{{title}}</text>
  				<view
  				    class="u-action-sheet__header__icon-wrap"
  				    @tap.stop="cancel"
  				>
  					<up-icon
  					    name="close"
  					    size="17"
  					    color="#c8c9cc"
  					    bold
  					></up-icon>
  				</view>
  			</view>
  			<!-- 描述信息 -->
  			<text
  			    class="u-action-sheet__description"
  				:style="[{
  					marginTop: `${title && description ? 0 : '18px'}`
  				}]"
  			    v-if="description"
  			>{{description}}</text>
  			<slot>
  				<!-- 分割线 -->
  				<u-line v-if="description"></u-line>
  				<!-- 操作项列表 -->
  				<scroll-view scroll-y class="u-action-sheet__item-wrap" :style="{maxHeight: wrapMaxHeight}">
  					<view :key="index" v-for="(item, index) in actions">
  						<!-- #ifdef MP -->
  						<button
  						    class="u-reset-button"
  						    :openType="item.openType"
  						    @getuserinfo="onGetUserInfo"
  						    @contact="onContact"
  						    @getphonenumber="onGetPhoneNumber"
  						    @error="onError"
  						    @launchapp="onLaunchApp"
  						    @opensetting="onOpenSetting"
  						    :lang="lang"
  						    :session-from="sessionFrom"
  						    :send-message-title="sendMessageTitle"
  						    :send-message-path="sendMessagePath"
  						    :send-message-img="sendMessageImg"
  						    :show-message-card="showMessageCard"
  						    :app-parameter="appParameter"
  						    @tap="selectHandler(index)"
  						    :hover-class="!item.disabled && !item.loading ? 'u-action-sheet--hover' : ''"
  						>
  							<!-- #endif -->
  							<view
  							    class="u-action-sheet__item-wrap__item"
  							    @tap.stop="selectHandler(index)"
  							    :hover-class="!item.disabled && !item.loading ? 'u-action-sheet--hover' : ''"
  							    :hover-stay-time="150"
  							    :style="getItemHoverStyle(index)"
  							>
  								<template v-if="!item.loading">
  									<text
  									    class="u-action-sheet__item-wrap__item__name"
  									    :style="[itemStyle(index)]"
  									>{{ item.name }}</text>
  									<text
  									    v-if="item.subname"
  									    class="u-action-sheet__item-wrap__item__subname"
  									>{{ item.subname }}</text>
  								</template>
  								<!-- 加载状态图标 -->
  								<u-loading-icon
  								    v-else
  								    custom-class="van-action-sheet__loading"
  								    size="18"
  								    mode="circle"
  								/>
  							</view>
  							<!-- #ifdef MP -->
  						</button>
  						<!-- #endif -->
  						<!-- 选项间分割线 -->
  						<u-line v-if="index !== actions.length - 1"></u-line>
  					</view>
  				</scroll-view>
  			</slot>
  			<!-- 取消按钮前的分割区域 -->
  			<u-gap
  			    bgColor="#eaeaec"
  			    height="6"
  			    v-if="cancelText"
  			></u-gap>
  			<!-- 取消按钮 -->
  			<view class="u-action-sheet__item-wrap__item u-action-sheet__cancel"
  				hover-class="u-action-sheet--hover" @tap="cancel" v-if="cancelText">
  				<text
  				    @touchmove.stop.prevent
  				    :hover-stay-time="150"
  				    class="u-action-sheet__cancel-text"
  				>{{cancelText}}</text>
  			</view>
  		</view>
  	</u-popup>
  </template>
  
  <script>
  	import { openType } from '../../libs/mixin/openType'
  	import { buttonMixin } from '../../libs/mixin/button'
  	import { props } from './props';
  	import { mpMixin } from '../../libs/mixin/mpMixin';
  	import { mixin } from '../../libs/mixin/mixin';
  	import { addUnit } from '../../libs/function/index';
  	/**
  	 * ActionSheet 操作菜单
  	 * @description 本组件用于从底部弹出一个操作菜单,供用户选择并返回结果。本组件功能类似于uni的uni.showActionSheetAPI,配置更加灵活,所有平台都表现一致。
  	 * @tutorial https://uview-plus.jiangruyi.com/components/actionSheet.html
  	 * 
  	 * @property {Boolean}			show				操作菜单是否展示 (默认 false 
  	 * @property {String}			title				操作菜单标题
  	 * @property {String}			description			选项上方的描述信息
  	 * @property {Array<Object>}	actions				按钮的文字数组,见官方文档示例
  	 * @property {String}			cancelText			取消按钮的提示文字,不为空时显示按钮
  	 * @property {Boolean}			closeOnClickAction	点击某个菜单项时是否关闭弹窗 (默认 true 
  	 * @property {Boolean}			safeAreaInsetBottom	处理底部安全区 (默认 true 
  	 * @property {String}			openType			小程序的打开方式 (contact | launchApp | getUserInfo | openSetting getPhoneNumber error )
  	 * @property {Boolean}			closeOnClickOverlay	点击遮罩是否允许关闭  (默认 true )
  	 * @property {Number|String}	round				圆角值,默认无圆角  (默认 0 )
  	 * @property {String}			lang				指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文
  	 * @property {String}			sessionFrom			会话来源,openType="contact"时有效
  	 * @property {String}			sendMessageTitle	会话内消息卡片标题,openType="contact"时有效
  	 * @property {String}			sendMessagePath		会话内消息卡片点击跳转小程序路径,openType="contact"时有效
  	 * @property {String}			sendMessageImg		会话内消息卡片图片,openType="contact"时有效
  	 * @property {Boolean}			showMessageCard		是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息,openType="contact"时有效 (默认 false 
  	 * @property {String}			appParameter		打开 APP 时,向 APP 传递的参数,openType=launchApp 时有效
  	 * 
  	 * @event {Function} select			点击ActionSheet列表项时触发 
  	 * @event {Function} close			点击取消按钮时触发
  	 * @event {Function} getuserinfo	用户点击该按钮时,会返回获取到的用户信息,回调的 detail 数据与 wx.getUserInfo 返回的一致,openType="getUserInfo"时有效
  	 * @event {Function} contact		客服消息回调,openType="contact"时有效
  	 * @event {Function} getphonenumber	获取用户手机号回调,openType="getPhoneNumber"时有效
  	 * @event {Function} error			当使用开放能力时,发生错误的回调,openType="error"时有效
  	 * @event {Function} launchapp		打开 APP 成功的回调,openType="launchApp"时有效
  	 * @event {Function} opensetting	在打开授权设置页后回调,openType="openSetting"时有效
  	 * @example <u-action-sheet :actions="list" :title="title" :show="show"></u-action-sheet>
  	 */
  	export default {
  		name: "u-action-sheet",
  		// 一些props参数和methods方法,通过mixin混入,因为其他文件也会用到
  		mixins: [openType, buttonMixin, mixin, props],
  		data() {
  			return {
  
  			}
  		},
  		computed: {
  			// 操作项目的样式
  			itemStyle() {
  				return (index) => {
  					let style = {};
  					if (this.actions[index].color) style.color = this.actions[index].color
  					if (this.actions[index].fontSize) style.fontSize = addUnit(this.actions[index].fontSize)
  					// 选项被禁用的样式
  					if (this.actions[index].disabled) style.color = '#c0c4cc'
  					return style;
  				}
  			},
  		},
  		emits: ["close", "select", "update:show"],
  		methods: {
  			// 关闭操作菜单事件处理
  			closeHandler() {
  				// 允许点击遮罩关闭时,才发出close事件
  				if(this.closeOnClickOverlay) {
  					this.$emit('update:show', false)
  					this.$emit('close')
  				}
  			},
  			// 点击取消按钮
  			cancel() {
  				this.$emit('update:show', false)
  				this.$emit('close')
  			},
  			// 选择操作项处理
  			selectHandler(index) {
  				const item = this.actions[index]
  				if (item && !item.disabled && !item.loading) {
  					this.$emit('select', item)
  					if (this.closeOnClickAction) {
  						this.$emit('update:show', false)
  						this.$emit('close')
  					}
  				}
  			},
  			// 动态处理Hover时候第一个item的圆角
  			getItemHoverStyle(index) {
  				if (index === 0 && this.round && !this.title && !this.description) {
  					return {
  						borderTopLeftRadius: `${this.round}px`,
  						borderTopRightRadius: `${this.round}px`,
  					}
  				}
  				return {}
  			},
  		}
  	}
  </script>
  
  <style lang="scss" scoped>
  	$u-action-sheet-reset-button-width:100% !default;
  	$u-action-sheet-title-font-size: 16px !default;
  	$u-action-sheet-title-padding: 12px 30px !default;
  	$u-action-sheet-title-color: $u-main-color !default;
  	$u-action-sheet-header-icon-wrap-right:15px !default;
  	$u-action-sheet-header-icon-wrap-top:15px !default;
  	$u-action-sheet-description-font-size:13px !default;
  	$u-action-sheet-description-color:14px !default;
  	$u-action-sheet-description-margin: 18px 15px !default;
  	$u-action-sheet-item-wrap-item-padding:17px !default;
  	$u-action-sheet-item-wrap-name-font-size:16px !default;
  	$u-action-sheet-item-wrap-subname-font-size:13px !default;
  	$u-action-sheet-item-wrap-subname-color: #c0c4cc !default;
  	$u-action-sheet-item-wrap-subname-margin-top:10px !default;
  	$u-action-sheet-cancel-text-font-size:16px !default;
  	$u-action-sheet-cancel-text-color:$u-content-color !default;
  	$u-action-sheet-cancel-text-font-size:15px !default;
  	$u-action-sheet-cancel-text-hover-background-color:rgb(242, 243, 245) !default;
  
  	.u-reset-button {
  		width: $u-action-sheet-reset-button-width;
  	}
  
  	.u-action-sheet {
  		text-align: center;
  		&__header {
  			position: relative;
  			padding: $u-action-sheet-title-padding;
  			&__title {
  				font-size: $u-action-sheet-title-font-size;
  				color: $u-action-sheet-title-color;
  				font-weight: bold;
  				text-align: center;
  			}
  
  			&__icon-wrap {
  				position: absolute;
  				right: $u-action-sheet-header-icon-wrap-right;
  				top: $u-action-sheet-header-icon-wrap-top;
  			}
  		}
  
  		&__description {
  			font-size: $u-action-sheet-description-font-size;
  			color: $u-tips-color;
  			margin: $u-action-sheet-description-margin;
  			text-align: center;
  		}
  
  		&__item-wrap {
  
  			&__item {
  				padding: $u-action-sheet-item-wrap-item-padding;
  				@include flex;
  				align-items: center;
  				justify-content: center;
  				flex-direction: column;
  
  				&__name {
  					font-size: $u-action-sheet-item-wrap-name-font-size;
  					color: $u-main-color;
  					text-align: center;
  				}
  
  				&__subname {
  					font-size: $u-action-sheet-item-wrap-subname-font-size;
  					color: $u-action-sheet-item-wrap-subname-color;
  					margin-top: $u-action-sheet-item-wrap-subname-margin-top;
  					text-align: center;
  				}
  			}
  		}
  
  		&__cancel-text {
  			font-size: $u-action-sheet-cancel-text-font-size;
  			color: $u-action-sheet-cancel-text-color;
  			text-align: center;
  			// padding: $u-action-sheet-cancel-text-font-size;
  		}
  
  		&--hover {
  			background-color: $u-action-sheet-cancel-text-hover-background-color;
  		}
  	}
  </style>