summaryrefslogtreecommitdiffstats
path: root/usecaseui-portal/src/app/shared/components/city-select/city-select/city-select.component.ts
blob: 432d49de8bc1efde4cd4d97efb3b883a6b5f88ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******
    Input
    areaList /MUST/: Selected region data
********/
import { Component, OnInit } from "@angular/core";
import { Input } from "@angular/core";
import { ADDRESS } from "./constants";

@Component({
	selector: "app-city-select",
	templateUrl: "./city-select.component.html",
	styleUrls: ["./city-select.component.less"],
})
export class CitySelectComponent implements OnInit {
	@Input() areaList: any[];

	constructor() {}

	ngOnInit() {}
	ngOnChanges() {}
	handleChange(area: any[], areaItem: any): void {
		if (areaItem.key === "province" && areaItem.options.length <= 1) {
			areaItem.options = ADDRESS;
		} else if (areaItem.key === "city" && areaItem.options.length <= 1) {
			ADDRESS.forEach((item) => {
				if (item.name === area[0].selected) {
					areaItem.options = item.city;
				}
			});
		} else if (
			areaItem.key === "district" &&
			areaItem.options.length <= 1
		) {
			ADDRESS.forEach((item: any) => {
				item.city.forEach((city) => {
					if (city.name === area[1].selected) {
						areaItem.options = city.county;
					}
				});
			});
		}
	}

	handleChangeSelected(area: any[], areaItem: any) {
		if (areaItem.key === "province") {
			area[1].selected = "";
			area[1].options = [];
			area[2].selected = "";
			area[2].options = [];
		} else if (areaItem.key === "city") {
			area[2].selected = "";
			area[2].options = [];
		}
	}

	// prompt text for each item of area_list
	checkArea(area: any): string {
		if (
			area.every((item) => {
				return item.selected === "";
			})
		) {
			return "empty";
		}
		if (
			area.some((item) => {
				return item.selected === "";
			})
		) {
			return "incomplete";
		}
		return "";
	}

	creatAreaList(): void {
		let arr = [
			{
				key: "province",
				selected: "",
				options: [],
			},
			{
				key: "city",
				selected: "",
				options: [],
			},
			{
				key: "district",
				selected: "",
				options: [],
			},
		];
		this.areaList.push(arr);
	}

	deleteAreaList(index: number): void {
		this.areaList.splice(index, 1);
	}
}