Skip to content

Commit ea81a4e

Browse files
committed
fix day renders
1 parent 56b0ef4 commit ea81a4e

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

example/src/screens/horizontalCalendarList.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
import React, {useState} from 'react';
1+
import React, {useState, useMemo, useCallback} from 'react';
22
// @ts-expect-error
33
import {CalendarList} from 'react-native-calendars';
44
import testIDs from '../testIDs';
55

6-
const initialDate = '2020-05-16';
6+
const INITIAL_DATE = '2020-05-16';
77

88
const HorizontalCalendarList = () => {
9-
const [selected, setSelected] = useState(initialDate);
10-
const markedDates = {
11-
[selected]: {
12-
selected: true,
13-
selectedColor: '#DFA460'
14-
}
15-
};
9+
const [selected, setSelected] = useState(INITIAL_DATE);
10+
const markedDates = useMemo(() => {
11+
return {
12+
[selected]: {
13+
selected: true,
14+
selectedColor: '#DFA460'
15+
}
16+
};
17+
}, [selected]);
1618

17-
const onDayPress = day => {
19+
const onDayPress = useCallback((day) => {
1820
setSelected(day.dateString);
19-
};
21+
}, []);
2022

2123
return (
2224
<CalendarList
23-
testID={testIDs.horizontalList.CONTAINER}
25+
current={INITIAL_DATE}
2426
markedDates={markedDates}
25-
current={initialDate}
2627
pastScrollRange={24}
2728
futureScrollRange={24}
2829
horizontal
2930
pagingEnabled
3031
onDayPress={onDayPress}
32+
testID={testIDs.horizontalList.CONTAINER}
33+
staticHeader
3134
/>
3235
);
3336
};

src/calendar/index.tsx

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import PropTypes from 'prop-types';
22
import XDate from 'xdate';
3-
import memoize from 'memoize-one';
43

54
import React, {useRef, useState, useEffect, useCallback} from 'react';
65
import {View, ViewStyle, StyleProp} from 'react-native';
@@ -113,9 +112,7 @@ const Calendar = (props: CalendarProps) => {
113112
const max = parseDate(maxDate);
114113

115114
if (allowSelectionOutOfRange || !(min && !isGTE(day, min)) && !(max && !isLTE(day, max))) {
116-
const shouldUpdateMonth = disableMonthChange === undefined || !disableMonthChange;
117-
118-
if (shouldUpdateMonth) {
115+
if (!disableMonthChange) {
119116
updateMonth(day);
120117
}
121118
if (interaction) {
@@ -124,17 +121,27 @@ const Calendar = (props: CalendarProps) => {
124121
}
125122
};
126123

127-
const pressDay = useCallback((date?: DateData) => {
124+
const onPressDay = useCallback((date?: DateData) => {
128125
if (date)
129126
handleDayInteraction(date, onDayPress);
130-
}, [onDayPress]);
127+
}, []);
131128

132-
const longPressDay = useCallback((date?: DateData) => {
129+
const onLongPressDay = useCallback((date?: DateData) => {
133130
if (date)
134131
handleDayInteraction(date, onDayLongPress);
135-
}, [onDayLongPress]);
132+
}, []);
136133

137-
const onSwipe = (gestureName: string) => {
134+
const onSwipeLeft = useCallback(() => {
135+
// @ts-expect-error
136+
header.current?.onPressRight();
137+
}, [header]);
138+
139+
const onSwipeRight = useCallback(() => {
140+
// @ts-expect-error
141+
header.current?.onPressLeft();
142+
}, [header]);
143+
144+
const onSwipe = useCallback((gestureName: string) => {
138145
const {SWIPE_UP, SWIPE_DOWN, SWIPE_LEFT, SWIPE_RIGHT} = swipeDirections;
139146

140147
switch (gestureName) {
@@ -148,19 +155,9 @@ const Calendar = (props: CalendarProps) => {
148155
onSwipeRight();
149156
break;
150157
}
151-
};
152-
153-
const onSwipeLeft = () => {
154-
// @ts-expect-error
155-
header.current?.onPressRight();
156-
};
158+
}, [onSwipeLeft, onSwipeRight]);
157159

158-
const onSwipeRight = () => {
159-
// @ts-expect-error
160-
header.current?.onPressLeft();
161-
};
162-
163-
const renderWeekNumber = memoize(weekNumber => {
160+
const renderWeekNumber = (weekNumber: number) => {
164161
return (
165162
<View style={style.current.dayContainer} key={`week-container-${weekNumber}`}>
166163
<BasicDay
@@ -174,7 +171,7 @@ const Calendar = (props: CalendarProps) => {
174171
</BasicDay>
175172
</View>
176173
);
177-
});
174+
};
178175

179176
const renderDay = (day: XDate, id: number) => {
180177
const dayProps = extractComponentProps(Day, props);
@@ -190,8 +187,8 @@ const Calendar = (props: CalendarProps) => {
190187
date={toMarkingFormat(day)}
191188
state={getState(day, currentMonth, props)}
192189
marking={markedDates?.[toMarkingFormat(day)]}
193-
onPress={pressDay}
194-
onLongPress={longPressDay}
190+
onPress={onPressDay}
191+
onLongPress={onLongPressDay}
195192
/>
196193
</View>
197194
);
@@ -256,7 +253,9 @@ const Calendar = (props: CalendarProps) => {
256253
};
257254

258255
const GestureComponent = enableSwipeMonths ? GestureRecognizer : View;
259-
const swipeProps = {onSwipe: (direction: string) => onSwipe(direction)};
256+
const swipeProps = {
257+
onSwipe: (direction: string) => onSwipe(direction)
258+
};
260259
const gestureProps = enableSwipeMonths ? swipeProps : undefined;
261260

262261
return (

0 commit comments

Comments
 (0)