index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <template>
  2. <div class="region-cascader">
  3. <div class="region-selector">
  4. <!-- 左侧:省份列表 -->
  5. <div class="region-list province-list">
  6. <div class="list-header">省份</div>
  7. <div class="list-body">
  8. <div
  9. v-for="province in provinces"
  10. :key="province.value"
  11. class="region-item"
  12. :class="{ 'is-active': isProvinceSelected(province.value), 'is-checked': isProvinceChecked(province.value) }"
  13. @click="handleProvinceClick(province)"
  14. >
  15. <el-checkbox
  16. v-if="multiple"
  17. :model-value="isProvinceChecked(province.value)"
  18. @click.stop
  19. @change="handleProvinceCheck(province, $event)"
  20. >
  21. {{ province.label }}
  22. </el-checkbox>
  23. <span v-else>{{ province.label }}</span>
  24. <el-icon v-if="province.children && province.children.length > 0" class="arrow-icon">
  25. <ArrowRight />
  26. </el-icon>
  27. </div>
  28. </div>
  29. </div>
  30. <!-- 中间:城市列表 -->
  31. <div class="region-list city-list">
  32. <div class="list-header">城市</div>
  33. <div class="list-body">
  34. <div
  35. v-for="city in currentCities"
  36. :key="city.value"
  37. class="region-item"
  38. :class="{ 'is-active': isCitySelected(city.value), 'is-checked': isCityChecked(city.value) }"
  39. @click="handleCityClick(city)"
  40. >
  41. <el-checkbox
  42. v-if="multiple"
  43. :model-value="isCityChecked(city.value)"
  44. @click.stop
  45. @change="handleCityCheck(city, $event)"
  46. >
  47. {{ city.label }}
  48. </el-checkbox>
  49. <span v-else>{{ city.label }}</span>
  50. <el-icon v-if="city.children && city.children.length > 0" class="arrow-icon">
  51. <ArrowRight />
  52. </el-icon>
  53. </div>
  54. <div v-if="currentCities.length === 0" class="empty-text">
  55. 请先选择省份
  56. </div>
  57. </div>
  58. </div>
  59. <!-- 右侧:区县列表 -->
  60. <div class="region-list district-list">
  61. <div class="list-header">区县</div>
  62. <div class="list-body">
  63. <div
  64. v-for="district in currentDistricts"
  65. :key="district.value"
  66. class="region-item"
  67. :class="{ 'is-checked': isDistrictChecked(district.value) }"
  68. @click="handleDistrictClick(district)"
  69. >
  70. <el-checkbox
  71. v-if="multiple"
  72. :model-value="isDistrictChecked(district.value)"
  73. @change="handleDistrictCheck(district, $event)"
  74. >
  75. {{ district.label }}
  76. </el-checkbox>
  77. <span v-else>{{ district.label }}</span>
  78. </div>
  79. <div v-if="currentDistricts.length === 0" class="empty-text">
  80. 请先选择城市
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </div>
  86. </template>
  87. <script setup lang="ts">
  88. import { ref, computed, watch } from 'vue';
  89. import { regionData } from 'element-china-area-data';
  90. import { ArrowRight } from '@element-plus/icons-vue';
  91. interface RegionData {
  92. label: string;
  93. value: string;
  94. children?: RegionData[];
  95. }
  96. interface Props {
  97. modelValue?: string[]; // 选中的区域代码数组
  98. multiple?: boolean; // 是否多选模式,默认 true
  99. }
  100. interface Emits {
  101. (e: 'update:modelValue', value: string[]): void;
  102. }
  103. const props = withDefaults(defineProps<Props>(), {
  104. modelValue: () => [],
  105. multiple: true
  106. });
  107. const emit = defineEmits<Emits>();
  108. // 所有省份
  109. const provinces = ref<RegionData[]>(regionData as RegionData[]);
  110. // 当前选中的省份
  111. const currentProvince = ref<RegionData | null>(null);
  112. // 当前选中的城市
  113. const currentCity = ref<RegionData | null>(null);
  114. // 当前省份下的城市列表
  115. const currentCities = computed(() => {
  116. return currentProvince.value?.children || [];
  117. });
  118. // 当前城市下的区县列表
  119. const currentDistricts = computed(() => {
  120. return currentCity.value?.children || [];
  121. });
  122. // 选中的区域代码
  123. const selectedRegions = ref<string[]>(props.modelValue || []);
  124. // 监听 props 变化
  125. watch(() => props.modelValue, (newVal) => {
  126. selectedRegions.value = newVal || [];
  127. }, { immediate: true });
  128. /** 判断省份是否被选中(显示箭头高亮) */
  129. const isProvinceSelected = (provinceCode: string) => {
  130. return currentProvince.value?.value === provinceCode;
  131. };
  132. /** 判断省份是否被勾选 */
  133. const isProvinceChecked = (provinceCode: string) => {
  134. const province = provinces.value.find(p => p.value === provinceCode);
  135. if (!province || !province.children) return false;
  136. // 递归检查该省份下所有区县是否都被选中
  137. const allDistrictCodes: string[] = [];
  138. province.children.forEach(city => {
  139. if (city.children) {
  140. city.children.forEach(district => {
  141. allDistrictCodes.push(district.value);
  142. });
  143. }
  144. });
  145. return allDistrictCodes.length > 0 && allDistrictCodes.every(code => selectedRegions.value.includes(code));
  146. };
  147. /** 判断城市是否被选中(显示箭头高亮) */
  148. const isCitySelected = (cityCode: string) => {
  149. return currentCity.value?.value === cityCode;
  150. };
  151. /** 判断城市是否被勾选 */
  152. const isCityChecked = (cityCode: string) => {
  153. const city = currentCities.value.find(c => c.value === cityCode);
  154. if (!city || !city.children) return false;
  155. // 检查该城市下所有区县是否都被选中
  156. return city.children.every(district => selectedRegions.value.includes(district.value));
  157. };
  158. /** 判断区县是否被勾选 */
  159. const isDistrictChecked = (districtCode: string) => {
  160. return selectedRegions.value.includes(districtCode);
  161. };
  162. /** 点击省份 */
  163. const handleProvinceClick = (province: RegionData) => {
  164. currentProvince.value = province;
  165. currentCity.value = null; // 重置城市选择
  166. };
  167. /** 点击城市 */
  168. const handleCityClick = (city: RegionData) => {
  169. currentCity.value = city;
  170. // 单选模式下不做选中操作
  171. if (!props.multiple) {
  172. // 单选模式下只展开城市,不自动选择
  173. }
  174. };
  175. /** 点击区县(单选模式) */
  176. const handleDistrictClick = (district: RegionData) => {
  177. if (!props.multiple) {
  178. // 单选模式:清空其他选中,只选中当前区县
  179. selectedRegions.value = [district.value];
  180. emit('update:modelValue', selectedRegions.value);
  181. }
  182. };
  183. /** 勾选/取消省份 */
  184. const handleProvinceCheck = (province: RegionData, checked: boolean | string | number) => {
  185. const isChecked = !!checked;
  186. if (!province.children) return;
  187. // 获取该省份下所有区县代码
  188. const districtCodes: string[] = [];
  189. province.children.forEach(city => {
  190. if (city.children) {
  191. city.children.forEach(district => {
  192. districtCodes.push(district.value);
  193. });
  194. }
  195. });
  196. if (isChecked) {
  197. // 添加该省份下所有区县
  198. const newRegions = [...selectedRegions.value];
  199. districtCodes.forEach(code => {
  200. if (!newRegions.includes(code)) {
  201. newRegions.push(code);
  202. }
  203. });
  204. selectedRegions.value = newRegions;
  205. } else {
  206. // 移除该省份下所有区县
  207. selectedRegions.value = selectedRegions.value.filter(code => !districtCodes.includes(code));
  208. }
  209. emit('update:modelValue', selectedRegions.value);
  210. };
  211. /** 勾选/取消城市 */
  212. const handleCityCheck = (city: RegionData, checked: boolean | string | number) => {
  213. const isChecked = !!checked;
  214. if (!city.children) return;
  215. const districtCodes = city.children.map(district => district.value);
  216. if (isChecked) {
  217. // 添加该城市下所有区县
  218. const newRegions = [...selectedRegions.value];
  219. districtCodes.forEach(code => {
  220. if (!newRegions.includes(code)) {
  221. newRegions.push(code);
  222. }
  223. });
  224. selectedRegions.value = newRegions;
  225. } else {
  226. // 移除该城市下所有区县
  227. selectedRegions.value = selectedRegions.value.filter(code => !districtCodes.includes(code));
  228. }
  229. emit('update:modelValue', selectedRegions.value);
  230. };
  231. /** 勾选/取消区县 */
  232. const handleDistrictCheck = (district: RegionData, checked: boolean | string | number) => {
  233. const isChecked = !!checked;
  234. if (isChecked) {
  235. selectedRegions.value.push(district.value);
  236. } else {
  237. selectedRegions.value = selectedRegions.value.filter(code => code !== district.value);
  238. }
  239. emit('update:modelValue', selectedRegions.value);
  240. };
  241. /** 获取选中的区域名称列表 */
  242. const getSelectedRegionNames = () => {
  243. const names: string[] = [];
  244. provinces.value.forEach(province => {
  245. if (province.children) {
  246. province.children.forEach(city => {
  247. if (city.children) {
  248. city.children.forEach(district => {
  249. if (selectedRegions.value.includes(district.value)) {
  250. names.push(`${province.label}-${city.label}-${district.label}`);
  251. }
  252. });
  253. }
  254. });
  255. }
  256. });
  257. return names;
  258. };
  259. // 暴露方法给父组件
  260. defineExpose({
  261. getSelectedRegionNames
  262. });
  263. </script>
  264. <style scoped>
  265. .region-cascader {
  266. width: 100%;
  267. }
  268. .region-selector {
  269. display: flex;
  270. border: 1px solid #dcdfe6;
  271. border-radius: 4px;
  272. height: 400px;
  273. overflow: hidden;
  274. }
  275. .region-list {
  276. flex: 1;
  277. display: flex;
  278. flex-direction: column;
  279. border-right: 1px solid #dcdfe6;
  280. min-width: 200px;
  281. }
  282. .region-list:last-child {
  283. border-right: none;
  284. }
  285. .list-header {
  286. padding: 12px 16px;
  287. background: #f5f7fa;
  288. border-bottom: 1px solid #dcdfe6;
  289. font-weight: 500;
  290. font-size: 14px;
  291. color: #303133;
  292. }
  293. .list-body {
  294. flex: 1;
  295. overflow-y: auto;
  296. padding: 8px 0;
  297. }
  298. .region-item {
  299. padding: 8px 16px;
  300. cursor: pointer;
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-between;
  304. transition: background-color 0.2s;
  305. }
  306. .region-item:hover {
  307. background: #f5f7fa;
  308. }
  309. .region-item.is-active {
  310. background: #ecf5ff;
  311. color: #409eff;
  312. }
  313. .region-item.is-checked {
  314. font-weight: 500;
  315. }
  316. .arrow-icon {
  317. color: #909399;
  318. font-size: 12px;
  319. margin-left: 8px;
  320. }
  321. .region-item.is-active .arrow-icon {
  322. color: #409eff;
  323. }
  324. .empty-text {
  325. padding: 20px;
  326. text-align: center;
  327. color: #909399;
  328. font-size: 14px;
  329. }
  330. /* 复选框样式调整 */
  331. .region-item :deep(.el-checkbox) {
  332. width: 100%;
  333. }
  334. .region-item :deep(.el-checkbox__label) {
  335. flex: 1;
  336. padding-left: 8px;
  337. }
  338. /* 滚动条样式 */
  339. .list-body::-webkit-scrollbar {
  340. width: 6px;
  341. }
  342. .list-body::-webkit-scrollbar-thumb {
  343. background: #dcdfe6;
  344. border-radius: 3px;
  345. }
  346. .list-body::-webkit-scrollbar-thumb:hover {
  347. background: #c0c4cc;
  348. }
  349. .list-body::-webkit-scrollbar-track {
  350. background: transparent;
  351. }
  352. </style>