Advertisement
Guest User

Untitled

a guest
Jun 13th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import React, { useState } from 'react';
  2. import { Alert, StyleSheet, Text, View, TouchableOpacity } from 'react-native';
  3. import { Agenda } from 'react-native-calendars';
  4.  
  5. const testIDs = require('./testIds');
  6.  
  7. import { LocaleConfig } from 'react-native-calendars';
  8.  
  9. const Calendar = () => {
  10.  
  11. const [items, setItems] = useState({});
  12.  
  13. return (
  14. <Agenda
  15. testID={testIDs.agenda.CONTAINER}
  16. items={items}
  17. loadItemsForMonth={loadItems}
  18. selected={'2020-05-16'}
  19. renderItem={renderItem}
  20. renderEmptyDate={renderEmptyDate}
  21. rowHasChanged={rowHasChanged}
  22. // markingType={'period'}
  23. // markedDates={{
  24. // '2017-05-08': {textColor: '#43515c'},
  25. // '2017-05-09': {textColor: '#43515c'},
  26. // '2017-05-14': {startingDay: true, endingDay: true, color: 'blue'},
  27. // '2017-05-21': {startingDay: true, color: 'blue'},
  28. // '2017-05-22': {endingDay: true, color: 'gray'},
  29. // '2017-05-24': {startingDay: true, color: 'gray'},
  30. // '2017-05-25': {color: 'gray'},
  31. // '2017-05-26': {endingDay: true, color: 'gray'}}}
  32. // monthFormat={'yyyy'}
  33. // theme={{calendarBackground: 'red', agendaKnobColor: 'green'}}
  34. //renderDay={(day, item) => (<Text>{day ? day.day: 'item'}</Text>)}
  35. // hideExtraDays={false}
  36. />
  37. );
  38. }
  39.  
  40. const loadItems = (day) => {
  41. setTimeout(() => {
  42. for (let i = -15; i < 85; i++) {
  43. const time = day.timestamp + i * 24 * 60 * 60 * 1000;
  44. const strTime = timeToString(time);
  45. if (!items[strTime]) {
  46. items[strTime] = [];
  47. const numItems = Math.floor(Math.random() * 3 + 1);
  48. for (let j = 0; j < numItems; j++) {
  49. items[strTime].push({
  50. name: 'Item for ' + strTime + ' #' + j,
  51. height: Math.max(50, Math.floor(Math.random() * 150))
  52. });
  53. }
  54. }
  55. }
  56. const newItems = {};
  57. Object.keys(items).forEach(key => { newItems[key] = items[key]; });
  58. setItems(newItems)
  59. }, 1000);
  60. }
  61.  
  62. const renderItem = (item) => {
  63. return (
  64. <TouchableOpacity
  65. testID={testIDs.agenda.ITEM}
  66. style={[styles.item, { height: item.height }]}
  67. onPress={() => Alert.alert(item.name)}
  68. >
  69. <Text>{item.name}</Text>
  70. </TouchableOpacity>
  71. );
  72. }
  73.  
  74. const renderEmptyDate = () => {
  75. return (
  76. <View style={styles.emptyDate}>
  77. <Text>Esta é uma data vazia!</Text>
  78. </View>
  79. );
  80. }
  81.  
  82. const rowHasChanged = (r1, r2) => {
  83. return r1.name !== r2.name;
  84. }
  85.  
  86. const timeToString = (time) => {
  87. const date = new Date(time);
  88. return date.toISOString().split('T')[0];
  89. }
  90.  
  91. const styles = StyleSheet.create({
  92. item: {
  93. backgroundColor: 'white',
  94. flex: 1,
  95. borderRadius: 5,
  96. padding: 10,
  97. marginRight: 10,
  98. marginTop: 17
  99. },
  100. emptyDate: {
  101. height: 15,
  102. flex: 1,
  103. paddingTop: 30
  104. }
  105. });
  106.  
  107. export default Calendar;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement