BirthdayModal.js 10.0 KB

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