Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import isEmpty from 'lodash/isEmpty';
import PropTypes from 'prop-types';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { StyleProp, View, ViewStyle } from 'react-native';
import XDate from 'xdate';
import isEmpty from 'lodash/isEmpty';
import React, {useRef, useState, useEffect, useCallback, useMemo} from 'react';
import {AccessibilityInfo, View, ViewStyle, StyleProp} from 'react-native';
// @ts-expect-error
import GestureRecognizer, {swipeDirections} from 'react-native-swipe-gestures';
import GestureRecognizer, { swipeDirections } from 'react-native-swipe-gestures';
import constants from '../commons/constants';
import {page, isGTE, isLTE, sameMonth} from '../dateutils';
import {xdateToData, parseDate, toMarkingFormat} from '../interface';
import {getState} from '../day-state-manager';
import {extractHeaderProps, extractDayProps} from '../componentUpdater';
import {DateData, Theme, MarkedDates, ContextProp} from '../types';
import {useDidUpdate} from '../hooks';
import styleConstructor from './style';
import CalendarHeader, {CalendarHeaderProps} from './header';
import Day, {DayProps} from './day/index';
import { extractDayProps, extractHeaderProps } from '../componentUpdater';
import { isGTE, isLTE, page, sameMonth } from '../dateutils';
import { getState } from '../day-state-manager';
import { useDidUpdate } from '../hooks';
import { parseDate, toMarkingFormat, xdateToData } from '../interface';
import { ContextProp, DateData, MarkedDates, Theme } from '../types';
import BasicDay from './day/basic';
import Day, { DayProps } from './day/index';
import CalendarHeader, { CalendarHeaderProps } from './header';
import styleConstructor from './style';

export interface CalendarProps extends CalendarHeaderProps, DayProps {
/** Specify theme properties to override specific styles for calendar parts */
Expand Down Expand Up @@ -94,10 +97,14 @@ const Calendar = (props: CalendarProps & ContextProp) => {
style: propsStyle
} = props;
const [currentMonth, setCurrentMonth] = useState(current || initialDate ? parseDate(current || initialDate) : new XDate());
const style = useRef(styleConstructor(theme));
const [style, setStyle] = useState(styleConstructor(theme));
const header = useRef();
const weekNumberMarking = useRef({disabled: true, disableTouchEvent: true});

useEffect(() => {
setStyle(styleConstructor(theme));
}, [theme]);

useEffect(() => {
if (initialDate) {
setCurrentMonth(parseDate(initialDate));
Expand Down Expand Up @@ -174,7 +181,7 @@ const Calendar = (props: CalendarProps & ContextProp) => {

const renderWeekNumber = (weekNumber: number) => {
return (
<View style={style.current.dayContainer} key={`week-container-${weekNumber}`}>
<View style={style.dayContainer} key={`week-container-${weekNumber}`}>
<BasicDay
key={`week-${weekNumber}`}
marking={weekNumberMarking.current}
Expand All @@ -190,15 +197,15 @@ const Calendar = (props: CalendarProps & ContextProp) => {

const renderDay = (day: XDate, id: number) => {
if (!sameMonth(day, currentMonth) && hideExtraDays) {
return <View key={id} style={style.current.emptyDayContainer}/>;
return <View key={id} style={style.emptyDayContainer}/>;
}

const dayProps = extractDayProps(props);
const dateString = toMarkingFormat(day);
const disableDaySelection = isEmpty(props.context);

return (
<View style={style.current.dayContainer} key={id}>
<View style={style.dayContainer} key={id}>
<Day
{...dayProps}
testID={`${testID}.day_${dateString}`}
Expand All @@ -224,7 +231,7 @@ const Calendar = (props: CalendarProps & ContextProp) => {
}

return (
<View style={style.current.week} key={id}>
<View style={style.week} key={id}>
{week}
</View>
);
Expand All @@ -239,7 +246,7 @@ const Calendar = (props: CalendarProps & ContextProp) => {
weeks.push(renderWeek(days.splice(0, 7), weeks.length));
}

return <View style={style.current.monthView}>{weeks}</View>;
return <View style={style.monthView}>{weeks}</View>;
};

const shouldDisplayIndicator = useMemo(() => {
Expand Down Expand Up @@ -280,7 +287,7 @@ const Calendar = (props: CalendarProps & ContextProp) => {
return (
<GestureComponent {...gestureProps} testID={`${testID}.container`}>
<View
style={[style.current.container, propsStyle]}
style={[style.container, propsStyle]}
testID={testID}
accessibilityElementsHidden={accessibilityElementsHidden} // iOS
importantForAccessibility={importantForAccessibility} // Android
Expand Down