Skip to content

Commit eb335eb

Browse files
authored
Calendar - fix double selection (#2126)
* Calendar - fix double selection * typing the context
1 parent d18dafc commit eb335eb

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

src/calendar-list/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {xdateToData, parseDate, toMarkingFormat} from '../interface';
1010
import {page, sameDate, sameMonth} from '../dateutils';
1111
import constants from '../commons/constants';
1212
import {useDidUpdate} from '../hooks';
13+
import {ContextProp} from '../types';
1314
import styleConstructor from './style';
1415
import Calendar, {CalendarProps} from '../calendar';
1516
import CalendarListItem from './item';
@@ -51,7 +52,7 @@ export interface CalendarListImperativeMethods {
5152
* @example: https://github.com/wix/react-native-calendars/blob/master/example/src/screens/calendarsList.js
5253
* @gif: https://github.com/wix/react-native-calendars/blob/master/demo/assets/calendar-list.gif
5354
*/
54-
const CalendarList = (props: CalendarListProps, ref: any) => {
55+
const CalendarList = (props: CalendarListProps & ContextProp, ref: any) => {
5556
useImperativeHandle(ref, () => ({
5657
scrollToDay: (date: XDate | string, offset: number, animated: boolean) => {
5758
scrollToDay(date, offset, animated);

src/calendar-list/item.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import XDate from 'xdate';
2-
import React, {useRef, useMemo, useContext, useCallback} from 'react';
2+
import React, {useRef, useMemo, useCallback} from 'react';
33
import {Text} from 'react-native';
44
import {Theme} from '../types';
55
import {toMarkingFormat} from '../interface';
66
import {extractCalendarProps} from '../componentUpdater';
77
import styleConstructor from './style';
88
import Calendar, {CalendarProps} from '../calendar';
9-
import CalendarContext from '../expandableCalendar/Context';
109

1110
export type CalendarListItemProps = CalendarProps & {
1211
item: any;
@@ -32,7 +31,6 @@ const CalendarListItem = React.memo((props: CalendarListItemProps) => {
3231
onPressArrowRight,
3332
visible
3433
} = props;
35-
const context = useContext(CalendarContext);
3634

3735
const style = useRef(styleConstructor(theme));
3836

@@ -100,7 +98,6 @@ const CalendarListItem = React.memo((props: CalendarListItemProps) => {
10098
disableMonthChange
10199
onPressArrowLeft={horizontal ? _onPressArrowLeft : onPressArrowLeft}
102100
onPressArrowRight={horizontal ? _onPressArrowRight : onPressArrowRight}
103-
context={context} // ???
104101
/>
105102
);
106103
});

src/calendar/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PropTypes from 'prop-types';
22
import XDate from 'xdate';
3-
3+
import isEmpty from 'lodash/isEmpty';
44
import React, {useRef, useState, useEffect, useCallback, useMemo} from 'react';
55
import {View, ViewStyle, StyleProp} from 'react-native';
66
// @ts-expect-error
@@ -11,7 +11,7 @@ import {page, isGTE, isLTE, sameMonth} from '../dateutils';
1111
import {xdateToData, parseDate, toMarkingFormat} from '../interface';
1212
import {getState} from '../day-state-manager';
1313
import {extractHeaderProps, extractDayProps} from '../componentUpdater';
14-
import {DateData, Theme, MarkedDates} from '../types';
14+
import {DateData, Theme, MarkedDates, ContextProp} from '../types';
1515
import {useDidUpdate} from '../hooks';
1616
import styleConstructor from './style';
1717
import CalendarHeader, {CalendarHeaderProps} from './header';
@@ -70,7 +70,7 @@ export interface CalendarProps extends CalendarHeaderProps, DayProps {
7070
* @example: https://github.com/wix/react-native-calendars/blob/master/example/src/screens/calendars.js
7171
* @gif: https://github.com/wix/react-native-calendars/blob/master/demo/assets/calendar.gif
7272
*/
73-
const Calendar = (props: CalendarProps) => {
73+
const Calendar = (props: CalendarProps & ContextProp) => {
7474
const {
7575
initialDate,
7676
current,
@@ -198,14 +198,15 @@ const Calendar = (props: CalendarProps) => {
198198
}
199199

200200
const dateString = toMarkingFormat(day);
201+
const isControlled = isEmpty(props.context);
201202

202203
return (
203204
<View style={style.current.dayContainer} key={id}>
204205
<Day
205206
{...dayProps}
206207
testID={`${testID}.day_${dateString}`}
207208
date={dateString}
208-
state={getState(day, currentMonth, props)}
209+
state={getState(day, currentMonth, props, isControlled)}
209210
marking={markedDates?.[dateString]}
210211
onPress={_onDayPress}
211212
onLongPress={onLongPressDay}

src/expandableCalendar/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
583583
numberOfDays={numberOfDays}
584584
headerStyle={_headerStyle}
585585
timelineLeftInset={timelineLeftInset}
586+
context={useContext(Context)}
586587
/>
587588
);
588589
};

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import {ViewStyle, TextStyle} from 'react-native';
22
import {MarkingProps} from './calendar/day/marking';
3+
import {CalendarContextProps} from './expandableCalendar/Context';
34

5+
export type ContextProp = {
6+
context?: CalendarContextProps;
7+
}
48
export type MarkingTypes = 'dot' | 'multi-dot' | 'period' | 'multi-period' | 'custom';
59
export type MarkedDates = {
610
[key: string]: MarkingProps;

0 commit comments

Comments
 (0)