Skip to content

Commit 48f9a2b

Browse files
committed
Support initialTime for other days that are not "today"
1 parent 4109d77 commit 48f9a2b

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

example/src/screens/timelineCalendar.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from 'react-native-calendars';
1111
import _ from 'lodash';
1212

13+
const INITIAL_TIME = {hour: 9, minutes: 0};
1314
const today = new Date();
1415
const getDate = (offset = 0) => CalendarUtils.getCalendarDateString(new Date().setDate(today.getDate() + offset));
1516

@@ -207,7 +208,13 @@ export default class TimelineCalendarScreen extends Component {
207208
rightArrowImageSource={require('../img/next.png')}
208209
markedDates={this.marked}
209210
/>
210-
<TimelineList events={eventsByDate} timelineProps={this.timelineProps} showNowIndicator scrollToNow />
211+
<TimelineList
212+
events={eventsByDate}
213+
timelineProps={this.timelineProps}
214+
showNowIndicator
215+
scrollToNow
216+
initialTime={INITIAL_TIME}
217+
/>
211218
</CalendarProvider>
212219
);
213220
}

src/timeline-list/index.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,30 @@ import InfiniteList from '../infinite-list';
1111
import useTimelinePages, {INITIAL_PAGE, NEAR_EDGE_THRESHOLD} from './useTimelinePages';
1212

1313
export interface TimelineListProps {
14+
/**
15+
* Map of all timeline events ({[date]: events})
16+
*/
1417
events: {[date: string]: TimelineProps['events']};
15-
timelineProps?: Omit<TimelineProps, 'events' | 'showNowIndicator' | 'scrollToNow'>;
18+
/**
19+
* General timeline props to pass to each timeline item
20+
*/
21+
timelineProps?: Omit<TimelineProps, 'events' | 'showNowIndicator' | 'scrollToNow' | 'initialTime'>;
22+
/**
23+
* Should show now indicator (shown only on "today" timeline)
24+
*/
1625
showNowIndicator?: boolean;
26+
/**
27+
* Should initially scroll to current time (relevant only for "today" timeline)
28+
*/
1729
scrollToNow?: boolean;
30+
/**
31+
* Should initially scroll to a specific time (relevant only for NOT "today" timelines)
32+
*/
33+
initialTime?: TimelineProps['initialTime'];
1834
}
1935

2036
const TimelineList = (props: TimelineListProps) => {
21-
const {timelineProps, events, showNowIndicator, scrollToNow} = props;
37+
const {timelineProps, events, showNowIndicator, scrollToNow, initialTime} = props;
2238
const {date, updateSource, setDate} = useContext(Context);
2339
const listRef = useRef<any>();
2440
const prevDate = useRef(date);
@@ -77,18 +93,22 @@ const TimelineList = (props: TimelineListProps) => {
7793
(_type, item, index) => {
7894
const timelineEvent = events[item];
7995
const isCurrent = prevDate.current === item;
96+
const isInitialPage = index === INITIAL_PAGE;
97+
const _isToday = isToday(new XDate(item));
98+
8099
return (
81100
<>
82101
<Timeline
83102
{...timelineProps}
84103
key={item}
85104
date={item}
86-
scrollToNow={scrollToNow && index === INITIAL_PAGE}
87-
scrollToFirst={false}
88105
events={timelineEvent}
106+
scrollToFirst={false}
107+
scrollToNow={_isToday && isInitialPage && scrollToNow}
108+
initialTime={!_isToday && isInitialPage ? initialTime : undefined}
89109
scrollOffset={isCurrent ? undefined : timelineOffset}
90110
onChangeOffset={onTimelineOffsetChange}
91-
showNowIndicator={showNowIndicator && isToday(new XDate(item))}
111+
showNowIndicator={_isToday && showNowIndicator}
92112
/>
93113
{/* NOTE: Keeping this for easy debugging */}
94114
{/* <Text style={{position: 'absolute'}}>{item}</Text> */}

src/timeline/Timeline.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export interface TimelineProps {
5959
* Should scroll to current time when loaded
6060
*/
6161
scrollToNow?: boolean;
62+
/**
63+
* Initial time to scroll to
64+
*/
65+
initialTime?: {hour: number; minutes: number};
6266
/**
6367
* Whether to use 24 hours format for the timeline hours
6468
*/
@@ -95,6 +99,7 @@ const Timeline = (props: TimelineProps) => {
9599
theme,
96100
scrollToFirst,
97101
scrollToNow,
102+
initialTime,
98103
showNowIndicator,
99104
scrollOffset,
100105
onChangeOffset,
@@ -118,6 +123,8 @@ const Timeline = (props: TimelineProps) => {
118123
initialPosition = min(map(packedEvents, 'top')) ?? 0;
119124
} else if (scrollToNow) {
120125
initialPosition = calcNowOffset(HOUR_BLOCK_HEIGHT);
126+
} else if (initialTime) {
127+
initialPosition = calcNowOffset(HOUR_BLOCK_HEIGHT, initialTime.hour, initialTime.minutes);
121128
}
122129

123130
if (initialPosition) {

0 commit comments

Comments
 (0)