uni-list-item.vue 12.0 KB
Newer Older
study夏羽's avatar
study夏羽 已提交
1 2
<template>
	<!-- #ifdef APP-NVUE -->
study夏羽's avatar
study夏羽 已提交
3
	<cell keep-scroll-position>
study夏羽's avatar
study夏羽 已提交
4
		<!-- #endif -->
study夏羽's avatar
study夏羽 已提交
5
		<view :class="{ 'uni-list-item--disabled': disabled }" :style="{'background-color':customStyle.backgroundColor}"
study夏羽's avatar
study夏羽 已提交
6
			:hover-class="(!clickable && !link) || disabled || showSwitch ? '' : 'uni-list-item--hover'"
study夏羽's avatar
study夏羽 已提交
7
			class="uni-list-item" @click="onClick">
study夏羽's avatar
study夏羽 已提交
8
			<view v-if="!isFirstChild" class="border--left" :class="{ 'uni-list--border': border }"></view>
study夏羽's avatar
study夏羽 已提交
9
			<view class="uni-list-item__container"
study夏羽's avatar
study夏羽 已提交
10 11
				:class="{ 'container--right': showArrow || link, 'flex--direction': direction === 'column'}"
				:style="{paddingTop:padding.top,paddingLeft:padding.left,paddingRight:padding.right,paddingBottom:padding.bottom}">
study夏羽's avatar
study夏羽 已提交
12 13
				<slot name="header">
					<view class="uni-list-item__header">
study夏羽's avatar
study夏羽 已提交
14 15 16 17 18 19
						<view v-if="thumb" class="uni-list-item__icon">
							<image :src="thumb" class="uni-list-item__icon-img" :class="['uni-list--' + thumbSize]" />
						</view>
						<view v-else-if="showExtraIcon" class="uni-list-item__icon">
							<uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" />
						</view>
study夏羽's avatar
study夏羽 已提交
20 21 22
					</view>
				</slot>
				<slot name="body">
study夏羽's avatar
study夏羽 已提交
23 24 25 26
					<view class="uni-list-item__content"
						:class="{ 'uni-list-item__content--center': thumb || showExtraIcon || showBadge || showSwitch }">
						<text v-if="title" class="uni-list-item__content-title"
							:class="[ellipsis !== 0 && ellipsis <= 2 ? 'uni-ellipsis-' + ellipsis : '']">{{ title }}</text>
study夏羽's avatar
study夏羽 已提交
27 28 29 30
						<text v-if="note" class="uni-list-item__content-note">{{ note }}</text>
					</view>
				</slot>
				<slot name="footer">
study夏羽's avatar
study夏羽 已提交
31 32
					<view v-if="rightText || showBadge || showSwitch" class="uni-list-item__extra"
						:class="{ 'flex--justify': direction === 'column' }">
study夏羽's avatar
study夏羽 已提交
33
						<text v-if="rightText" class="uni-list-item__extra-text">{{ rightText }}</text>
study夏羽's avatar
study夏羽 已提交
34 35 36
						<uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" :custom-style="badgeStyle" />
						<switch v-if="showSwitch" :disabled="disabled" :checked="switchChecked"
							@change="onSwitchChange" />
study夏羽's avatar
study夏羽 已提交
37 38 39 40 41 42 43 44 45 46 47
					</view>
				</slot>
			</view>
			<uni-icons v-if="showArrow || link" :size="16" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
		</view>
		<!-- #ifdef APP-NVUE -->
	</cell>
	<!-- #endif -->
</template>

