RegionModal.js 7.6 KB

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