Skip to content

Commit c2f37ab

Browse files
authored
Merge pull request #1918 from wix/infra/CustomHeader_example
Calendar example - customHeader prop - usage
2 parents dd96aea + 4d4c5de commit c2f37ab

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

example/src/screens/calendarScreen.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState, Fragment, useCallback, useMemo} from 'react';
1+
import React, {useState, Fragment, useCallback, useMemo, useRef} from 'react';
22
import {StyleSheet, View, ScrollView, Text, TouchableOpacity} from 'react-native';
33
import {Calendar, CalendarProps} from 'react-native-calendars';
44
import testIDs from '../testIDs';
@@ -7,6 +7,7 @@ const INITIAL_DATE = '2020-02-02';
77

88
const CalendarScreen = () => {
99
const [selected, setSelected] = useState(INITIAL_DATE);
10+
const [currentMonth, setCurrentMonth] = useState(INITIAL_DATE);
1011

1112
const onDayPress: CalendarProps['onDayPress'] = useCallback(day => {
1213
setSelected(day.dateString);
@@ -415,14 +416,36 @@ const CalendarScreen = () => {
415416
);
416417
};
417418

419+
const customHeaderProps: any = useRef();
420+
421+
const setCustomHeaderNewMonth = (next = false) => {
422+
const add = next ? 1 : -1;
423+
const month = customHeaderProps?.current?.month;
424+
const newMonth = new Date(month.setMonth(month.getMonth() + add));
425+
customHeaderProps?.current?.addMonth(add);
426+
setCurrentMonth(newMonth.toISOString().split('T')[0]);
427+
};
428+
const moveNext = () => {
429+
setCustomHeaderNewMonth(true);
430+
};
431+
const movePrevious = () => {
432+
setCustomHeaderNewMonth(false);
433+
};
434+
418435
const renderCalendarWithCustomHeader = () => {
419436
const CustomHeader = React.forwardRef((props, ref) => {
437+
customHeaderProps.current = props;
438+
420439
return (
421440
// @ts-expect-error
422441
<View ref={ref} {...props} style={styles.customHeader}>
423-
<Text>This is a custom header!</Text>
424-
<TouchableOpacity onPress={() => console.warn('Tapped!')}>
425-
<Text>Tap Me</Text>
442+
<TouchableOpacity onPress={movePrevious}>
443+
<Text>Previous</Text>
444+
</TouchableOpacity>
445+
<Text>Custom header!</Text>
446+
<Text>{currentMonth}</Text>
447+
<TouchableOpacity onPress={moveNext}>
448+
<Text>Next</Text>
426449
</TouchableOpacity>
427450
</View>
428451
);
@@ -432,6 +455,7 @@ const CalendarScreen = () => {
432455
<Fragment>
433456
<Text style={styles.text}>Calendar with custom header component</Text>
434457
<Calendar
458+
initialDate={INITIAL_DATE}
435459
testID={testIDs.calendars.LAST}
436460
style={[styles.calendar, styles.customCalendar]}
437461
customHeader={CustomHeader}

0 commit comments

Comments
 (0)