<script>
study夏羽's avatar
study夏羽 已提交
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
	/**
	 * ListItem 列表子组件
	 * @description 列表子组件
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=24
	 * @property {String} 	title 							标题
	 * @property {String} 	note 							描述
	 * @property {String} 	thumb 							左侧缩略图,若thumb有值,则不会显示扩展图标
	 * @property {String}  	thumbSize = [lg|base|sm]		略缩图大小
	 * 	@value 	 lg			大图
	 * 	@value 	 base		一般
	 * 	@value 	 sm			小图
	 * @property {String} 	badgeText						数字角标内容
	 * @property {String} 	badgeType 						数字角标类型,参考[uni-icons](https://ext.dcloud.net.cn/plugin?id=21)
	 * @property {Object}   badgeStyle           数字角标样式
	 * @property {String} 	rightText 						右侧文字内容
	 * @property {Boolean} 	disabled = [true|false]			是否禁用
	 * @property {Boolean} 	clickable = [true|false] 		是否开启点击反馈
	 * @property {String} 	link = [navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈
	 *  @value 	navigateTo 	同 uni.navigateTo()
	 * 	@value redirectTo 	同 uni.redirectTo()
	 * 	@value reLaunch   	同 uni.reLaunch()
	 * 	@value switchTab  	同 uni.switchTab()
	 * @property {String | PageURIString} 	to  			跳转目标页面
	 * @property {Boolean} 	showBadge = [true|false] 		是否显示数字角标
	 * @property {Boolean} 	showSwitch = [true|false] 		是否显示Switch
	 * @property {Boolean} 	switchChecked = [true|false] 	Switch是否被选中
	 * @property {Boolean} 	showExtraIcon = [true|false] 	左侧是否显示扩展图标
	 * @property {Object} 	extraIcon 						扩展图标参数,格式为 {color: '#4cd964',size: '22',type: 'spinner'}
	 * @property {String} 	direction = [row|column]		排版方向
	 * @value row 			水平排列
	 * @value column 		垂直排列
	 * @event {Function} 	click 							点击 uniListItem 触发事件
	 * @event {Function} 	switchChange 					点击切换 Switch 时触发
	 */
	export default {
		name: 'UniListItem',
		emits: ['click', 'switchChange'],
		props: {
			direction: {
				type: String,
				default: 'row'
			},
			title: {
				type: String,
				default: ''
			},
			note: {
				type: String,
				default: ''
			},
			ellipsis: {
study夏羽's avatar
study夏羽 已提交
99
				type: [Number, String],
study夏羽's avatar
study夏羽 已提交
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
				default: 0
			},
			disabled: {
				type: [Boolean, String],
				default: false
			},
			clickable: {
				type: Boolean,
				default: false
			},
			showArrow: {
				type: [Boolean, String],
				default: false
			},
			link: {
				type: [Boolean, String],
				default: false
			},
			to: {
				type: String,
				default: ''
			},
			showBadge: {
				type: [Boolean, String],
				default: false
			},
			showSwitch: {
				type: [Boolean, String],
				default: false
			},
			switchChecked: {
				type: [Boolean, String],
				default: false
			},
			badgeText: {
				type: String,
				default: ''
			},
			badgeType: {
				type: String,
				default: 'success'
			},
study夏羽's avatar
study夏羽 已提交
142
			badgeStyle: {
study夏羽's avatar
study夏羽 已提交
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
				type: Object,
				default () {
					return {}
				}
			},
			rightText: {
				type: String,
				default: ''
			},
			thumb: {
				type: String,
				default: ''
			},
			thumbSize: {
				type: String,
				default: 'base'
			},
			showExtraIcon: {
				type: [Boolean, String],
				default: false
			},
			extraIcon: {
				type: Object,
				default () {
					return {
						type: '',
						color: '#000000',
						size: 20
					};
				}
			},
			border: {
				type: Boolean,
				default: true
study夏羽's avatar
study夏羽 已提交
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
			},
			customStyle: {
				type: Object,
				default () {
					return {
						padding: '',
						backgroundColor: '#FFFFFF'
					}
				}
			}
		},
		watch: {
			'customStyle.padding': {
				handler(padding) {
					if(typeof padding == 'number'){
						padding += ''
					}
					let paddingArr = padding.split(' ')
					if (paddingArr.length === 1) {
						this.padding = {
							"top": padding,
							"right": padding,
							"bottom": padding,
							"left": padding
						}
					} else if (paddingArr.length === 2) {
						this.padding = {
							"top": padding[0],
							"right": padding[1],
							"bottom": padding[0],
							"left": padding[1]
						}
					} else if (paddingArr.length === 4) {
						this.padding = {
							"top": padding[0],
							"right": padding[1],
							"bottom": padding[2],
							"left": padding[3]
						}
					}
				},
				immediate: true
study夏羽's avatar
study夏羽 已提交
219 220
			}
		},
study夏羽's avatar
study夏羽 已提交
221 222 223
		// inject: ['list'],
		data() {
			return {
study夏羽's avatar
study夏羽 已提交
224 225 226 227 228 229 230
				isFirstChild: false,
				padding: {
					top: "",
					right: "",
					bottom: "",
					left: ""
				}
study夏羽's avatar
study夏羽 已提交
231
			};
study夏羽's avatar
study夏羽 已提交
232
		},
study夏羽's avatar
study夏羽 已提交
233 234 235 236 237 238 239 240
		mounted() {
			this.list = this.getForm()
			// 判断是否存在 uni-list 组件
			if (this.list) {
				if (!this.list.firstChildAppend) {
					this.list.firstChildAppend = true;
					this.isFirstChild = true;
				}
study夏羽's avatar
study夏羽 已提交
241 242
			}
		},
study夏羽's avatar
study夏羽 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		methods: {
			/**
			 * 获取父元素实例
			 */
			getForm(name = 'uniList') {
				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;
			},
			onClick() {
				if (this.to !== '') {
					this.openPage();
					return;
				}
				if (this.clickable || this.link) {
study夏羽's avatar
study夏羽 已提交
263
					this.$emit('click', {
study夏羽's avatar
study夏羽 已提交
264
						data: {}
study夏羽's avatar
study夏羽 已提交
265 266
					});
				}
study夏羽's avatar
study夏羽 已提交
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 303 304 305
			},
			onSwitchChange(e) {
				this.$emit('switchChange', e.detail);
			},
			openPage() {
				if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
					this.pageApi(this.link);
				} else {
					this.pageApi('navigateTo');
				}
			},
			pageApi(api) {
				let callback = {
					url: this.to,
					success: res => {
						this.$emit('click', {
							data: res
						});
					},
					fail: err => {
						this.$emit('click', {
							data: err
						});
					}
				}
				switch (api) {
					case 'navigateTo':
						uni.navigateTo(callback)
						break
					case 'redirectTo':
						uni.redirectTo(callback)
						break
					case 'reLaunch':
						uni.reLaunch(callback)
						break
					case 'switchTab':
						uni.switchTab(callback)
						break
					default:
study夏羽's avatar
study夏羽 已提交
306
						uni.navigateTo(callback)
study夏羽's avatar
study夏羽 已提交
307 308
				}
			}
study夏羽's avatar
study夏羽 已提交
309
		}
