BirthdayModal.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 ModalView from '../utils/ModalUtil';
  28. type Props = {};
  29. export default class BirthdayModal extends Component<Props> {
  30. state = {
  31. text_height: -1,
  32. modalVisible: false,
  33. year_type: false, //true为闰年,false为平年
  34. year_array_views: [],
  35. year_array: [],
  36. year_array_views_index: -1,
  37. month_array_views: [],
  38. month_array_views_indexs: -1,
  39. month_array: [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' ],
  40. day_array: [
  41. '1',
  42. '2',
  43. '3',
  44. '4',
  45. '5',
  46. '6',
  47. '7',
  48. '8',
  49. '9',
  50. '10',
  51. '11',
  52. '12',
  53. '13',
  54. '14',
  55. '15',
  56. '16',
  57. '17',
  58. '18',
  59. '19',
  60. '20',
  61. '21',
  62. '22',
  63. '23',
  64. '24',
  65. '25',
  66. '26',
  67. '27',
  68. '28',
  69. '29',
  70. '30',
  71. '31'
  72. ],
  73. day_array_views: [],
  74. day_array_views_index: -1
  75. };
  76. render() {
  77. return (
  78. <ModalView
  79. close={() => {
  80. this.setState({ modalVisible: false });
  81. }}
  82. visible={this.state.modalVisible}
  83. customerlayout={{ flex: 1, justifyContent: 'flex-end' }}
  84. contentView={this.getView}
  85. />
  86. );
  87. }
  88. getView = () => {
  89. return (
  90. <Modal
  91. animationType="slide"
  92. transparent={true}
  93. visible={this.state.modalVisible}
  94. onRequestClose={() => {
  95. this.setState({ modalVisible: false });
  96. }}
  97. >
  98. <View
  99. style={{
  100. flex: 1,
  101. flexDirection: 'column'
  102. }}
  103. >
  104. <TouchableOpacity
  105. style={{
  106. flex: 3.2,
  107. width: '100%'
  108. }}
  109. activeOpacity={1}
  110. onPress={() =>
  111. this.setState({
  112. modalVisible: false
  113. })}
  114. />
  115. <View
  116. style={{
  117. flex: 2,
  118. backgroundColor: 'white',
  119. flexDirection: 'column',
  120. justifyContent: 'center',
  121. alignItems: 'center',
  122. width: '100%'
  123. }}
  124. >
  125. <View
  126. style={{
  127. flex: 1,
  128. flexDirection: 'row',
  129. alignItems: 'center',
  130. justifyContent: 'center'
  131. }}
  132. >
  133. <TouchableOpacity
  134. style={{
  135. flex: 1
  136. }}
  137. activeOpacity={1}
  138. onPress={() => this.cancel()}
  139. >
  140. <View
  141. style={{
  142. flex: 1,
  143. alignItems: 'center',
  144. justifyContent: 'center'
  145. }}
  146. >
  147. <Text
  148. style={{
  149. fontSize: 20,
  150. color: 'rgba(59, 59, 59, 1)',
  151. textAlignVertical: 'center'
  152. }}
  153. >
  154. 取消
  155. </Text>
  156. </View>
  157. </TouchableOpacity>
  158. <View style={{ flex: 3.5 }} />
  159. <TouchableOpacity
  160. style={{
  161. flex: 1
  162. }}
  163. activeOpacity={1}
  164. onPress={() => this.commit()}
  165. >
  166. <View
  167. style={{
  168. flex: 1,
  169. alignItems: 'center',
  170. justifyContent: 'center'
  171. }}
  172. >
  173. <Text
  174. style={{
  175. fontSize: 20,
  176. color: 'rgba(59, 59, 59, 1)',
  177. textAlignVertical: 'center'
  178. }}
  179. >
  180. 完成
  181. </Text>
  182. </View>
  183. </TouchableOpacity>
  184. </View>
  185. <View
  186. style={{
  187. flex: 0.05,
  188. width: '90%',
  189. backgroundColor: 'rgba(246, 247, 248, 1)'
  190. }}
  191. />
  192. <View
  193. style={{
  194. flex: 5,
  195. flexDirection: 'row'
  196. }}
  197. >
  198. <View
  199. style={{
  200. flex: 1
  201. }}
  202. >
  203. <ScrollView
  204. style={{
  205. flex: 1
  206. }}
  207. ref={(view) => (this.year_scroll = view)}
  208. onLayout={() => this.year_onlayout()}
  209. showsVerticalScrollIndicator={false}
  210. >
  211. {this.create_year_item()}
  212. </ScrollView>
  213. </View>
  214. <View
  215. style={{
  216. flex: 1
  217. }}
  218. >
  219. <ScrollView
  220. ref={(view) => (this.month_scroll = view)}
  221. onLayout={() => this.month_onlayout()}
  222. style={{
  223. flex: 1
  224. }}
  225. showsVerticalScrollIndicator={false}
  226. >
  227. {this.create_month_item()}
  228. </ScrollView>
  229. </View>
  230. <View
  231. style={{
  232. flex: 1
  233. }}
  234. >
  235. <ScrollView
  236. ref={(view) => (this.day_scroll = view)}
  237. onLayout={() => this.day__onlayout()}
  238. style={{
  239. flex: 1
  240. }}
  241. showsVerticalScrollIndicator={false}
  242. >
  243. {this.create_day_item()}
  244. </ScrollView>
  245. </View>
  246. </View>
  247. </View>
  248. </View>
  249. </Modal>
  250. );
  251. };
  252. componentWillMount() {
  253. var year = parseInt(this.props.year);
  254. console.log('========BirthdayModal.js---componentWillMount============================');
  255. console.log(year);
  256. if (this.state.year_array.length == 0) {
  257. let position = 0;
  258. for (let index = parseInt(year - 50); index < parseInt(year + 10); index++) {
  259. if (parseInt(year) == index) {
  260. this.setState({
  261. year_array_views_index: position
  262. });
  263. }
  264. this.state.year_array.push(index);
  265. position++;
  266. }
  267. }
  268. this.setState({
  269. month_array_views_indexs: parseInt(this.props.month - 1),
  270. day_array_views_index: parseInt(this.props.day - 1)
  271. });
  272. // var hour = date.getHours().toString();
  273. // var minute = date.getMinutes().toString();
  274. }
  275. year_onlayout() {
  276. if (this.state.year_array_views_index != -1) {
  277. this.year_scroll.scrollTo({
  278. x: 0,
  279. y: (this.state.text_height + 20) * this.state.year_array_views_index,
  280. duration: 500
  281. });
  282. }
  283. }
  284. month_onlayout() {
  285. if (this.state.month_array_views_indexs != -1) {
  286. this.month_scroll.scrollTo({
  287. x: 0,
  288. y: (this.state.text_height + 20) * this.state.month_array_views_indexs,
  289. duration: 500
  290. });
  291. }
  292. }
  293. day__onlayout() {
  294. if (this.state.day_array_views_index != -1) {
  295. this.day_scroll.scrollTo({
  296. x: 0,
  297. y: (this.state.text_height + 20) * this.state.day_array_views_index,
  298. duration: 500
  299. });
  300. }
  301. }
  302. create_year_item() {
  303. this.state.year_array_views = [];
  304. for (var i = 0; i < this.state.year_array.length; i++) {
  305. let index = i;
  306. let textstyle = null;
  307. if (this.state.year_array_views_index == i) {
  308. textstyle = styles.item_text_click;
  309. } else {
  310. textstyle = styles.item_text;
  311. }
  312. this.state.year_array_views.push(
  313. <Text
  314. style={textstyle}
  315. key={i}
  316. onPress={() => this.click_year_item(index)}
  317. onLayout={(event) => this.text_onLayout(event)}
  318. >
  319. {this.state.year_array[i]}年
  320. </Text>
  321. );
  322. }
  323. return this.state.year_array_views;
  324. }
  325. click_year_item(index) {
  326. this.setState({
  327. year_array_views_index: index,
  328. year_type: this.getRunYear(this.state.year_array[index])
  329. });
  330. }
  331. create_month_item() {
  332. this.state.month_array_views = [];
  333. for (var i = 0; i < this.state.month_array.length; i++) {
  334. let index = i;
  335. let textstyle = null;
  336. if (this.state.month_array_views_indexs == i) {
  337. textstyle = styles.item_text_click;
  338. } else {
  339. textstyle = styles.item_text;
  340. }
  341. this.state.month_array_views.push(
  342. <Text style={textstyle} key={i} onPress={() => this.click_month_item(index)}>
  343. {this.state.month_array[i]}月
  344. </Text>
  345. );
  346. }
  347. return this.state.month_array_views;
  348. }
  349. click_month_item(index) {
  350. this.setState({
  351. month_array_views_indexs: index
  352. });
  353. }
  354. create_day_item() {
  355. this.state.day_array_views = [];
  356. var forNum = 0;
  357. //获取月份
  358. var month_index = this.state.month_array_views_indexs;
  359. switch (month_index + 1) {
  360. case 1:
  361. case 3:
  362. case 5:
  363. case 7:
  364. case 8:
  365. case 10:
  366. case 12:
  367. //31天
  368. forNum = 31;
  369. break;
  370. case 4:
  371. case 6:
  372. case 9:
  373. case 11:
  374. //30天
  375. forNum = 30;
  376. break;
  377. case 2:
  378. //2月判断平年闰年
  379. if (this.getRunYear(this.state.year_array[this.state.year_array_views_index])) {
  380. //闰年2月29
  381. forNum = 29;
  382. } else {
  383. //平年2月28
  384. forNum = 28;
  385. }
  386. break;
  387. }
  388. for (var i = 0; i < forNum; i++) {
  389. //console.log(i);
  390. let index = i;
  391. let textstyle = null;
  392. if (this.state.day_array_views_index == i) {
  393. textstyle = styles.item_text_click;
  394. } else {
  395. textstyle = styles.item_text;
  396. }
  397. this.state.day_array_views.push(
  398. <Text style={textstyle} key={i} onPress={() => this.click_day_item(index)}>
  399. {this.state.day_array[i]}日
  400. </Text>
  401. );
  402. }
  403. return this.state.day_array_views;
  404. }
  405. click_day_item(index) {
  406. this.setState({
  407. day_array_views_index: index
  408. });
  409. }
  410. text_onLayout = (event) => {
  411. if (this.state.text_height == -1) {
  412. this.setState({
  413. text_height: event.nativeEvent.layout.height
  414. });
  415. } else {
  416. }
  417. };
  418. commit() {
  419. if (
  420. this.state.year_array_views_index == -1 ||
  421. this.state.month_array_views_indexs == -1 ||
  422. this.state.day_array_views_index == -1
  423. ) {
  424. alert('请选择完整日期');
  425. } else {
  426. var year = this.state.year_array[this.state.year_array_views_index];
  427. var month = this.state.month_array[this.state.month_array_views_indexs];
  428. var day = this.state.day_array[this.state.day_array_views_index];
  429. this.props.birthdaycommit(year, month, day);
  430. this.setModalVisible(false);
  431. }
  432. }
  433. cancel() {
  434. this.setModalVisible(false);
  435. // alert(this.getmyDate());
  436. }
  437. setModalVisible(visible) {
  438. this.setState({
  439. modalVisible: visible
  440. });
  441. }
  442. getmyDate() {
  443. var date = new Date();
  444. var year = date.getFullYear().toString();
  445. var month = (date.getMonth() + 1).toString();
  446. var day = date.getDate().toString();
  447. var hour = date.getHours().toString();
  448. var minute = date.getMinutes().toString();
  449. return year + '年' + month + '月' + day + '日' + ' ' + hour + ':' + minute;
  450. }
  451. getRunYear(year) {
  452. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
  453. //闰年2月29
  454. // console.log("闰年");
  455. return true;
  456. } else {
  457. //平年2月28
  458. // console.log("平年");
  459. return false;
  460. }
  461. }
  462. }
  463. const styles = StyleSheet.create({
  464. item_text: {
  465. color: 'rgba(77, 77, 77, 1)',
  466. fontSize: 18,
  467. justifyContent: 'center',
  468. alignItems: 'center',
  469. marginTop: 20,
  470. width: '100%',
  471. textAlignVertical: 'center',
  472. textAlign: 'center'
  473. },
  474. item_text_click: {
  475. color: 'rgba(59, 149, 243, 1)',
  476. fontSize: 18,
  477. justifyContent: 'center',
  478. alignItems: 'center',
  479. marginTop: 20,
  480. width: '100%',
  481. textAlignVertical: 'center',
  482. textAlign: 'center'
  483. }
  484. });