Skip to content

Commit 9dc3ee3

Browse files
improvements
1 parent db3c867 commit 9dc3ee3

File tree

3 files changed

+114
-76
lines changed

3 files changed

+114
-76
lines changed

example/src/screens/newCalendarListScreen.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import React, {useMemo, useState, useCallback} from 'react';
2-
import {StyleSheet, View, Switch, Text} from 'react-native';
3-
import {NewCalendarList} from 'react-native-calendars';
1+
import React, { useMemo, useState, useCallback } from 'react';
2+
import { StyleSheet, View, Switch, Text } from 'react-native';
3+
import { NewCalendarList } from 'react-native-calendars';
44
import testIDs from '../testIDs';
55

66
const initialDate = '2020-05-16';
77

88
const NewCalendarListScreen = () => {
99
const [selected, setSelected] = useState(initialDate);
1010
const [isHorizontal, setIsHorizontal] = useState(false);
11-
11+
1212
const onValueChange = useCallback((value) => {
1313
setIsHorizontal(value);
1414
}, [isHorizontal]);
@@ -38,14 +38,15 @@ const NewCalendarListScreen = () => {
3838
<View style={styles.container}>
3939
<View style={styles.switchView}>
4040
<Text style={styles.switchText}>Horizontal</Text>
41-
<Switch value={isHorizontal} onValueChange={onValueChange}/>
41+
<Switch value={isHorizontal} onValueChange={onValueChange} />
4242
</View>
4343
<NewCalendarList
4444
key={Number(isHorizontal)} // only for this example - to force rerender
4545
horizontal={isHorizontal}
46-
staticHeader
46+
numberOfPages={2}
47+
staticHeader
4748
// initialDate={initialDate}
48-
// scrollRange={10}
49+
scrollRange={2}
4950
calendarProps={calendarProps}
5051
testID={testIDs.horizontalList.CONTAINER}
5152
/>
@@ -60,7 +61,7 @@ const styles = StyleSheet.create({
6061
flex: 1
6162
},
6263
switchView: {
63-
flexDirection: 'row',
64+
flexDirection: 'row',
6465
height: 70,
6566
padding: 10,
6667
paddingBottom: 30,
@@ -74,7 +75,7 @@ const styles = StyleSheet.create({
7475
zIndex: 100
7576
},
7677
switchText: {
77-
marginRight: 20,
78+
marginRight: 20,
7879
fontSize: 16
7980
}
8081
});

src/calendar-list/new.tsx

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import XDate from 'xdate';
2-
import React, {useCallback, useEffect, useRef, useState, useMemo} from 'react';
3-
import {View, ScrollViewProps, ScrollView} from 'react-native';
2+
import React, { useCallback, useEffect, useRef, useState, useMemo } from 'react';
3+
import { View, ScrollViewProps, ScrollView } from 'react-native';
44
import constants from '../commons/constants';
5-
import {toMarkingFormat} from '../interface';
6-
import {extractHeaderProps} from '../componentUpdater';
7-
import Calendar, {CalendarProps} from '../calendar';
5+
import { toMarkingFormat } from '../interface';
6+
import { extractHeaderProps } from '../componentUpdater';
7+
import Calendar, { CalendarProps } from '../calendar';
88
import CalendarHeader from '../calendar/header';
99
import InfiniteList from '../infinite-list';
1010
import styleConstructor from './style';
@@ -24,6 +24,9 @@ export interface CalendarListProps {
2424
calendarProps?: CalendarProps;
2525
/** Identifier for testing */
2626
testID?: string;
27+
calendarHeight?: number;
28+
numberOfPages?: number;
29+
2730
}
2831

2932
const NUMBER_OF_PAGES = 50;
@@ -32,9 +35,9 @@ const CALENDAR_HEIGHT = 360;
3235
const CalendarList = (props: CalendarListProps) => {
3336
const {
3437
initialDate,
35-
horizontal,
38+
horizontal,
3639
scrollRange = NUMBER_OF_PAGES,
37-
staticHeader,
40+
staticHeader,
3841
scrollViewProps,
3942
calendarProps,
4043
testID
@@ -45,10 +48,9 @@ const CalendarList = (props: CalendarListProps) => {
4548
const [positionIndex, setPositionIndex] = useState(scrollRange);
4649

4750
/** Static Header */
48-
4951
const [currentMonth, setCurrentMonth] = useState(initialDate || items[scrollRange]);
5052
const shouldRenderStaticHeader = staticHeader && horizontal;
51-
const headerProps = extractHeaderProps(props);
53+
const headerProps = extractHeaderProps(calendarProps || {});
5254
const staticHeaderStyle = useMemo(() => {
5355
return [style.current.staticHeader, calendarProps?.headerStyle];
5456
}, [calendarProps?.headerStyle]);
@@ -102,7 +104,7 @@ const CalendarList = (props: CalendarListProps) => {
102104
}
103105
}, [updateMonth]);
104106

105-
const onPageChange = useCallback((pageIndex: number, _: number, info: {scrolledByUser: boolean}) => {
107+
const onPageChange = useCallback((pageIndex: number, _: number, info: { scrolledByUser: boolean }) => {
106108
if (shouldRenderStaticHeader && info.scrolledByUser) {
107109
setCurrentMonth(items[pageIndex]);
108110
}
@@ -143,7 +145,7 @@ const CalendarList = (props: CalendarListProps) => {
143145
const array: string[] = [...items];
144146
const startingDate = items[index];
145147
const shouldAppend = index > scrollRange;
146-
148+
147149
if (startingDate) {
148150
if (shouldAppend) {
149151
for (let i = 2; i <= scrollRange; i++) {
@@ -165,7 +167,7 @@ const CalendarList = (props: CalendarListProps) => {
165167
/** List */
166168

167169
const listContainerStyle = useMemo(() => {
168-
return [style.current.flatListContainer, {flex: horizontal ? undefined : 1}];
170+
return [style.current.flatListContainer, { flex: horizontal ? undefined : 1 }];
169171
}, [style, horizontal]);
170172

171173
const scrollProps = useMemo(() => {
@@ -180,17 +182,16 @@ const CalendarList = (props: CalendarListProps) => {
180182
return (
181183
<Calendar
182184
{...calendarProps}
183-
{...headerProps}
184185
initialDate={item}
185186
disableMonthChange
186187
hideArrows={!horizontal}
187188
onPressArrowRight={scrollToNextMonth}
188-
onPressArrowLeft={scrollToPreviousMonth}
189+
onPressArrowLeft={scrollToPreviousMonth}
189190
hideExtraDays={calendarProps?.hideExtraDays || true}
190191
style={[style.current.calendar, calendarProps?.style]}
191192
headerStyle={horizontal ? calendarProps?.headerStyle : undefined}
192-
testID={`${testID}_${item}`}
193-
// context={context}
193+
testID={`${ testID }_${ item }`}
194+
// context={context}
194195
/>
195196
);
196197
}, [calendarProps, scrollToNextMonth, scrollToPreviousMonth]);
@@ -203,13 +204,13 @@ const CalendarList = (props: CalendarListProps) => {
203204
data={items}
204205
renderItem={renderItem}
205206
reloadPages={reloadPages}
206-
onReachNearEdgeThreshold={Math.round(NUMBER_OF_PAGES * 0.4)}
207+
onReachNearEdgeThreshold={props.numberOfPages ?? Math.round(NUMBER_OF_PAGES * 0.4)}
207208
extendedState={calendarProps?.markedDates}
208209
isHorizontal={horizontal}
209210
style={style.current.container}
210211
initialPageIndex={scrollRange}
211212
positionIndex={positionIndex}
212-
pageHeight={CALENDAR_HEIGHT}
213+
pageHeight={props.calendarHeight ?? CALENDAR_HEIGHT}
213214
pageWidth={constants.screenWidth}
214215
onPageChange={onPageChange}
215216
scrollViewProps={scrollProps}
@@ -224,10 +225,10 @@ export default CalendarList;
224225
function getDate(date: string, index: number) {
225226
const d = new XDate(date);
226227
d.addMonths(index, true);
227-
228+
228229
// if (index !== 0) {
229-
d.setDate(1);
230-
// }
230+
d.setDate(1);
231+
// }
231232
return toMarkingFormat(d);
232233
}
233234

0 commit comments

Comments
 (0)