Skip to content

Commit d638307

Browse files
committed
Calendar - formatting file; adding 'shouldDisplayIndicator'
1 parent de68f00 commit d638307

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/calendar/index.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import PropTypes from 'prop-types';
22
import XDate from 'xdate';
33

4-
import React, {useRef, useState, useEffect, useCallback} from 'react';
4+
import React, {useRef, useState, useEffect, useCallback, useMemo} from 'react';
55
import {View, ViewStyle, StyleProp} from 'react-native';
66
// @ts-expect-error
77
import GestureRecognizer, {swipeDirections} from 'react-native-swipe-gestures';
@@ -78,7 +78,33 @@ export interface CalendarProps extends CalendarHeaderProps, DayProps {
7878
* @gif: https://github.com/wix/react-native-calendars/blob/master/demo/assets/calendar.gif
7979
*/
8080
const Calendar = (props: CalendarProps) => {
81-
const {initialDate, current, theme, disableMonthChange, allowSelectionOutOfRange, minDate, maxDate, onDayPress, onDayLongPress, hideExtraDays, markedDates, firstDay, showSixWeeks, customHeader, headerStyle, displayLoadingIndicator, testID, enableSwipeMonths, accessibilityElementsHidden, importantForAccessibility, onMonthChange, onVisibleMonthsChange, style: propsStyle} = props;
81+
useWhyDidYouUpdate(props.current, props);
82+
83+
const {
84+
initialDate,
85+
current,
86+
theme,
87+
markedDates,
88+
minDate,
89+
maxDate,
90+
allowSelectionOutOfRange,
91+
onDayPress,
92+
onDayLongPress,
93+
onMonthChange,
94+
onVisibleMonthsChange,
95+
disableMonthChange,
96+
enableSwipeMonths,
97+
hideExtraDays,
98+
firstDay,
99+
showSixWeeks,
100+
displayLoadingIndicator,
101+
customHeader,
102+
headerStyle,
103+
accessibilityElementsHidden,
104+
importantForAccessibility,
105+
testID,
106+
style: propsStyle
107+
} = props;
82108
const [currentMonth, setCurrentMonth] = useState(current || initialDate ? parseDate(current || initialDate) : new XDate());
83109
const style = useRef(styleConstructor(theme));
84110
const header = useRef();
@@ -232,16 +258,17 @@ const Calendar = (props: CalendarProps) => {
232258
return <View style={style.current.monthView}>{weeks}</View>;
233259
};
234260

235-
const renderHeader = () => {
236-
let indicator;
237-
261+
const shouldDisplayIndicator = useMemo(() => {
238262
if (currentMonth) {
239263
const lastMonthOfDay = toMarkingFormat(currentMonth.clone().addMonths(1, true).setDate(1).addDays(-1));
240264
if (displayLoadingIndicator && !markedDates?.[lastMonthOfDay]) {
241-
indicator = true;
265+
return true;
242266
}
243267
}
268+
return false;
269+
}, [currentMonth, displayLoadingIndicator, markedDates]);
244270

271+
const renderHeader = () => {
245272
const headerProps = extractHeaderProps(props);
246273
const ref = customHeader ? undefined : header;
247274
const CustomHeader = customHeader;
@@ -255,7 +282,7 @@ const Calendar = (props: CalendarProps) => {
255282
ref={ref}
256283
month={currentMonth}
257284
addMonth={addMonth}
258-
displayLoadingIndicator={indicator}
285+
displayLoadingIndicator={shouldDisplayIndicator}
259286
/>
260287
);
261288
};

0 commit comments

Comments
 (0)