Skip to content

Commit 250c7fb

Browse files
committed
Support custom renderItem in TimelineList component
1 parent 7537375 commit 250c7fb

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export {default as asCalendarConsumer} from './expandableCalendar/asCalendarCons
1717
export {default as Timeline} from './timeline/Timeline';
1818
export type {TimelineProps, TimelineEventProps, TimelinePackedEventProps} from './timeline/Timeline';
1919
export {default as TimelineList} from './timeline-list';
20+
export {TimelineListProps, TimelineListRenderItemInfo} from './timeline-list';
2021
export {default as CalendarUtils} from './services';
2122
export type {DateData, AgendaEntry, AgendaSchedule} from './types';
2223
export {default as LocaleConfig} from 'xdate';

src/timeline-list/index.tsx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import Timeline, {TimelineProps} from '../timeline/Timeline';
1010
import InfiniteList from '../infinite-list';
1111
import useTimelinePages, {INITIAL_PAGE, NEAR_EDGE_THRESHOLD} from './useTimelinePages';
1212

13+
export interface TimelineListRenderItemInfo {
14+
item: string;
15+
index: number;
16+
isCurrent: boolean;
17+
isInitialPage: boolean;
18+
isToday: boolean;
19+
}
20+
1321
export interface TimelineListProps {
1422
/**
1523
* Map of all timeline events ({[date]: events})
@@ -19,6 +27,10 @@ export interface TimelineListProps {
1927
* General timeline props to pass to each timeline item
2028
*/
2129
timelineProps?: Omit<TimelineProps, 'events' | 'scrollToFirst' | 'showNowIndicator' | 'scrollToNow' | 'initialTime'>;
30+
/**
31+
* Pass to render a custom Timeline item
32+
*/
33+
renderItem?: (timelineProps: TimelineProps, info: TimelineListRenderItemInfo) => JSX.Element;
2234
/**
2335
* Should scroll to first event of the day
2436
*/
@@ -38,7 +50,7 @@ export interface TimelineListProps {
3850
}
3951

4052
const TimelineList = (props: TimelineListProps) => {
41-
const {timelineProps, events, showNowIndicator, scrollToFirst, scrollToNow, initialTime} = props;
53+
const {timelineProps, events, renderItem, showNowIndicator, scrollToFirst, scrollToNow, initialTime} = props;
4254
const {date, updateSource, setDate} = useContext(Context);
4355
const listRef = useRef<any>();
4456
const prevDate = useRef(date);
@@ -100,20 +112,26 @@ const TimelineList = (props: TimelineListProps) => {
100112
const isInitialPage = index === INITIAL_PAGE;
101113
const _isToday = isToday(new XDate(item));
102114

115+
const _timelineProps = {
116+
...timelineProps,
117+
key: item,
118+
date: item,
119+
events: timelineEvent,
120+
scrollToNow: _isToday && isInitialPage && scrollToNow,
121+
initialTime: !_isToday && isInitialPage ? initialTime : undefined,
122+
scrollToFirst: !_isToday && isInitialPage && scrollToFirst,
123+
scrollOffset: isCurrent ? undefined : timelineOffset,
124+
onChangeOffset: onTimelineOffsetChange,
125+
showNowIndicator: _isToday && showNowIndicator
126+
};
127+
128+
if (renderItem) {
129+
return renderItem(_timelineProps, {item, index, isCurrent, isInitialPage, isToday: _isToday});
130+
}
131+
103132
return (
104133
<>
105-
<Timeline
106-
{...timelineProps}
107-
key={item}
108-
date={item}
109-
events={timelineEvent}
110-
scrollToNow={_isToday && isInitialPage && scrollToNow}
111-
initialTime={!_isToday && isInitialPage ? initialTime : undefined}
112-
scrollToFirst={!_isToday && isInitialPage && scrollToFirst}
113-
scrollOffset={isCurrent ? undefined : timelineOffset}
114-
onChangeOffset={onTimelineOffsetChange}
115-
showNowIndicator={_isToday && showNowIndicator}
116-
/>
134+
<Timeline {..._timelineProps} />
117135
{/* NOTE: Keeping this for easy debugging */}
118136
{/* <Text style={{position: 'absolute'}}>{item}</Text> */}
119137
</>

0 commit comments

Comments
 (0)