index.vue 5.72 KB
<template>
  <van-action-sheet get-container="body"
                    :close-on-click-action="false"
                    :close-on-click-overlay="false"
                    :closeable="false"
                    :title="title"
                    :value="visible"
                    @closed="handleClosed"
                    @input="updateVisible">
    <div class="region-sheet">
      <div class="region-sheet__search">
        <van-search v-model.trim="keyword"
                    background="transparent"
                    shape="round"
                    :placeholder="searchPlaceholder"
                    :show-action="false" />
      </div>

      <div class="region-sheet__list">
        <div v-for="item in filteredOptions"
             :key="item.value"
             class="region-sheet__item"
             :class="{ active: item.value === tempValue }"
             @click="handleSelect(item)">
          <span class="region-sheet__label">{{ item.label }}</span>
          <van-icon v-if="item.value === tempValue"
                    color="#2c66f7"
                    name="success" />
        </div>

        <div v-if="!filteredOptions.length" class="region-sheet__empty">
          暂无地区数据
        </div>
      </div>

      <div class="region-sheet__footer">
        <van-button class="region-sheet__button"
                    block
                    plain
                    round
                    @click="handleCancel">
          取消
        </van-button>
        <van-button class="region-sheet__button"
                    type="info"
                    block
                    round
                    @click="handleConfirm">
          确定
        </van-button>
      </div>
    </div>
  </van-action-sheet>
</template>

<script>
import { ActionSheet } from "vant";

export default {
	name: "RegionPicker",
	components: {
		[ActionSheet.name]: ActionSheet,
	},
	props: {
		title: {
			type: String,
			default: "门店区域",
		},
		value: {
			type: [String, Number],
			default: "",
		},
		visible: {
			type: Boolean,
			default: false,
		},
		options: {
			type: Array,
			default() {
				return [];
			},
		},
		searchPlaceholder: {
			type: String,
			default: "搜索地区",
		},
		labelKey: {
			type: String,
			default: "label",
		},
		valueKey: {
			type: String,
			default: "value",
		},
	},
	data() {
		return {
			keyword: "",
			tempValue: this.value,
			innerOptions: [],
			hasLoaded: false,
		};
	},
	computed: {
		sourceOptions() {
			return this.options.length ? this.options : this.innerOptions;
		},
		normalizedOptions() {
			return (this.sourceOptions || []).map((item) => {
				const label = item?.[this.labelKey];
				const value = item?.[this.valueKey];
				return {
					...item,
					label,
					value,
				};
			}).filter(item => item.label !== undefined && item.value !== undefined);
		},
		filteredOptions() {
			const keyword = this.keyword.trim().toLowerCase();
			if (!keyword) {
				return this.normalizedOptions;
			}
			return this.normalizedOptions.filter((item) => {
				return String(item.label).toLowerCase().includes(keyword)
					|| String(item.value).toLowerCase().includes(keyword);
			});
		},
	},
	watch: {
		value: {
			immediate: true,
			handler(val) {
				this.tempValue = val;
			},
		},
		visible: {
			immediate: true,
			handler(val) {
				console.log(val);

				if (val) {
					this.initPanelState();
				}
			},
		},
	},
	methods: {
		async initPanelState() {
			this.keyword = "";
			this.tempValue = this.value;
			await this.ensureRegionOptions();
		},
		async ensureRegionOptions() {
			if (this.options.length || this.hasLoaded) {
				return;
			}
			const result = await Promise.resolve(this.loadRegionOptions());
			this.innerOptions = Array.isArray(result) ? result : [];
			this.hasLoaded = true;
		},
		loadRegionOptions() {
			// 地区数据获取留空,后续可在这里接接口或自行改成外部传参。
			return [];
		},
		findOptionByValue(value) {
			return this.normalizedOptions.find(item => item.value === value) || null;
		},
		handleSelect(item) {
			const isSameRegion = this.tempValue === item.value;
			this.tempValue = isSameRegion ? "" : item.value;
			this.$emit("select", isSameRegion ? null : item);
		},
		handleCancel() {
			this.keyword = "";
			this.tempValue = this.value;
			this.$emit("update:visible", false);
			this.$emit("cancel");
		},
		handleConfirm() {
			const selectedOption = this.findOptionByValue(this.tempValue);
			this.$emit("input", this.tempValue);
			this.$emit("change", selectedOption);
			this.$emit("confirm", selectedOption);
			this.keyword = "";
			this.$emit("update:visible", false);
		},
		handleClosed() {
			this.$emit("closed");
		},
		updateVisible(val) {
			if (!val) {
				this.keyword = "";
				this.tempValue = this.value;
			}
			this.$emit("update:visible", val);
		},
	},
};
</script>

<style lang="scss" scoped>
::v-deep .van-action-sheet {
	&__header {
		font-weight: 500;
		font-size: 16px;
		line-height: 20px;
		text-align: center;
		color: #000000e5;
		padding: 16px;
	}
}
.region-sheet {
	display: flex;
	flex-direction: column;
	height: 70vh;
	background: #ffffff;

	&__search {
		::v-deep .van-search {
			padding: 12px 16px 4px !important;
		}
	}

	&__list {
		flex: 1;
		overflow-y: auto;
		padding: 0px 16px 12px;
	}

	&__item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 14px 12px;
		font-size: 14px;
		color: #333333;
		background: #ffffff;
		border-bottom: 1px solid #f2f3f5;

		&.active {
			color: #2c66f7;
		}
	}

	&__label {
		flex: 1;
		padding-right: 12px;
		word-break: break-all;
	}

	&__empty {
		padding: 48px 0;
		text-align: center;
		font-size: 14px;
		color: #999999;
	}

	&__footer {
		display: flex;
		column-gap: 12px;
		padding: 8px 16px;
	}

	&__button {
		flex: 1;
	}
}
</style>