1- import React , { useMemo } from 'react' ;
2- import { View , Text , ViewStyle , TextStyle , Dimensions } from 'react-native' ;
1+ import React , { useCallback , useMemo , useRef } from 'react' ;
2+ import { View , Text , TouchableWithoutFeedback , ViewStyle , TextStyle , Dimensions , StyleSheet } from 'react-native' ;
33import range from 'lodash/range' ;
44import { HOUR_BLOCK_HEIGHT } from './Packer' ;
5+ import { buildTimeString , calcTimeByPosition } from './helpers/presenter' ;
56
67const { width : dimensionWidth } = Dimensions . get ( 'window' ) ;
78
8- interface TimelineHoursProps {
9+ interface NewEventTime {
10+ hour : number ;
11+ minutes : number ;
12+ }
13+
14+ export interface TimelineHoursProps {
915 start ?: number ;
1016 end ?: number ;
1117 format24h ?: boolean ;
18+ onBackgroundLongPress ?: ( timeString : string , time : { hour : number ; minutes : number } ) => void ;
19+ onBackgroundLongPressOut ?: ( timeString : string , time : { hour : number ; minutes : number } ) => void ;
1220 styles : { [ key : string ] : ViewStyle | TextStyle } ;
1321}
1422
1523const TimelineHours = ( props : TimelineHoursProps ) => {
16- const { format24h, start = 0 , end = 24 , styles} = props ;
24+ const { format24h, start = 0 , end = 24 , styles, onBackgroundLongPress, onBackgroundLongPressOut} = props ;
25+
26+ const lastLongPressEventTime = useRef < NewEventTime > ( ) ;
1727 // const offset = this.calendarHeight / (end - start);
1828 const offset = HOUR_BLOCK_HEIGHT ;
1929 const EVENT_DIFF = 20 ;
@@ -37,8 +47,33 @@ const TimelineHours = (props: TimelineHoursProps) => {
3747 } ) ;
3848 } , [ start , end , format24h ] ) ;
3949
50+ const handleBackgroundPress = useCallback (
51+ event => {
52+ const yPosition = event . nativeEvent . locationY ;
53+ const { hour, minutes} = calcTimeByPosition ( yPosition , HOUR_BLOCK_HEIGHT ) ;
54+
55+ lastLongPressEventTime . current = { hour, minutes} ;
56+
57+ const timeString = buildTimeString ( hour , minutes ) ;
58+ onBackgroundLongPress ?.( timeString , lastLongPressEventTime . current ) ;
59+ } ,
60+ [ onBackgroundLongPress ]
61+ ) ;
62+
63+ const handlePressOut = useCallback ( ( ) => {
64+ if ( lastLongPressEventTime . current ) {
65+ const { hour, minutes} = lastLongPressEventTime . current ;
66+ const timeString = buildTimeString ( hour , minutes ) ;
67+ onBackgroundLongPressOut ?.( timeString , lastLongPressEventTime . current ) ;
68+ lastLongPressEventTime . current = undefined ;
69+ }
70+ } , [ onBackgroundLongPressOut ] ) ;
71+
4072 return (
4173 < >
74+ < TouchableWithoutFeedback onLongPress = { handleBackgroundPress } onPressOut = { handlePressOut } >
75+ < View style = { StyleSheet . absoluteFillObject } />
76+ </ TouchableWithoutFeedback >
4277 { hours . map ( ( { timeText, time} , index ) => {
4378 return (
4479 < React . Fragment key = { time } >
0 commit comments