@@ -5,7 +5,16 @@ import throttle from 'lodash/throttle';
55
66import XDate from 'xdate' ;
77
8- import React , { useContext , useRef , useState , useEffect , useCallback , useMemo } from 'react' ;
8+ import React , {
9+ useContext ,
10+ useRef ,
11+ useState ,
12+ useEffect ,
13+ useCallback ,
14+ useMemo ,
15+ useImperativeHandle ,
16+ forwardRef
17+ } from 'react' ;
918import {
1019 AccessibilityInfo ,
1120 PanResponder ,
@@ -71,6 +80,10 @@ export interface ExpandableCalendarProps extends CalendarListProps {
7180 closeOnDayPress ?: boolean ;
7281}
7382
83+ type ExpandableCalendarRef = {
84+ toggleCalendarPosition : ( ) => boolean ;
85+ } ;
86+
7487const headerStyleOverride = {
7588 stylesheet : {
7689 calendar : {
@@ -94,7 +107,7 @@ const headerStyleOverride = {
94107 * @example : https://github.com/wix/react-native-calendars/blob/master/example/src/screens/expandableCalendar.js
95108 */
96109
97- const ExpandableCalendar = ( props : ExpandableCalendarProps ) => {
110+ const ExpandableCalendar = forwardRef < ExpandableCalendarRef , ExpandableCalendarProps > ( ( props , ref ) => {
98111 const _context = useContext ( Context ) ;
99112 const { date, setDate, numberOfDays, timelineLeftInset} = _context ;
100113
@@ -370,7 +383,6 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
370383 _height . current = Number ( _wrapperStyles . current . style . height ) ;
371384 bounceToPosition ( ) ;
372385 } ;
373-
374386 const numberOfDaysCondition = useMemo ( ( ) => {
375387 return ! numberOfDays || numberOfDays && numberOfDays <= 1 ;
376388 } , [ numberOfDays ] ) ;
@@ -437,8 +449,17 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
437449
438450 const toggleCalendarPosition = useCallback ( ( ) => {
439451 bounceToPosition ( isOpen ? closedHeight : openHeight . current ) ;
452+ return ! isOpen ;
440453 } , [ isOpen , bounceToPosition , closedHeight ] ) ;
441454
455+ useImperativeHandle (
456+ ref ,
457+ ( ) => ( {
458+ toggleCalendarPosition
459+ } ) ,
460+ [ toggleCalendarPosition ]
461+ ) ;
462+
442463 /** Events */
443464
444465 const _onPressArrowLeft = useCallback ( ( method : ( ) => void , month ?: XDate ) => {
@@ -626,20 +647,20 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
626647 ) }
627648 </ View >
628649 ) ;
629- } ;
630-
631- export default ExpandableCalendar ;
632-
633- ExpandableCalendar . displayName = 'ExpandableCalendar' ;
634- ExpandableCalendar . defaultProps = {
635- horizontal : true ,
636- initialPosition : Positions . CLOSED ,
637- firstDay : 0 ,
638- leftArrowImageSource : LEFT_ARROW ,
639- rightArrowImageSource : RIGHT_ARROW ,
640- allowShadow : true ,
641- openThreshold : PAN_GESTURE_THRESHOLD ,
642- closeThreshold : PAN_GESTURE_THRESHOLD ,
643- closeOnDayPress : true
644- } ;
645- ExpandableCalendar . positions = Positions ;
650+ } ) ;
651+
652+ export default Object . assign ( ExpandableCalendar , {
653+ displayName : 'ExpandableCalendar' ,
654+ positions : Positions ,
655+ defaultProps : {
656+ horizontal : true ,
657+ initialPosition : Positions . CLOSED ,
658+ firstDay : 0 ,
659+ leftArrowImageSource : LEFT_ARROW ,
660+ rightArrowImageSource : RIGHT_ARROW ,
661+ allowShadow : true ,
662+ openThreshold : PAN_GESTURE_THRESHOLD ,
663+ closeThreshold : PAN_GESTURE_THRESHOLD ,
664+ closeOnDayPress : true
665+ }
666+ } ) ;
0 commit comments