Advertisement
Felanpro

React Native (Calendar App Documentation)

May 20th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import * as React from 'react';
  2. import * as RN from 'react-native';
  3.  
  4. class MyCalendar extends React.Component {
  5. months = ["January", "February", "March", "April",
  6. "May", "June", "July", "August", "September", "October",
  7. "November", "December"
  8. ];
  9.  
  10. _onPress = (item) => {
  11. this.setState(() => {
  12. if (!item.match && item != -1) {
  13. this.state.activeDate.setDate(item);
  14. return this.state;
  15. }
  16. });
  17. };
  18.  
  19. changeMonth = (n) => {
  20. this.setState(() => {
  21. this.state.activeDate.setMonth(
  22. this.state.activeDate.getMonth() + n
  23. )
  24. return this.state;
  25. });
  26. }
  27.  
  28. weekDays = [
  29. "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
  30. ];
  31.  
  32. nDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  33.  
  34. state = {
  35. activeDate: new Date()
  36. }
  37.  
  38. generateMatrix() {
  39. var matrix = [];
  40. // Create header
  41. matrix[0] = this.weekDays;
  42.  
  43. var year = this.state.activeDate.getFullYear();
  44. var month = this.state.activeDate.getMonth();
  45. var firstDay = new Date(year, month, 1).getDay();
  46.  
  47. var maxDays = this.nDays[month];
  48. if (month == 1) { // February
  49. if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
  50. maxDays += 1;
  51. }
  52. }
  53.  
  54. var counter = 1;
  55. for (var row = 1; row < 7; row++) {
  56. matrix[row] = [];
  57. for (var col = 0; col < 7; col++) {
  58. matrix[row][col] = -1;
  59. if (row == 1 && col >= firstDay) {
  60. // Fill in rows only after the first day of the month
  61. matrix[row][col] = counter++;
  62. } else if (row > 1 && counter <= maxDays) {
  63. // Fill in rows only if the counter's not greater than
  64. // the number of days in the month
  65. matrix[row][col] = counter++;
  66. }
  67. }
  68. }
  69.  
  70. return matrix;
  71. }
  72.  
  73.  
  74. render() {
  75. var matrix = this.generateMatrix();
  76.  
  77. var rows = [];
  78. rows = matrix.map((row, rowIndex) => {
  79. var rowItems = row.map((item, colIndex) => {
  80. return (
  81. <RN.Text
  82. style={{
  83. flex: 1,
  84. height: 18,
  85. textAlign: 'center',
  86. // Highlight header
  87. backgroundColor: rowIndex == 0 ? '#ddd' : '#fff',
  88. // Highlight Sundays
  89. color: colIndex == 0 ? '#a00' : '#000',
  90. // Highlight current date
  91. fontWeight: item == this.state.activeDate.getDate()
  92. ? 'bold': ''
  93. }}
  94. onPress={() => this._onPress(item)}>
  95. {item != -1 ? item : ''}
  96. </RN.Text>
  97. );
  98. });
  99.  
  100. return (
  101. <RN.View
  102. style={{
  103. flex: 1,
  104. flexDirection: 'row',
  105. padding: 15,
  106. justifyContent: 'space-around',
  107. alignItems: 'center',
  108. }}>
  109. {rowItems}
  110. </RN.View>
  111. );
  112. });
  113.  
  114. return (
  115. <RN.SafeAreaView>
  116. <RN.Text style={{
  117. fontWeight: 'bold',
  118. fontSize: 18,
  119. textAlign: 'center'
  120. }}>
  121. {this.months[this.state.activeDate.getMonth()]} &nbsp;
  122. {this.state.activeDate.getFullYear()}
  123. </RN.Text>
  124. { rows }
  125.  
  126. <RN.Button title="Previous"
  127. onPress={() => this.changeMonth(-1)}/>
  128.  
  129. <RN.Button title="Next"
  130. onPress={() => this.changeMonth(+1)}/>
  131. </RN.SafeAreaView>
  132. );
  133. }
  134. }
  135.  
  136. // Export for now to get rid of error and see preview:
  137. export default MyCalendar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement