MaskSelecter.vue 4.36 KB
<template>
	<view class="mask-selecter" v-if="show" @click="close">
		<view v-if="type == 'select'" class="select" @click.stop="clickStop">
			<view class="item flex-center" :class="item.value == defaultValue ? 'active': ''"
				v-for="(item,index) in options" :key="index" @click.stop="$emit('change',item)">
				{{item.label}}
			</view>
		</view>

		<view v-if="type == 'tag'" class="select tag-select" @click.stop="clickStop">
			<view class="tag-item flex-center" :class="item.value == defaultValue ? 'active': ''"
				v-for="(item,index) in options" :key="index" @click.stop="$emit('change',item)">
				{{item.label}}
			</view>
		</view>

		<view v-if="type == 'children'" class="select children-select" @click.stop="clickStop">
			<view class="left">
				<view class="left-item item flex-center" :class="leftItem.value == item.value ? 'active' : ''"
					v-for="(item,index) in options" @click.stop="selectLeft(item)">{{item.label}}
				</view>
			</view>
			<view class="right">
				<template v-if="rightOptions.length">
					<view class="right-item item flex-center" v-for="(item,index) in rightOptions" :key="index"
						@click.stop="$emit('change',item)">
						{{item.label}}
					</view>
				</template>
			</view>
			<!-- <view class="tag-item flex-center" v-for="(item,index) in options" :key="index"
				@click="$emit('change',item)">
				{{item.label}}
			</view> -->
		</view>
		<view v-if="type == 'dateRange'" class="select time-range" @click.stop="clickStop">
			<uni-datetime-picker v-model="datetimerange" type="daterange" rangeSeparator="至"
				@change="datetimerangeChange" :startPlaceholder="defaultValue[0] || '开始日期'"
				:endPlaceholder="defaultValue[1] || '结束日期'" />

			<view class="pub-btn time-confirm" @click.stop="$emit('change',datetimerange)">
				确认
			</view>
		</view>
	</view>
</template>

<script>
	// import dayjs from 'dayjs';
	export default {
		name: "MaskSelecter",
		props: {
			value: {
				type: Boolean,
				default: () => false
			},
			type: {
				//children 联动 ,select選擇 tag 标签選擇
				type: String,
				default: () => 'select'
			},
			options: {
				type: Array,
				default: () => []
			},
			defaultValue: {
				type: [String, Number, Array],
				default: () => ''
			},
		},
		computed: {
			show: {
				get() {
					// if (this.value == true && this.type == 'dateRange' && !this.datetimerange.length) {
					// 	this.datetimerange = [...this.defaultValue]
					// }
					return this.value
				},
				set(newVal) {
					this.$emit('input', newVal)
				}
			}
		},
		data() {
			return {
				leftItem: {},
				rightOptions: [],
				datetimerange: []
			};
		},
		methods: {
			clickStop() {},
			selectLeft(leftItem) {
				this.leftItem = leftItem
				this.rightOptions = leftItem.children || []
			},
			datetimerangeChange(timeRange) {
				// console.log(timeRange);
				this.datetimerange = [...timeRange]
				// this.$emit('change', timeRange)
			},
			close() {
				this.$emit('close')
			},
		}
	}
</script>

<style lang="less" scoped>
	@import url('@/style/index.less');

	.mask-selecter {
		height: calc(100vh - 188rpx);
		// #ifdef APP
		// height: 100vh;
		height: calc(100vh - 104rpx);
		// #endif
		position: fixed;
		bottom: 0;
		width: 100%;
		z-index: 999;
		background-color: rgba(0, 0, 0, .3);
	}

	.select {
		background-color: @uni-bg-color;

		.item {
			height: 75rpx;

			&.active {
				color: @uni-color-primary;
				border-color: @uni-color-primary;
			}
		}

	}

	.tag-select {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx 32rpx;
		box-sizing: border-box;

		.tag-item {
			width: 218rpx;
			height: 60rpx;
			border-radius: 8rpx;
			border: 2rpx solid @uni-border-color;
			margin-bottom: 30rpx;
			margin-right: 12rpx;

			overflow: hidden;

			&:nth-child(3n) {
				margin-right: 0;
			}

			&.active {
				color: @uni-color-primary;
				border-color: @uni-color-primary;
			}
		}
	}

	.children-select {
		background-color: #f5f5f5;
		display: flex;

		.left {
			flex-shrink: 0;
			width: 240rpx;

			.active {
				color: @uni-color-primary;
			}
		}

		.right {
			width: 100%;
			background-color: #fff;
		}

		.item {
			height: 84rpx;
		}
	}

	.time-range {
		padding: 30rpx;
	}

	.time-confirm {
		margin: 30rpx 0;
	}
</style>