PubPicker.vue 2.96 KB
<template>
	<view class="pubPicker">
		<template v-if="type=='select'">
			<picker @change="bindPickerChange" :value="index" :range="range" range-key="label" :disabled="readonly">
				<view class="flex flex-between">
					<view class="uni-input">{{showText}}</view>
					<uni-icons type="right" size="18" style="margin-left: 10rpx;"></uni-icons>
				</view>
			</picker>
		</template>
		<template v-if="type == 'date'">
			<picker mode="date" :value="pickerValue" :start="startDate" :end="endDate" @change="bindDateChange"
				:disabled="readonly" fields="day">
				<view class="flex flex-between">
					<view class="uni-input">{{showTextDate}}</view>
					<uni-icons type="right" size="18" style="margin-left: 10rpx;"></uni-icons>
				</view>
			</picker>
		</template>
		<template v-if="type == 'tagSelect'">
			<view class="tags">
				<view class="tag flex-center" :class="item.value == pickerValue ? 'active' :''"
					v-for="(item,index) in range" @click="selectTag(item)">{{item.label}}</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		name: "PubPicker",
		props: {
			range: {
				type: Array,
				default: () => []
			},
			value: {
				type: [String, Number],
				default: () => ''
			},
			placeholder: {
				type: String,
				default: () => '請選擇'
			},
			type: {
				type: String,
				default: () => 'select'
			},
			readonly: {
				type: Boolean,
				default: () => false
			},

		},
		computed: {
			pickerValue: {
				get() {
					return this.value;
				},
				set(newValue) {
					this.$emit('input', newValue);
					this.$emit('change', this.pickerItem);
				},
			},
			showText() {
				let findItem = this.range.find(item => item.value == this.pickerValue)
				if (findItem) {
					return findItem.label
				}
				return this.placeholder || ''
			},
			showTextDate() {
				return this.pickerValue || '請選擇日期'
			},
		},
		data() {
			return {
				index: 0,
				startDate: '',
				endDate: '',
				pickerItem: ''
			};
		},
		methods: {
			bindPickerChange(val) {

				// console.log(val.detail.value);
				this.index = val.detail.value
				this.pickerItem = this.range[this.index]
				this.pickerValue = this.range[this.index]?.value
			},
			bindDateChange(e) {
				this.pickerValue = e.detail.value
			},
			selectTag(e) {
				if (this.readonly) {
					return
				}
				this.pickerItem = e
				this.pickerValue = e.value

			},
		}
	}
</script>

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

	.pubPicker {
		display: inline-block;
	}

	.tags {
		width: 100%;
		display: flex;
		flex-wrap: wrap;

		.tag {
			width: 182rpx;
			height: 80rpx;
			border-radius: 8rpx;
			background: #FFFFFF;
			border: 2rpx solid #E5E5E5;
			margin-right: 20rpx;
			margin-bottom: 23rpx;

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

			&.active {
				border: 2rpx solid #32BAEC;
				color: #32BAEC;


			}
		}
	}
</style>