import React, { useState } from 'react'; import { View, Text, StyleSheet } from 'react-native'; // eslint-disable-next-line import/no-unresolved import IonIcon from 'react-native-vector-icons/Ionicons'; import Ripple from 'react-native-material-ripple'; import { useColors } from '../../themes'; import { CalendarProps } from './types'; import { Fonts } from '../../styles'; import { scaleDimension, customScaleDimension } from '../../common/utils'; const styles = StyleSheet.create({ calendarContainer: { borderTopLeftRadius: 10, borderTopRightRadius: 10, shadowColor: '#000', shadowOffset: { width: 0, height: 1, }, shadowOpacity: 0.22, shadowRadius: 2.22, elevation: 3, }, }); const months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ]; const nDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; const Calendar = (props: CalendarProps) => { const [activeDate, setActiveDate] = useState(new Date()); // getting the suitable color based on the theme // activated inside the app const Colors = useColors(); const { setDate, title, children } = props; const changeMonth = (delta: number) => { const newTimeInMS = activeDate.setMonth(activeDate.getMonth() + delta); const updatedDate = new Date(newTimeInMS); setActiveDate(updatedDate); }; const changeDay = (dateOfMonth: number) => { const newTimeInMS = activeDate.setMonth(activeDate.getMonth(), dateOfMonth); const updatedDate = new Date(newTimeInMS); setActiveDate(updatedDate); }; setDate(activeDate); const generateMatrix = () => { const matrix = []; // Create header matrix[0] = ['1', '2', '3', '4', '5', '6', '7']; const year = activeDate.getFullYear(); const month = activeDate.getMonth(); // const firstDay = new Date(year, month, 1).getDay(); let maxDays = nDays[month]; if (month === 1) { // February if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) { maxDays += 1; } } let counter = 8; for (let row = 1; row < 5; row += 1) { matrix[row] = []; for (let col = 0; col < 7; col += 1) { if (counter <= maxDays) { matrix[row][col] = counter.toString(); counter += 1; } else { matrix[row][col] = ''; } } } return matrix; }; const matrix = generateMatrix(); const renderEachRow = (daysArray: string[]) => { return ( {daysArray.map((item) => { return item === '' ? null : ( changeDay(parseInt(item, 10))} rippleContainerBorderRadius={18} > {item} ); })} ); }; return ( {title} changeMonth(-1)} > {months[activeDate.getMonth()]} changeMonth(1)} > {matrix.map((item) => renderEachRow(item))} {children} ); }; Calendar.defaultProps = { title: 'Select Date:', }; export default Calendar;