Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { useState } from 'react';
- import { Alert, StyleSheet, Text, View, TouchableOpacity } from 'react-native';
- import { Agenda } from 'react-native-calendars';
- const testIDs = require('./testIds');
- import { LocaleConfig } from 'react-native-calendars';
- const Calendar = () => {
- const [items, setItems] = useState({});
- return (
- <Agenda
- testID={testIDs.agenda.CONTAINER}
- items={items}
- loadItemsForMonth={loadItems}
- selected={'2020-05-16'}
- renderItem={renderItem}
- renderEmptyDate={renderEmptyDate}
- rowHasChanged={rowHasChanged}
- // markingType={'period'}
- // markedDates={{
- // '2017-05-08': {textColor: '#43515c'},
- // '2017-05-09': {textColor: '#43515c'},
- // '2017-05-14': {startingDay: true, endingDay: true, color: 'blue'},
- // '2017-05-21': {startingDay: true, color: 'blue'},
- // '2017-05-22': {endingDay: true, color: 'gray'},
- // '2017-05-24': {startingDay: true, color: 'gray'},
- // '2017-05-25': {color: 'gray'},
- // '2017-05-26': {endingDay: true, color: 'gray'}}}
- // monthFormat={'yyyy'}
- // theme={{calendarBackground: 'red', agendaKnobColor: 'green'}}
- //renderDay={(day, item) => (<Text>{day ? day.day: 'item'}</Text>)}
- // hideExtraDays={false}
- />
- );
- }
- const loadItems = (day) => {
- setTimeout(() => {
- for (let i = -15; i < 85; i++) {
- const time = day.timestamp + i * 24 * 60 * 60 * 1000;
- const strTime = timeToString(time);
- if (!items[strTime]) {
- items[strTime] = [];
- const numItems = Math.floor(Math.random() * 3 + 1);
- for (let j = 0; j < numItems; j++) {
- items[strTime].push({
- name: 'Item for ' + strTime + ' #' + j,
- height: Math.max(50, Math.floor(Math.random() * 150))
- });
- }
- }
- }
- const newItems = {};
- Object.keys(items).forEach(key => { newItems[key] = items[key]; });
- setItems(newItems)
- }, 1000);
- }
- const renderItem = (item) => {
- return (
- <TouchableOpacity
- testID={testIDs.agenda.ITEM}
- style={[styles.item, { height: item.height }]}
- onPress={() => Alert.alert(item.name)}
- >
- <Text>{item.name}</Text>
- </TouchableOpacity>
- );
- }
- const renderEmptyDate = () => {
- return (
- <View style={styles.emptyDate}>
- <Text>Esta é uma data vazia!</Text>
- </View>
- );
- }
- const rowHasChanged = (r1, r2) => {
- return r1.name !== r2.name;
- }
- const timeToString = (time) => {
- const date = new Date(time);
- return date.toISOString().split('T')[0];
- }
- const styles = StyleSheet.create({
- item: {
- backgroundColor: 'white',
- flex: 1,
- borderRadius: 5,
- padding: 10,
- marginRight: 10,
- marginTop: 17
- },
- emptyDate: {
- height: 15,
- flex: 1,
- paddingTop: 30
- }
- });
- export default Calendar;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement