import React, { ReactElement, useCallback, useMemo } from 'react'; import { StyleSheet, TouchableOpacity, View, Text } from 'react-native'; import { DayType, ThemeType, DayDot, DayTheme, DateString } from '../../types'; import Dot from '../Dot'; import { getDayFromDateString } from '../../utils/date'; const styles = StyleSheet.create({ activeDate: { backgroundColor: '#3b5998', }, container: { alignItems: 'center', justifyContent: 'center', backgroundColor: 'white', flex: 1, marginVertical: 5, paddingVertical: 10, }, content: { alignItems: 'center', justifyContent: 'center', }, dotsContainer: { position: 'absolute', bottom: -5, flexDirection: 'row' }, endDate: { borderBottomRightRadius: 60, borderTopRightRadius: 60, }, startDate: { borderBottomLeftRadius: 60, borderTopLeftRadius: 60, }, nonTouchableDayText: { color: '#d3d3d3' }, }); interface NonTouchableDayProps { date: DateString; isActive: boolean; isMonthDate: boolean; isOutOfRange: boolean; isStartDate: boolean; isEndDate: boolean; isVisible: boolean; isWeekend: boolean; isToday: boolean; theme: ThemeType; dayTheme?: DayTheme; } const NonTouchableDay = React.memo( (props: NonTouchableDayProps) => { const { isMonthDate, isActive, isOutOfRange, isStartDate, isEndDate, theme, dayTheme, date, isWeekend, isToday, } = props; return ( {getDayFromDateString(date)} ); }, (prevProps, nextProps) => { return ( prevProps.isActive === nextProps.isActive && prevProps.isVisible === nextProps.isVisible && prevProps.isStartDate === nextProps.isStartDate && prevProps.isEndDate === nextProps.isEndDate ); } ); interface Props { onPress: (date: DateString) => void; dots?: DayDot[]; dayTheme?: DayTheme; item: DayType; theme: ThemeType; renderDayContent?: (day: DayType) => ReactElement; } const Day = React.memo( (props: Props) => { const { item: { date, isVisible, isActive, isStartDate, isEndDate, isMonthDate, isOutOfRange, isToday, isWeekend, isHidden, }, dots = [], dayTheme, theme, } = props; const dayTextStyle = useMemo( () => ({ color: isActive ? 'white' : 'black', }), [isActive] ); const renderDot = useCallback( (d: DayDot, i) => { return ( ); }, [isActive, theme.dotContainerStyle] ); if (isHidden) { return ; } if (!isVisible) { return ( ); } // Should render a maximum of 3 dots const finalDots = dots.slice(0, 3); return ( props.onPress(props.item.date)} > {props.renderDayContent ? ( props.renderDayContent(props.item) ) : ( {getDayFromDateString(date)} {finalDots.map(renderDot)} )} ); }, (prevProps, nextProps) => { return ( prevProps.onPress === nextProps.onPress && prevProps.item.isActive === nextProps.item.isActive && prevProps.item.isVisible === nextProps.item.isVisible && prevProps.item.isStartDate === nextProps.item.isStartDate && prevProps.item.isEndDate === nextProps.item.isEndDate && prevProps.renderDayContent === nextProps.renderDayContent && prevProps.dots?.length === nextProps.dots?.length ); } ); export default Day;