-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
174 lines (163 loc) · 6.6 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import Taro, { Component } from '@tarojs/taro'
import { View, Text, Picker } from '@tarojs/components'
import './index.css'
import region from './region'
export default class TaroRegionPicker extends Component {
state = {
region: '请选择省市区',
// H5、微信小程序、百度小程序、字节跳动小程序
range: [],
value: [0, 0, 0],
// 支付宝小程序
list: []
}
componentWillMount() {
// 省市区选择器初始化
// H5、微信小程序、百度小程序、字节跳动小程序
let range = this.state.range;
let temp = [];
for (let i = 0; i < region.length; i++) {
temp.push(region[i].name);
}
range.push(temp);
temp = [];
for (let i = 0; i < region[0].city.length; i++) {
temp.push(region[0].city[i].name);
}
range.push(temp);
temp = [];
for (let i = 0; i < region[0].city[0].districtAndCounty.length; i++) {
temp.push(region[0].city[0].districtAndCounty[i]);
}
range.push(temp);
this.setState({
range: range
})
// 支付宝小程序
let list = this.state.list;
for (let i = 0; i < region.length; i++) {
let proviceTemp = Object.create(null);
proviceTemp.name = region[i].name;
proviceTemp.subList = [];
for (let j = 0; j < region[i].city.length; j++) {
let cityTemp = Object.create(null);
cityTemp.name = region[i].city[j].name;
cityTemp.subList = [];
for (let k = 0; k < region[i].city[j].districtAndCounty.length; k++) {
let districtAndCountyTemp = Object.create(null);
districtAndCountyTemp.name = region[i].city[j].districtAndCounty[k];
cityTemp.subList.push(districtAndCountyTemp);
}
proviceTemp.subList.push(cityTemp);
}
list.push(proviceTemp);
}
this.setState({
list: list
})
}
// H5、微信小程序、百度小程序、字节跳动小程序
onChange = (e) => {
let regionTemp = this.state.region;
let rangeTemp = this.state.range;
let valueTemp = this.state.value;
valueTemp = e.detail.value;
regionTemp = rangeTemp[0][valueTemp[0]] + ' - ' + rangeTemp[1][valueTemp[1]] + ' - ' + rangeTemp[2][valueTemp[2]];
this.setState({
region: regionTemp,
range: rangeTemp,
value: valueTemp
}, () => {this.props.onGetRegion(this.state.region)})
}
onColumnChange = (e) => {
let rangeTemp = this.state.range;
let valueTemp = this.state.value;
let column = e.detail.column;
let row = e.detail.value;
valueTemp[column] = row;
switch (column) {
case 0:
let cityTemp = [];
let districtAndCountyTemp = [];
for (let i = 0; i < region[row].city.length; i++) {
cityTemp.push(region[row].city[i].name);
}
for (let i = 0; i < region[row].city[0].districtAndCounty.length; i++) {
districtAndCountyTemp.push(region[row].city[0].districtAndCounty[i]);
}
valueTemp[1] = 0;
valueTemp[2] = 0;
rangeTemp[1] = cityTemp;
rangeTemp[2] = districtAndCountyTemp;
break;
case 1:
let districtAndCountyTemp2 = [];
for (let i = 0; i < region[valueTemp[0]].city[row].districtAndCounty.length; i++) {
districtAndCountyTemp2.push(region[valueTemp[0]].city[row].districtAndCounty[i]);
}
valueTemp[2] = 0;
rangeTemp[2] = districtAndCountyTemp2;
break;
case 2:
break;
}
this.setState({
range: rangeTemp,
value: valueTemp
})
}
// 支付宝小程序
onClick = () => {
let temp = this.state.region;
my.multiLevelSelect({
list: this.state.list,
success: (result) => {
if (result.success) {
temp = result.result[0].name + ' - ' + result.result[1].name + ' - ' + result.result[2].name;
this.setState({
region: temp
}, () => {this.props.onGetRegion(this.state.region)})
}
}
})
}
render () {
return (
<View>
{
// 支付宝不支持多列选择器,借助支付宝小程序API:my.multiLevelSelect实现省市区选择器
process.env.TARO_ENV === 'alipay'
?
<View className={this.state.region == '请选择省市区'
? 'taro-region-picker taro-region-picker-gray'
: 'taro-region-picker taro-region-picker-black'}
onClick={this.state.onClick}
>
<View>
<Text>{this.state.region}</Text>
</View>
</View>
:
// 使用多列选择器实现省市区选择器,支持H5、微信小程序、百度小程序、字节跳动小程序
// PS:微信小程序、百度小程序、字节跳动小程序支持设置Picker的属性mode='region'实现省市区选择器,但本组件均采用多列选择器方式实现
<View className={this.state.region == '请选择省市区'
? 'taro-region-picker taro-region-picker-gray'
: 'taro-region-picker taro-region-picker-black'}
>
<Picker
mode='multiSelector'
onChange={this.onChange}
onColumnChange={this.onColumnChange}
range={this.state.range}
value={this.state.value}
>
<View>
<Text>{this.state.region}</Text>
</View>
</Picker>
</View>
}
</View>
)
}
}