BirthdayModal.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 date = new Date();
  254. var year = parseInt(this.props.year);
  255. console.log('========BirthdayModal.js---componentWillMount============================');
  256. console.log(year);
  257. if (this.state.year_array.length == 0) {
  258. let position = 0;
  259. for (let index = parseInt(year - 50); index < parseInt(year + 10); index++) {
  260. if (parseInt(year) == index) {
  261. this.setState({
  262. year_array_views_index: position
  263. });
  264. }
  265. this.state.year_array.push(index);
  266. position++;
  267. }
  268. }
  269. this.setState({
  270. month_array_views_indexs: parseInt(this.props.month - 1),
  271. day_array_views_index: parseInt(this.props.day - 1)
  272. });
  273. // var hour = date.getHours().toString();
  274. // var minute = date.getMinutes().toString();
  275. }
  276. year_onlayout() {
  277. if (this.state.year_array_views_index != -1) {
  278. this.year_scroll.scrollTo({
  279. x: 0,
  280. y: (this.state.text_height + 20) * this.state.year_array_views_index,
  281. duration: 500
  282. });
  283. }
  284. }
  285. month_onlayout() {
  286. if (this.state.month_array_views_indexs != -1) {
  287. this.month_scroll.scrollTo({
  288. x: 0,
  289. y: (this.state.text_height + 20) * this.state.month_array_views_indexs,
  290. duration: 500
  291. });
  292. }
  293. }
  294. day__onlayout() {
  295. if (this.state.day_array_views_index != -1) {
  296. this.day_scroll.scrollTo({
  297. x: 0,
  298. y: (this.state.text_height + 20) * this.state.day_array_views_index,
  299. duration: 500
  300. });
  301. }
  302. }
  303. create_year_item() {
  304. this.state.year_array_views = [];
  305. for (var i = 0; i < this.state.year_array.length; i++) {
  306. let index = i;
  307. let textstyle = null;
  308. if (this.state.year_array_views_index == i) {
  309. textstyle = styles.item_text_click;
  310. } else {
  311. textstyle = styles.item_text;
  312. }
  313. this.state.year_array_views.push(
  314. <Text
  315. style={textstyle}
  316. key={i}
  317. onPress={() => this.click_year_item(index)}
  318. onLayout={(event) => this.text_onLayout(event)}
  319. >
  320. {this.state.year_array[i]}年
  321. </Text>
  322. );
  323. }
  324. return this.state.year_array_views;
  325. }
  326. click_year_item(index) {
  327. this.setState({
  328. year_array_views_index: index,
  329. year_type: this.getRunYear(this.state.year_array[index])
  330. });
  331. }
  332. create_month_item() {
  333. this.state.month_array_views = [];
  334. for (var i = 0; i < this.state.month_array.length; i++) {
  335. let index = i;
  336. let textstyle = null;
  337. if (this.state.month_array_views_indexs == i) {
  338. textstyle = styles.item_text_click;
  339. } else {
  340. textstyle = styles.item_text;
  341. }
  342. this.state.month_array_views.push(
  343. <Text style={textstyle} key={i} onPress={() => this.click_month_item(index)}>
  344. {this.state.month_array[i]}月
  345. </Text>
  346. );
  347. }
  348. return this.state.month_array_views;
  349. }
  350. click_month_item(index) {
  351. this.setState({
  352. month_array_views_indexs: index
  353. });
  354. }
  355. create_day_item() {
  356. this.state.day_array_views = [];
  357. var forNum = 0;
  358. //获取月份
  359. var month_index = this.state.month_array_views_indexs;
  360. switch (month_index + 1) {
  361. case 1:
  362. case 3:
  363. case 5:
  364. case 7:
  365. case 8:
  366. case 10:
  367. case 12:
  368. //31天
  369. forNum = 31;
  370. break;
  371. case 4:
  372. case 6:
  373. case 9:
  374. case 11:
  375. //30天
  376. forNum = 30;
  377. break;
  378. case 2:
  379. //2月判断平年闰年
  380. if (this.getRunYear(this.state.year_array[this.state.year_array_views_index])) {
  381. //闰年2月29
  382. forNum = 29;
  383. } else {
  384. //平年2月28
  385. forNum = 28;
  386. }
  387. break;
  388. }
  389. for (var i = 0; i < forNum; i++) {
  390. //console.log(i);
  391. let index = i;
  392. let textstyle = null;
  393. if (this.state.day_array_views_index == i) {
  394. textstyle = styles.item_text_click;
  395. } else {
  396. textstyle = styles.item_text;
  397. }
  398. this.state.day_array_views.push(
  399. <Text style={textstyle} key={i} onPress={() => this.click_day_item(index)}>
  400. {this.state.day_array[i]}日
  401. </Text>
  402. );
  403. }
  404. return this.state.day_array_views;
  405. }
  406. click_day_item(index) {
  407. this.setState({
  408. day_array_views_index: index
  409. });
  410. }
  411. text_onLayout = (event) => {
  412. if (this.state.text_height == -1) {
  413. this.setState({
  414. text_height: event.nativeEvent.layout.height
  415. });
  416. } else {
  417. }
  418. };
  419. commit() {
  420. if (
  421. this.state.year_array_views_index == -1 ||
  422. this.state.month_array_views_indexs == -1 ||
  423. this.state.day_array_views_index == -1
  424. ) {
  425. alert('请选择完整日期');
  426. } else {
  427. var year = this.state.year_array[this.state.year_array_views_index];
  428. var month = this.state.month_array[this.state.month_array_views_indexs];
  429. var day = this.state.day_array[this.state.day_array_views_index];
  430. this.props.birthdaycommit(year, month, day);
  431. this.setModalVisible(false);
  432. }
  433. }
  434. cancel() {
  435. this.setModalVisible(false);
  436. // alert(this.getmyDate());
  437. }
  438. setModalVisible(visible) {
  439. this.setState({
  440. modalVisible: visible
  441. });
  442. }
  443. getmyDate() {
  444. var date = new Date();
  445. var year = date.getFullYear().toString();
  446. var month = (date.getMonth() + 1).toString();
  447. var day = date.getDate().toString();
  448. var hour = date.getHours().toString();
  449. var minute = date.getMinutes().toString();
  450. return year + '年' + month + '月' + day + '日' + ' ' + hour + ':' + minute;
  451. }
  452. getRunYear(year) {
  453. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
  454. //闰年2月29
  455. // console.log("闰年");
  456. return true;
  457. } else {
  458. //平年2月28
  459. // console.log("平年");
  460. return false;
  461. }
  462. }
  463. }
  464. const styles = StyleSheet.create({
  465. item_text: {
  466. color: 'rgba(77, 77, 77, 1)',
  467. fontSize: 18,
  468. justifyContent: 'center',
  469. alignItems: 'center',
  470. marginTop: 20,
  471. width: '100%',
  472. textAlignVertical: 'center',
  473. textAlign: 'center'
  474. },
  475. item_text_click: {
  476. color: 'rgba(59, 149, 243, 1)',
  477. fontSize: 18,
  478. justifyContent: 'center',
  479. alignItems: 'center',
  480. marginTop: 20,
  481. width: '100%',
  482. textAlignVertical: 'center',
  483. textAlign: 'center'
  484. }
  485. });