Skip to content

Commit 27d805a

Browse files
committed
moving to useMemo
1 parent 5d45c9f commit 27d805a

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

src/expandableCalendar/Context/Provider.tsx

Lines changed: 17 additions & 13 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, {useEffect, useRef, useState, useCallback} from 'react';
4+
import React, {useEffect, useRef, useState, useCallback, useMemo} from 'react';
55
import {Animated, TouchableOpacity, View, ViewStyle, ViewProps, StyleProp} from 'react-native';
66

77
import {toMarkingFormat} from '../../interface';
@@ -69,6 +69,10 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
6969
const buttonY = useRef(new Animated.Value(todayBottomMargin ? -todayBottomMargin : -TOP_POSITION));
7070
const opacity = useRef(new Animated.Value(1));
7171

72+
const wrapperStyle = useMemo(() => {
73+
return [style.current.contextWrapper, propsStyle];
74+
}, [style, propsStyle]);
75+
7276
useEffect(() => {
7377
if (date) {
7478
_setDate(date, UpdateSources.PROP_UPDATE);
@@ -79,16 +83,6 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
7983
animateTodayButton(currentDate);
8084
}, [todayBottomMargin, currentDate]);
8185

82-
const getProviderContextValue = () => {
83-
return {
84-
date: currentDate,
85-
prevDate: prevDate,
86-
updateSource: updateSource,
87-
setDate: _setDate,
88-
setDisabled: _setDisabled
89-
};
90-
};
91-
9286
const _setDate = (date: string, updateSource: UpdateSources) => {
9387
const updateState = (buttonIcon: number) => {
9488
setPrevDate(currentDate);
@@ -109,6 +103,16 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
109103
setDisabled(showTodayButton, disabled, isDisabled, updateState);
110104
};
111105

106+
const getProviderContextValue = useMemo(() => {
107+
return {
108+
date: currentDate,
109+
prevDate: prevDate,
110+
updateSource: updateSource,
111+
setDate: _setDate,
112+
setDisabled: _setDisabled
113+
};
114+
}, [currentDate, prevDate, updateSource]);
115+
112116
const animateTodayButton = (date: string) => {
113117
if (shouldAnimateTodayButton(props)) {
114118
const animationData = getPositionAnimation(date, todayBottomMargin);
@@ -153,8 +157,8 @@ const CalendarProvider = (props: CalendarContextProviderProps) => {
153157
};
154158

155159
return (
156-
<CalendarContext.Provider value={getProviderContextValue()}>
157-
<View style={[style.current.contextWrapper, propsStyle]}>{children}</View>
160+
<CalendarContext.Provider value={getProviderContextValue}>
161+
<View style={wrapperStyle}>{children}</View>
158162
{showTodayButton && renderTodayButton()}
159163
</CalendarContext.Provider>
160164
);

src/expandableCalendar/index.tsx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
126126
} = props;
127127

128128
/** Date */
129+
129130
const {date, setDate} = useContext(Context);
130131
const initialDate = date;
131132
const getYear = (date: string) => {
@@ -161,13 +162,15 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
161162
}, [markedDates, date]);
162163

163164
/** Number of weeks */
165+
164166
const getNumberOfWeeksInMonth = (month: string) => {
165167
const days = page(parseDate(month), firstDay);
166168
return days.length / 7;
167169
};
168170
const numberOfWeeks = useRef(getNumberOfWeeksInMonth(date));
169171

170172
/** Position */
173+
171174
const closedHeight = CLOSED_HEIGHT + (hideKnob ? 0 : KNOB_CONTAINER_HEIGHT);
172175
const getOpenHeight = () => {
173176
if (!horizontal) {
@@ -186,13 +189,15 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
186189
const isOpen = position === Positions.OPEN;
187190

188191
/** Components' refs */
192+
189193
const wrapper = useRef<any>();
190194
const calendar = useRef<any>();
191195
const header = useRef<any>();
192196
const weekCalendar = useRef<any>();
193197

194198

195199
/** Styles */
200+
196201
const style = useRef(styleConstructor(theme));
197202
const themeObject = Object.assign(headerStyleOverride, theme);
198203

@@ -212,7 +217,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
212217
}
213218
};
214219

215-
const getWeekDaysStyle = useMemo(() => {
220+
const weekDaysStyle = useMemo(() => {
216221
const leftPaddings = calendarStyle?.paddingLeft;
217222
const rightPaddings = calendarStyle?.paddingRight;
218223

@@ -225,7 +230,24 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
225230
];
226231
}, [calendarStyle]);
227232

233+
const headerStyle = useMemo(() => {
234+
return [style.current.header, {height: HEADER_HEIGHT + 10, top: headerDeltaY.current}];
235+
}, [headerDeltaY.current]);
236+
237+
const weekCalendarStyle = useMemo(() => {
238+
return [style.current.weekContainer, isOpen ? style.current.hidden : style.current.visible];
239+
}, [isOpen]);
240+
241+
const containerStyle = useMemo(() => {
242+
return [allowShadow && style.current.containerShadow, propsStyle];
243+
}, [allowShadow, propsStyle]);
244+
245+
const wrapperStyle = useMemo(() => {
246+
return {height: deltaY.current};
247+
}, [deltaY.current]);
248+
228249
/** Effects */
250+
229251
useEffect(() => {
230252
if (AccessibilityInfo) {
231253
if (AccessibilityInfo.isScreenReaderEnabled) {
@@ -436,10 +458,10 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
436458
), [date]);
437459

438460
/** Renders */
439-
461+
440462
const renderWeekDaysNames = () => {
441463
return (
442-
<View style={getWeekDaysStyle}>
464+
<View style={weekDaysStyle}>
443465
<WeekDaysNames
444466
firstDay={firstDay}
445467
style={style.current.dayHeader}
@@ -454,7 +476,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
454476
return (
455477
<Animated.View
456478
ref={header}
457-
style={[style.current.header, {height: HEADER_HEIGHT + 10, top: headerDeltaY.current}]}
479+
style={headerStyle}
458480
pointerEvents={'none'}
459481
>
460482
<Text allowFontScaling={false} style={style.current.headerTitle}>
@@ -472,7 +494,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
472494
return (
473495
<Animated.View
474496
ref={weekCalendar}
475-
style={[style.current.weekContainer, isOpen ? style.current.hidden : style.current.visible]}
497+
style={weekCalendarStyle}
476498
pointerEvents={position === Positions.CLOSED ? 'auto' : 'none'}
477499
>
478500
<WeekComponent
@@ -513,7 +535,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
513535
};
514536

515537
return (
516-
<View testID={testID} style={[allowShadow && style.current.containerShadow, propsStyle]}>
538+
<View testID={testID} style={containerStyle}>
517539
{screenReaderEnabled ? (
518540
<Calendar
519541
testID="calendar"
@@ -524,7 +546,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
524546
renderArrow={_renderArrow}
525547
/>
526548
) : (
527-
<Animated.View ref={wrapper} style={{height: deltaY.current}} {...panResponder.panHandlers}>
549+
<Animated.View ref={wrapper} style={wrapperStyle} {...panResponder.panHandlers}>
528550
<CalendarList
529551
testID="calendar"
530552
horizontal={horizontal}

0 commit comments

Comments
 (0)