<template>
<div
class
=
"city-picker"
>
<input type=
"text"
:readonly=
"readonly"
:disabled=
"disabled"
:placeholder=
"placeholder"
:style=
"{width: width}"
v-model=
"selectedCity"
>
<!-- 城市选择器弹出框 -->
<div
class
=
"city-picker-modal"
v-show=
"showModal"
>
<div
class
=
"city-picker-header"
>
<span>请选择城市</span>
<span
class
=
"close-icon"
@click=
"handleCloseModal"
>×</span>
</div>
<div
class
=
"city-picker-body"
>
<!-- 渲染城市选择器树形结构 -->
<div
class
=
"city-picker-tree"
>
<div
class
=
"top-tab"
>
<div
:
class
=
"{ active: (activeTab === 'province') }"
@click=
"handleToggleTab('province')"
>省份</div>
<div
:
class
=
"{ active: (activeTab === 'city') }"
@click=
"handleToggleTab('city')"
>城市</div>
<div
:
class
=
"{ active: (activeTab === 'district') }"
@click=
"handleToggleTab('district')"
>区县</div>
</div>
<div
class
=
"tab-content"
>
<template v-
if
=
"activeTab === 'province'"
>
<!-- 渲染省份数据 -->
<template v-
for
=
"province in cityData"
>
<div :key=
"province.value"
class
=
"province"
>
<div @click=
"handleShowCity(province)"
>{{ province.label }}</div>
<template v-
if
=
"province.children && province.children.length > 0"
>
<div v-show=
"province.showCity"
>
<!-- 渲染城市数据 -->
<template v-
for
=
"city in province.children"
>
<div :key=
"city.value"
class
=
"city"
>
<div @click=
"handleShowDistrict(city)"
>{{ city.label }}</div>
<template v-
if
=
"city.children && city.children.length > 0"
>
<div v-show=
"city.showDistrict"
>
<!-- 渲染区县数据 -->
<div v-
for
=
"district in city.children"
:key=
"district.value"
>{{ district.label }}</div>
</div>
</template>
</div>
</template>
</div>
</template>
</div>
</template>
</template>
<template v-
else
-
if
=
"activeTab === 'city'"
>
<!-- 渲染城市数据 -->
<template v-
for
=
"province in cityData"
>
<template v-
if
=
"province.children && province.children.length > 0"
>
<template v-
for
=
"city in province.children"
>
<div :key=
"city.value"
class
=
"city"
>{{ city.label }}</div>
</template>
</template>
</template>
</template>
<template v-
else
-
if
=
"activeTab === 'district'"
>
<!-- 渲染区县数据 -->
<template v-
for
=
"province in cityData"
>
<template v-
if
=
"province.children && province.children.length > 0"
>
<template v-
for
=
"city in province.children"
>
<template v-
if
=
"city.children && city.children.length > 0"
>
<template v-
for
=
"district in city.children"
>
<div :key=
"district.value"
>{{ district.label }}</div>
</template>
</template>
</template>
</template>
</template>
</template>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import cityData from
'./city-data.json'
;
export
default
{
props: {
modelValue: {
type: String,
default
:
''
},
placeholder: {
type: String,
default
:
'请选择城市'
},
width: {
type: String,
default
:
'200px'
},
disabled: {
type: Boolean,
default
: false
},
readonly: {
type: Boolean,
default
: false
}
},
data() {
return
{
selectedCity: this.modelValue,
cityData: [],
showModal: false,
activeTab:
'province'
}
},
methods: {
handleShowCity(province) {
province.showCity = !province.showCity;
this.activeTab = (province.showCity ?
'city'
:
'province'
);
},
handleShowDistrict(city) {
city.showDistrict = !city.showDistrict;
this.activeTab = (city.showDistrict ?
'district'
:
'city'
);
this.selectedCity = city.label;
this.
$emit
(
'onChange'
, city.label);
this.showModal = false;
},
handleToggleTab(tab) {
this.activeTab = tab;
},
handleCloseModal() {
this.showModal = false;
}
},
mounted() {
this.cityData = cityData;
this.cityData.forEach((province) => {
province.showCity = false;
if
(province.children && province.children.length > 0) {
province.children.forEach((city) => {
city.showDistrict = false;
})
}
})
}
}
</script>