study夏羽's avatar
study夏羽 已提交
310
	};
study夏羽's avatar
study夏羽 已提交
311 312 313
</script>

<style lang="scss">
study夏羽's avatar
study夏羽 已提交
314 315 316 317 318 319 320 321 322 323 324 325
	$uni-font-size-sm:12px;
	$uni-font-size-base:14px;
	$uni-font-size-lg:16px;
	$uni-spacing-col-lg: 12px;
	$uni-spacing-row-lg: 15px;
	$uni-img-size-sm:20px;
	$uni-img-size-base:26px;
	$uni-img-size-lg:40px;
	$uni-border-color:#e5e5e5;
	$uni-bg-color-hover:#f1f1f1;
	$uni-text-color-grey:#999;
	$list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
study夏羽's avatar
study夏羽 已提交
326

study夏羽's avatar
study夏羽 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340
	.uni-list-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		font-size: $uni-font-size-lg;
		position: relative;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		flex-direction: row;
		/* #ifdef H5 */
		cursor: pointer;
		/* #endif */
	}
study夏羽's avatar
study夏羽 已提交
341

study夏羽's avatar
study夏羽 已提交
342 343 344
	.uni-list-item--disabled {
		opacity: 0.3;
	}
study夏羽's avatar
study夏羽 已提交
345

study夏羽's avatar
study夏羽 已提交
346 347 348
	.uni-list-item--hover {
		background-color: $uni-bg-color-hover;
	}
study夏羽's avatar
study夏羽 已提交
349

study夏羽's avatar
study夏羽 已提交
350 351 352 353 354 355 356 357 358 359 360 361
	.uni-list-item__container {
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		padding: $list-item-pd;
		padding-left: $uni-spacing-row-lg;
		flex: 1;
		overflow: hidden;
		// align-items: center;
	}
study夏羽's avatar
study夏羽 已提交
362

study夏羽's avatar
study夏羽 已提交
363 364 365
	.container--right {
		padding-right: 0;
	}
study夏羽's avatar
study夏羽 已提交
366

study夏羽's avatar
study夏羽 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380
	// .border--left {
	// 	margin-left: $uni-spacing-row-lg;
	// }
	.uni-list--border {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		/* #ifdef APP-NVUE */
		border-top-color: $uni-border-color;
		border-top-style: solid;
		border-top-width: 0.5px;
		/* #endif */
	}
study夏羽's avatar
study夏羽 已提交
381

study夏羽's avatar
study夏羽 已提交
382
	/* #ifndef APP-NVUE */
study夏羽's avatar
study夏羽 已提交
383 384 385 386 387 388 389 390 391 392 393
	.uni-list--border:after {
		position: absolute;
		top: 0;
		right: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		background-color: $uni-border-color;
	}
study夏羽's avatar
study夏羽 已提交
394

study夏羽's avatar
study夏羽 已提交
395
	/* #endif */
