RegionModal.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8. import React, { Component } from "react";
  9. import {
  10. Platform,
  11. StyleSheet,
  12. Text,
  13. View,
  14. Image,
  15. TouchableOpacity,
  16. ImageBackground,
  17. TextInput,
  18. Button,
  19. StatusBar,
  20. Modal,
  21. ScrollView,
  22. TouchableHighlight,
  23. DeviceEventEmitter,
  24. findNodeHandle,
  25. UIManager
  26. } from "react-native";
  27. import CitysData from "../../data/citys.json";
  28. type Props = {};
  29. export default class RegionModal extends Component<Props> {
  30. state = {
  31. modalVisible: false,
  32. cityjson: JSON.parse(JSON.stringify(CitysData.provinces)),
  33. provinces: CitysData.provinces[0].provinces,
  34. provinces_views: [],
  35. provinces_views_index: -1,
  36. city_data: CitysData.provinces[0].citys,
  37. city_views: [],
  38. city_views_index: -1,
  39. click_provinces_name: "",
  40. click_city_name: "",
  41. text_height: -1
  42. };
  43. render() {
  44. return (
  45. <Modal
  46. animationType="slide"
  47. transparent={true}
  48. visible={this.state.modalVisible}
  49. onRequestClose={() => {
  50. this.setState({ modalVisible: false });
  51. }}
  52. >
  53. <View
  54. style={{
  55. flex: 1,
  56. flexDirection: "column"
  57. }}
  58. >
  59. <TouchableOpacity
  60. style={{
  61. flex: 3.2,
  62. backgroundColor: "rgba(0, 0, 0, 0.5)",
  63. width: "100%"
  64. }}
  65. activeOpacity={1}
  66. onPress={() =>
  67. this.setState({
  68. modalVisible: false
  69. })
  70. }
  71. />
  72. <View
  73. style={{
  74. flex: 2,
  75. backgroundColor: "white",
  76. flexDirection: "column",
  77. justifyContent: "center",
  78. alignItems: "center",
  79. width: "100%"
  80. }}
  81. >
  82. <View
  83. style={{
  84. flex: 1,
  85. flexDirection: "row",
  86. alignItems: "center",
  87. justifyContent: "center"
  88. }}
  89. >
  90. <TouchableOpacity
  91. style={{
  92. flex: 1
  93. }}
  94. activeOpacity={1}
  95. onPress={() => this.cancel()}
  96. >
  97. <View
  98. style={{
  99. flex: 1,
  100. alignItems: "center",
  101. justifyContent: "center"
  102. }}
  103. >
  104. <Text
  105. style={{
  106. fontSize: 20,
  107. color: "rgba(59, 59, 59, 1)",
  108. textAlignVertical: "center"
  109. }}
  110. >
  111. 取消
  112. </Text>
  113. </View>
  114. </TouchableOpacity>
  115. <View style={{ flex: 3.5 }} />
  116. <TouchableOpacity
  117. style={{
  118. flex: 1
  119. }}
  120. activeOpacity={1}
  121. onPress={() => this.commit()}
  122. >
  123. <View
  124. style={{
  125. flex: 1,
  126. alignItems: "center",
  127. justifyContent: "center"
  128. }}
  129. >
  130. <Text
  131. style={{
  132. fontSize: 20,
  133. color: "rgba(59, 59, 59, 1)",
  134. textAlignVertical: "center"
  135. }}
  136. >
  137. 完成
  138. </Text>
  139. </View>
  140. </TouchableOpacity>
  141. </View>
  142. <View
  143. style={{
  144. flex: 0.05,
  145. width: "90%",
  146. backgroundColor: "rgba(246, 247, 248, 1)"
  147. // backgroundColor: "red"
  148. }}
  149. />
  150. <View
  151. style={{
  152. flex: 5,
  153. flexDirection: "row"
  154. }}
  155. >
  156. <View
  157. style={{
  158. flex: 1
  159. }}
  160. >
  161. <ScrollView
  162. style={{
  163. flex: 1
  164. }}
  165. ref={view => (this.provinces_scroll = view)}
  166. onLayout={() => this.provinces_onlayout()}
  167. showsVerticalScrollIndicator={false}
  168. >
  169. {this.scroll_item()}
  170. </ScrollView>
  171. </View>
  172. <View
  173. style={{
  174. flex: 1
  175. }}
  176. >
  177. <ScrollView
  178. ref={view => (this.city_scroll = view)}
  179. onLayout={() => this.city_onlayout()}
  180. style={{
  181. flex: 1
  182. }}
  183. showsVerticalScrollIndicator={false}
  184. >
  185. {this.scroll_city_item(this.state.city_data)}
  186. </ScrollView>
  187. </View>
  188. </View>
  189. </View>
  190. </View>
  191. </Modal>
  192. );
  193. }
  194. provinces_onlayout() {
  195. if (this.state.provinces_views_index != -1) {
  196. this.provinces_scroll.scrollTo({
  197. x: 0,
  198. y: (this.state.text_height + 20) * this.state.provinces_views_index,
  199. duration: 500
  200. });
  201. }
  202. }
  203. city_onlayout() {
  204. if (this.state.city_views_index != -1) {
  205. this.city_scroll.scrollTo({
  206. x: 0,
  207. y: (this.state.text_height + 20) * this.state.city_views_index,
  208. duration: 500
  209. });
  210. }
  211. }
  212. commit() {
  213. if (
  214. this.state.provinces_views_index == -1 ||
  215. this.state.city_views_index == -1
  216. ) {
  217. alert("请选择完整地区");
  218. } else {
  219. this.props.cityscommit(
  220. this.state.click_provinces_name,
  221. this.state.click_city_name
  222. );
  223. this.setModalVisible(false);
  224. }
  225. }
  226. cancel() {
  227. this.setModalVisible(false);
  228. }
  229. setModalVisible(visible) {
  230. this.setState({
  231. modalVisible: visible
  232. });
  233. }
  234. scroll_item() {
  235. // console.log(this.state.cityjson);
  236. for (var i = 0; i < this.state.cityjson.length; i++) {
  237. let index = i;
  238. let textstyle = null;
  239. if (this.state.provinces_views_index == i) {
  240. textstyle = styles.item_text_click;
  241. } else {
  242. textstyle = styles.item_text;
  243. }
  244. this.state.provinces_views[i] = (
  245. <Text
  246. style={textstyle}
  247. key={i}
  248. onPress={() => this.get_city(index)}
  249. onLayout={event => this.onLayout(event)}
  250. >
  251. {CitysData.provinces[i].provinceName}
  252. </Text>
  253. );
  254. }
  255. return this.state.provinces_views;
  256. }
  257. onLayout = event => {
  258. if (this.state.text_height == -1) {
  259. this.setState({
  260. text_height: event.nativeEvent.layout.height
  261. });
  262. } else {
  263. }
  264. };
  265. get_city(index) {
  266. console.log(index);
  267. this.setState({
  268. city_data: CitysData.provinces[index].citys,
  269. text_color: "blue",
  270. provinces_views_index: index,
  271. city_views_index: -1,
  272. click_provinces_name: CitysData.provinces[index].provinceName,
  273. click_city_name: ""
  274. });
  275. this.city_scroll.scrollTo({
  276. x: 0,
  277. y: 0,
  278. duration: 100
  279. });
  280. // this.forceUpdate();
  281. }
  282. scroll_city_item(citys) {
  283. this.state.city_views = [];
  284. for (var i = 0; i < citys.length; i++) {
  285. let index = i;
  286. if (this.state.city_views_index == i) {
  287. textstyle = styles.item_text_click;
  288. } else {
  289. textstyle = styles.item_text;
  290. }
  291. this.state.city_views[i] = (
  292. <Text style={textstyle} key={i} onPress={() => this.click_citys(index)}>
  293. {citys[i].citysName}
  294. </Text>
  295. );
  296. }
  297. return this.state.city_views;
  298. }
  299. click_citys(index) {
  300. this.setState({
  301. city_views_index: index,
  302. click_city_name: this.state.city_data[index].citysName
  303. });
  304. }
  305. }
  306. const styles = StyleSheet.create({
  307. item_text: {
  308. color: "rgba(77, 77, 77, 1)",
  309. fontSize: 20,
  310. justifyContent: "center",
  311. alignItems: "center",
  312. marginTop: 20,
  313. width: "100%",
  314. textAlignVertical: "center",
  315. textAlign: "center"
  316. },
  317. item_text_click: {
  318. color: "rgba(59, 149, 243, 1)",
  319. fontSize: 20,
  320. justifyContent: "center",
  321. alignItems: "center",
  322. marginTop: 20,
  323. width: "100%",
  324. textAlignVertical: "center",
  325. textAlign: "center"
  326. }
  327. });