@@ -16,13 +16,14 @@ import {
16
16
ImageSourcePropType ,
17
17
GestureResponderEvent ,
18
18
PanResponderGestureState ,
19
- TouchableOpacity
19
+ TouchableOpacity ,
20
+ type LayoutChangeEvent
20
21
} from 'react-native' ;
21
22
22
23
import { page } from '../dateutils' ;
23
24
import { parseDate , toMarkingFormat } from '../interface' ;
24
25
import { DateData , Direction } from '../types' ;
25
- import styleConstructor , { HEADER_HEIGHT , KNOB_CONTAINER_HEIGHT } from './style' ;
26
+ import styleConstructor , { KNOB_CONTAINER_HEIGHT } from './style' ;
26
27
import WeekDaysNames from '../commons/WeekDaysNames' ;
27
28
import Calendar from '../calendar' ;
28
29
import CalendarList , { CalendarListProps } from '../calendar-list' ;
@@ -38,7 +39,6 @@ export enum Positions {
38
39
}
39
40
const SPEED = 20 ;
40
41
const BOUNCINESS = 6 ;
41
- const CLOSED_HEIGHT = 120 ; // header + 1 week
42
42
const WEEK_HEIGHT = 46 ;
43
43
const DAY_NAMES_PADDING = 24 ;
44
44
const PAN_GESTURE_THRESHOLD = 30 ;
@@ -126,6 +126,10 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
126
126
} = props ;
127
127
128
128
const [ screenReaderEnabled , setScreenReaderEnabled ] = useState ( false ) ;
129
+ const [ headerHeight , setHeaderHeight ] = useState ( 0 ) ;
130
+ const onHeaderLayout = useCallback ( ( { nativeEvent : { layout : { height} } } : LayoutChangeEvent ) => {
131
+ setHeaderHeight ( height ) ;
132
+ } , [ ] ) ;
129
133
130
134
/** Date */
131
135
@@ -175,20 +179,24 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
175
179
if ( ! horizontal ) {
176
180
return Math . max ( constants . screenHeight , constants . screenWidth ) ;
177
181
}
178
- return CLOSED_HEIGHT + ( WEEK_HEIGHT * ( numberOfWeeks . current - 1 ) ) + ( hideKnob ? 12 : KNOB_CONTAINER_HEIGHT ) + ( constants . isAndroid ? 3 : 0 ) ;
182
+ return headerHeight + ( WEEK_HEIGHT * ( numberOfWeeks . current ) ) + ( hideKnob ? 0 : KNOB_CONTAINER_HEIGHT ) ;
179
183
} ;
180
184
const openHeight = useRef ( getOpenHeight ( ) ) ;
181
- const closedHeight = useMemo ( ( ) => CLOSED_HEIGHT + ( hideKnob || Number ( numberOfDays ) > 1 ? 0 : KNOB_CONTAINER_HEIGHT ) , [ numberOfDays , hideKnob ] ) ;
185
+ const closedHeight = useMemo ( ( ) => headerHeight + WEEK_HEIGHT + ( hideKnob || Number ( numberOfDays ) > 1 ? 0 : KNOB_CONTAINER_HEIGHT ) , [ numberOfDays , hideKnob , headerHeight ] ) ;
182
186
const startHeight = useMemo ( ( ) => isOpen ? openHeight . current : closedHeight , [ closedHeight , isOpen ] ) ;
183
187
const _height = useRef ( startHeight ) ;
184
188
const deltaY = useMemo ( ( ) => new Animated . Value ( startHeight ) , [ startHeight ] ) ;
185
- const headerDeltaY = useRef ( new Animated . Value ( isOpen ? - HEADER_HEIGHT : 0 ) ) ;
189
+ const headerDeltaY = useRef ( new Animated . Value ( isOpen ? - headerHeight : 0 ) ) ;
186
190
187
191
useEffect ( ( ) => {
188
192
_height . current = startHeight ;
189
193
deltaY . setValue ( startHeight ) ;
190
194
} , [ startHeight ] ) ;
191
195
196
+ useEffect ( ( ) => {
197
+ openHeight . current = getOpenHeight ( ) ;
198
+ } , [ headerHeight ] ) ;
199
+
192
200
193
201
useEffect ( ( ) => {
194
202
if ( numberOfDays ) {
@@ -209,7 +217,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
209
217
const themeObject = Object . assign ( headerStyleOverride , theme ) ;
210
218
211
219
const _wrapperStyles = useRef ( { style : { height : startHeight } } ) ;
212
- const _headerStyles = { style : { top : isOpen ? - HEADER_HEIGHT : 0 } } ;
220
+ const _headerStyles = { style : { top : isOpen ? - headerHeight : 0 } } ;
213
221
const _weekCalendarStyles = { style : { opacity : isOpen ? 0 : 1 } } ;
214
222
215
223
const shouldHideArrows = ! horizontal ? true : hideArrows || false ;
@@ -238,16 +246,16 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
238
246
} , [ calendarStyle ] ) ;
239
247
240
248
const animatedHeaderStyle = useMemo ( ( ) => {
241
- return [ style . current . header , { height : HEADER_HEIGHT + 10 , top : headerDeltaY . current } ] ;
242
- } , [ headerDeltaY . current ] ) ;
249
+ return [ style . current . header , { height : headerHeight , top : headerDeltaY . current } ] ;
250
+ } , [ headerDeltaY . current , headerHeight ] ) ;
243
251
244
252
const weekCalendarStyle = useMemo ( ( ) => {
245
- return [ style . current . weekContainer , isOpen ? style . current . hidden : style . current . visible ] ;
246
- } , [ isOpen ] ) ;
253
+ return [ style . current . weekContainer , isOpen ? style . current . hidden : style . current . visible , { top : headerHeight } ] ;
254
+ } , [ isOpen , headerHeight ] ) ;
247
255
248
256
const containerStyle = useMemo ( ( ) => {
249
- return [ allowShadow && style . current . containerShadow , propsStyle ] ;
250
- } , [ allowShadow , propsStyle ] ) ;
257
+ return [ allowShadow && style . current . containerShadow , propsStyle , headerHeight === 0 && style . current . hidden ] ;
258
+ } , [ allowShadow , propsStyle , headerHeight ] ) ;
251
259
252
260
const wrapperStyle = useMemo ( ( ) => {
253
261
return { height : deltaY } ;
@@ -343,7 +351,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
343
351
344
352
if ( ! horizontal ) {
345
353
// vertical CalenderList header
346
- _headerStyles . style . top = Math . min ( Math . max ( - gestureState . dy , - HEADER_HEIGHT ) , 0 ) ;
354
+ _headerStyles . style . top = Math . min ( Math . max ( - gestureState . dy , - headerHeight ) , 0 ) ;
347
355
} else {
348
356
// horizontal Week view
349
357
if ( ! isOpen ) {
@@ -370,7 +378,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
370
378
onPanResponderMove : handlePanResponderMove ,
371
379
onPanResponderRelease : handlePanResponderEnd ,
372
380
onPanResponderTerminate : handlePanResponderEnd
373
- } ) : PanResponder . create ( { } ) , [ numberOfDays , position ] ) ;
381
+ } ) : PanResponder . create ( { } ) , [ numberOfDays , position , headerHeight ] ) ; // All the functions here rely on headerHeight directly or indirectly through refs that are updated in useEffect
374
382
375
383
/** Animated */
376
384
@@ -572,6 +580,7 @@ const ExpandableCalendar = (props: ExpandableCalendarProps) => {
572
580
horizontal = { horizontal }
573
581
firstDay = { firstDay }
574
582
calendarStyle = { calendarStyle }
583
+ onHeaderLayout = { onHeaderLayout }
575
584
{ ...others }
576
585
current = { date }
577
586
theme = { themeObject }
0 commit comments