1- import React , { useState , Fragment , useCallback , useMemo } from 'react' ;
1+ import React , { useState , Fragment , useCallback , useMemo , useRef } from 'react' ;
22import { StyleSheet , View , ScrollView , Text , TouchableOpacity } from 'react-native' ;
33import { Calendar , CalendarProps } from 'react-native-calendars' ;
44import testIDs from '../testIDs' ;
@@ -7,6 +7,7 @@ const INITIAL_DATE = '2020-02-02';
77
88const 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 = { currentMonth }
435459 testID = { testIDs . calendars . LAST }
436460 style = { [ styles . calendar , styles . customCalendar ] }
437461 customHeader = { CustomHeader }
0 commit comments