Skip to content

Commit ed1afcf

Browse files
committed
Merge branch 'master' of github.com:wix/react-native-calendars into infra/more_functional_components
2 parents 5c48f04 + 530f747 commit ed1afcf

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/agenda/index.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ export default class Agenda extends Component<AgendaProps, State> {
114114
calendarIsReady: false,
115115
calendarScrollable: false,
116116
firstReservationLoad: false,
117-
selectedDay: parseDate(props.selected) || new XDate(true),
118-
topDay: parseDate(props.selected) || new XDate(true)
117+
selectedDay: this.getSelectedDate(),
118+
topDay: this.getSelectedDate()
119119
};
120120

121121
this.currentMonth = this.state.selectedDay.clone();
@@ -134,9 +134,10 @@ export default class Agenda extends Component<AgendaProps, State> {
134134
this.state.scrollY.removeAllListeners();
135135
}
136136

137-
componentDidUpdate(prevProps: AgendaProps) {
138-
if (!sameDate(parseDate(this.props.selected), parseDate(prevProps.selected))) {
139-
this.setState({selectedDay: parseDate(this.props.selected)});
137+
componentDidUpdate(prevProps: AgendaProps, prevState: State) {
138+
const newSelectedDate = this.getSelectedDate();
139+
if (!sameDate(newSelectedDate, prevState.selectedDay)) {
140+
this.setState({selectedDay: newSelectedDate});
140141
} else if (!prevProps.items) {
141142
this.loadReservations(this.props);
142143
}
@@ -149,6 +150,11 @@ export default class Agenda extends Component<AgendaProps, State> {
149150
return null;
150151
}
151152

153+
getSelectedDate() {
154+
const {selected} = this.props;
155+
return selected ? parseDate(selected) : new XDate(true);
156+
}
157+
152158
calendarOffset() {
153159
return 96 - this.viewHeight / 2;
154160
}

src/calendar/day/basic/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const BasicDay = (props: BasicDayProps) => {
5252
testID
5353
} = props;
5454
const style = useRef(styleConstructor(theme));
55-
const dateData = useRef(date ? xdateToData(new XDate(date)) : undefined);
5655
const _marking = marking || {};
5756
const isSelected = _marking.selected || state === 'selected';
5857
const isDisabled = typeof _marking.disabled !== 'undefined' ? _marking.disabled : state === 'disabled';
@@ -61,6 +60,7 @@ const BasicDay = (props: BasicDayProps) => {
6160
const isMultiDot = markingType === Marking.markings.MULTI_DOT;
6261
const isMultiPeriod = markingType === Marking.markings.MULTI_PERIOD;
6362
const isCustom = markingType === Marking.markings.CUSTOM;
63+
const dateData = date ? xdateToData(new XDate(date)) : undefined;
6464

6565
const shouldDisableTouchEvent = () => {
6666
const {disableTouchEvent} = _marking;
@@ -126,12 +126,12 @@ const BasicDay = (props: BasicDayProps) => {
126126
};
127127

128128
const _onPress = useCallback(() => {
129-
onPress?.(dateData.current);
130-
}, [onPress]);
129+
onPress?.(dateData);
130+
}, [onPress, date]);
131131

132132
const _onLongPress = useCallback(() => {
133-
onLongPress?.(dateData.current);
134-
}, [onLongPress]);
133+
onLongPress?.(dateData);
134+
}, [onLongPress, date]);
135135

136136
const renderMarking = () => {
137137
const {marked, dotColor, dots, periods} = _marking;

src/calendar/header/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ const CalendarHeader = forwardRef((props: CalendarHeaderProps, ref) => {
121121
return onPressArrowLeft(subtractMonth, month);
122122
}
123123
return subtractMonth();
124-
}, [onPressArrowLeft, subtractMonth]);
124+
}, [onPressArrowLeft, subtractMonth, month]);
125125

126126
const onPressRight = useCallback(() => {
127127
if (typeof onPressArrowRight === 'function') {
128128
return onPressArrowRight(addMonth, month);
129129
}
130130
return addMonth();
131-
}, [onPressArrowRight, addMonth]);
131+
}, [onPressArrowRight, addMonth, month]);
132132

133133
const onAccessibilityAction = (event: AccessibilityActionEvent) => {
134134
switch (event.nativeEvent.actionName) {

0 commit comments

Comments
 (0)