Commit b634ee9c by Jaxon

feat: 添加 全局 区域 选择组件

1 parent 9cd16293
<template>
<van-action-sheet :close-on-click-action="false"
: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>
export default {
name: "RegionPicker",
props: {
value: {
type: [String, Number],
default: "",
},
visible: {
type: Boolean,
default: false,
},
options: {
type: Array,
default() {
return [];
},
},
searchPlaceholder: {
type: String,
default: "搜索地区",
},
labelKey: {
type: String,
default: "name",
},
valueKey: {
type: String,
default: "code",
},
},
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) {
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) {
this.tempValue = item.value;
this.$emit("select", 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>
.region-sheet {
display: flex;
flex-direction: column;
height: 70vh;
background: #ffffff;
&__search {
padding: 12px 12px 0;
}
&__list {
flex: 1;
overflow-y: auto;
padding: 8px 12px 16px;
}
&__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: 12px;
border-top: 1px solid #f2f3f5;
}
&__button {
flex: 1;
}
}
</style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!