study夏羽's avatar
study夏羽 已提交
396 397 398 399 400 401 402 403 404 405 406 407
	.uni-list-item__content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		padding-right: 8px;
		flex: 1;
		color: #3b4144;
		// overflow: hidden;
		flex-direction: column;
		justify-content: space-between;
		overflow: hidden;
	}
study夏羽's avatar
study夏羽 已提交
408

study夏羽's avatar
study夏羽 已提交
409 410 411
	.uni-list-item__content--center {
		justify-content: center;
	}
study夏羽's avatar
study夏羽 已提交
412

study夏羽's avatar
study夏羽 已提交
413 414 415 416 417
	.uni-list-item__content-title {
		font-size: $uni-font-size-base;
		color: #3b4144;
		overflow: hidden;
	}
study夏羽's avatar
study夏羽 已提交
418

study夏羽's avatar
study夏羽 已提交
419 420 421 422 423 424
	.uni-list-item__content-note {
		margin-top: 6rpx;
		color: $uni-text-color-grey;
		font-size: $uni-font-size-sm;
		overflow: hidden;
	}
study夏羽's avatar
study夏羽 已提交
425

study夏羽's avatar
study夏羽 已提交
426 427 428 429 430 431 432 433 434
	.uni-list-item__extra {
		// width: 25%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
	}
study夏羽's avatar
study夏羽 已提交
435

study夏羽's avatar
study夏羽 已提交
436 437 438 439 440 441 442
	.uni-list-item__header {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
	}
study夏羽's avatar
study夏羽 已提交
443

study夏羽's avatar
study夏羽 已提交
444 445 446 447 448 449
	.uni-list-item__icon {
		margin-right: 18rpx;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}
study夏羽's avatar
study夏羽 已提交
450

study夏羽's avatar
study夏羽 已提交
451 452 453 454 455 456 457 458
	.uni-list-item__icon-img {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		height: $uni-img-size-base;
		width: $uni-img-size-base;
		margin-right: 10px;
	}
study夏羽's avatar
study夏羽 已提交
459

study夏羽's avatar
study夏羽 已提交
460 461 462 463 464 465 466
	.uni-icon-wrapper {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		padding: 0 10px;
	}
study夏羽's avatar
study夏羽 已提交
467

study夏羽's avatar
study夏羽 已提交
468 469 470 471 472 473
	.flex--direction {
		flex-direction: column;
		/* #ifndef APP-NVUE */
		align-items: initial;
		/* #endif */
	}
study夏羽's avatar
study夏羽 已提交
474

study夏羽's avatar
study夏羽 已提交
475 476 477 478 479
	.flex--justify {
		/* #ifndef APP-NVUE */
		justify-content: initial;
		/* #endif */
	}
study夏羽's avatar
study夏羽 已提交
480

study夏羽's avatar
study夏羽 已提交
481 482 483 484
	.uni-list--lg {
		height: $uni-img-size-lg;
		width: $uni-img-size-lg;
	}
study夏羽's avatar
study夏羽 已提交
485

study夏羽's avatar
study夏羽 已提交
486 487 488 489
	.uni-list--base {
		height: $uni-img-size-base;
		width: $uni-img-size-base;
	}
study夏羽's avatar
study夏羽 已提交
490

study夏羽's avatar
study夏羽 已提交
491 492 493 494
	.uni-list--sm {
		height: $uni-img-size-sm;
		width: $uni-img-size-sm;
	}
study夏羽's avatar
study夏羽 已提交
495

study夏羽's avatar
study夏羽 已提交
496 497 498 499
	.uni-list-item__extra-text {
		color: $uni-text-color-grey;
		font-size: $uni-font-size-sm;
	}
study夏羽's avatar
study夏羽 已提交
500

study夏羽's avatar
study夏羽 已提交
501 502 503 504 505 506 507 508
	.uni-ellipsis-1 {
		/* #ifndef APP-NVUE */
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		/* #endif */
		/* #ifdef APP-NVUE */
		lines: 1;
study夏羽's avatar
study夏羽 已提交
509
		text-overflow: ellipsis;
study夏羽's avatar
study夏羽 已提交
510 511
		/* #endif */
	}
study夏羽's avatar
study夏羽 已提交
512

study夏羽's avatar
study夏羽 已提交
513 514 515 516 517 518 519 520 521 522
	.uni-ellipsis-2 {
		/* #ifndef APP-NVUE */
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		/* #endif */
		/* #ifdef APP-NVUE */
		lines: 2;
study夏羽's avatar
study夏羽 已提交
523
		text-overflow: ellipsis;
study夏羽's avatar
study夏羽 已提交
524 525
		/* #endif */
	}
study夏羽's avatar
study夏羽 已提交
526
</style>