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 ) ;
@@ -138,12 +139,16 @@ const CalendarScreen = () => {
138139 selectedDayBackgroundColor : '#333248' ,
139140 arrowColor : 'white' ,
140141 // textDisabledColor: 'red',
141- 'stylesheet.calendar.header' : {
142- week : {
143- marginTop : 30 ,
144- marginHorizontal : 12 ,
145- flexDirection : 'row' ,
146- justifyContent : 'space-between'
142+ stylesheet : {
143+ calendar : {
144+ header : {
145+ week : {
146+ marginTop : 30 ,
147+ marginHorizontal : 12 ,
148+ flexDirection : 'row' ,
149+ justifyContent : 'space-between'
150+ }
151+ }
147152 }
148153 }
149154 } }
@@ -411,14 +416,36 @@ const CalendarScreen = () => {
411416 ) ;
412417 } ;
413418
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+
414435 const renderCalendarWithCustomHeader = ( ) => {
415436 const CustomHeader = React . forwardRef ( ( props , ref ) => {
437+ customHeaderProps . current = props ;
438+
416439 return (
417440 // @ts -expect-error
418441 < View ref = { ref } { ...props } style = { styles . customHeader } >
419- < Text > This is a custom header!</ Text >
420- < TouchableOpacity onPress = { ( ) => console . warn ( 'Tapped!' ) } >
421- < 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 >
422449 </ TouchableOpacity >
423450 </ View >
424451 ) ;
@@ -428,6 +455,7 @@ const CalendarScreen = () => {
428455 < Fragment >
429456 < Text style = { styles . text } > Calendar with custom header component</ Text >
430457 < Calendar
458+ initialDate = { INITIAL_DATE }
431459 testID = { testIDs . calendars . LAST }
432460 style = { [ styles . calendar , styles . customCalendar ] }
433461 customHeader = { CustomHeader }
0 commit comments