Skip to content

Commit 3f12474

Browse files
authored
Merge pull request #1740 from wix/feat/TimelineDateProp
Support passing date to Timeline
2 parents 3cdc905 + a68ca10 commit 3f12474

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

example/src/screens/timelineCalendar.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,12 @@ export default class TimelineCalendarScreen extends Component {
103103
};
104104

105105
createNewEvent: TimelineProps['onBackgroundLongPress'] = (timeString, timeObject) => {
106-
const {currentDate} = this.state;
107106
const hourString = `${(timeObject.hour + 1).toString().padStart(2, '0')}`;
108107
const minutesString = `${timeObject.minutes.toString().padStart(2, '0')}`;
109108

110109
const newEvent = {
111-
start: `${currentDate} ${timeString}`,
112-
end: `${currentDate} ${hourString}:${minutesString}:00`,
110+
start: `${timeString}`,
111+
end: `${timeObject.date} ${hourString}:${minutesString}:00`,
113112
title: 'New Event',
114113
color: '#ffffff'
115114
};
@@ -141,11 +140,11 @@ export default class TimelineCalendarScreen extends Component {
141140
};
142141

143142
render() {
144-
const {events, newEvent} = this.state;
143+
const {currentDate, events, newEvent} = this.state;
145144
const timelineEvents = newEvent ? [...events, newEvent] : events;
146145
return (
147146
<CalendarProvider
148-
date={this.state.currentDate}
147+
date={currentDate}
149148
onDateChanged={this.onDateChanged}
150149
onMonthChange={this.onMonthChange}
151150
showTodayButton
@@ -158,6 +157,7 @@ export default class TimelineCalendarScreen extends Component {
158157
markedDates={this.marked}
159158
/>
160159
<Timeline
160+
date={currentDate}
161161
format24h={true}
162162
eventTapped={e => e}
163163
events={timelineEvents.filter(event => sameDate(new XDate(event.start), new XDate(this.state.currentDate)))}

src/timeline/Timeline.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import NowIndicator from './NowIndicator';
1313
const {width: dimensionWidth} = Dimensions.get('window');
1414

1515
export interface TimelineProps {
16+
/**
17+
* The date of this timeline instance in ISO format (e.g. 2011-10-25)
18+
*/
19+
date?: string;
20+
/**
21+
* List of events to display in this timeline
22+
*/
1623
events: Event[];
1724
/**
1825
* The timeline day start time
@@ -33,10 +40,12 @@ export interface TimelineProps {
3340
onEventPress?: (event: Event) => void;
3441
/**
3542
* Pass to handle creation of a new event by long press on the timeline background
43+
* NOTE: If passed, the date prop will be included in the returned time string (e.g. 2017-09-06 01:30:00)
3644
*/
3745
onBackgroundLongPress?: TimelineHoursProps['onBackgroundLongPress'];
3846
/**
3947
* Pass to handle creation of a new event by long press out on the timeline background
48+
* NOTE: If passed, the date prop will be included in the returned time string (e.g. 2017-09-06 01:30:00)
4049
*/
4150
onBackgroundLongPressOut?: TimelineHoursProps['onBackgroundLongPressOut'];
4251
styles?: Theme; //TODO: deprecate (prop renamed 'theme', as in the other components).
@@ -61,6 +70,7 @@ const Timeline = (props: TimelineProps) => {
6170
format24h = true,
6271
start = 0,
6372
end = 24,
73+
date,
6474
events = [],
6575
onEventPress,
6676
onBackgroundLongPress,
@@ -139,6 +149,7 @@ const Timeline = (props: TimelineProps) => {
139149
<TimelineHours
140150
start={start}
141151
end={end}
152+
date={date}
142153
format24h={format24h}
143154
styles={styles.current}
144155
onBackgroundLongPress={onBackgroundLongPress}

src/timeline/TimelineHours.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ const {width: dimensionWidth} = Dimensions.get('window');
99
interface NewEventTime {
1010
hour: number;
1111
minutes: number;
12+
date?: string;
1213
}
1314

1415
export interface TimelineHoursProps {
1516
start?: number;
1617
end?: number;
18+
date?: string;
1719
format24h?: boolean;
18-
onBackgroundLongPress?: (timeString: string, time: {hour: number; minutes: number}) => void;
19-
onBackgroundLongPressOut?: (timeString: string, time: {hour: number; minutes: number}) => void;
20+
onBackgroundLongPress?: (timeString: string, time: NewEventTime) => void;
21+
onBackgroundLongPressOut?: (timeString: string, time: NewEventTime) => void;
2022
styles: {[key: string]: ViewStyle | TextStyle};
2123
}
2224

2325
const TimelineHours = (props: TimelineHoursProps) => {
24-
const {format24h, start = 0, end = 24, styles, onBackgroundLongPress, onBackgroundLongPressOut} = props;
26+
const {format24h, start = 0, end = 24, date, styles, onBackgroundLongPress, onBackgroundLongPressOut} = props;
2527

2628
const lastLongPressEventTime = useRef<NewEventTime>();
2729
// const offset = this.calendarHeight / (end - start);
@@ -52,22 +54,22 @@ const TimelineHours = (props: TimelineHoursProps) => {
5254
const yPosition = event.nativeEvent.locationY;
5355
const {hour, minutes} = calcTimeByPosition(yPosition, HOUR_BLOCK_HEIGHT);
5456

55-
lastLongPressEventTime.current = {hour, minutes};
57+
lastLongPressEventTime.current = {hour, minutes, date};
5658

57-
const timeString = buildTimeString(hour, minutes);
59+
const timeString = buildTimeString(hour, minutes, date);
5860
onBackgroundLongPress?.(timeString, lastLongPressEventTime.current);
5961
},
60-
[onBackgroundLongPress]
62+
[onBackgroundLongPress, date]
6163
);
6264

6365
const handlePressOut = useCallback(() => {
6466
if (lastLongPressEventTime.current) {
65-
const {hour, minutes} = lastLongPressEventTime.current;
66-
const timeString = buildTimeString(hour, minutes);
67+
const {hour, minutes, date} = lastLongPressEventTime.current;
68+
const timeString = buildTimeString(hour, minutes, date);
6769
onBackgroundLongPressOut?.(timeString, lastLongPressEventTime.current);
6870
lastLongPressEventTime.current = undefined;
6971
}
70-
}, [onBackgroundLongPressOut]);
72+
}, [onBackgroundLongPressOut, date]);
7173

7274
return (
7375
<>

src/timeline/__tests__/presenter.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,10 @@ describe('timeline presenter', () => {
6262
expect(uut.buildTimeString(undefined, 30)).toBe('00:30:00');
6363
expect(uut.buildTimeString(9, undefined)).toBe('09:00:00');
6464
});
65+
66+
it('should concatenate date when passed', () => {
67+
expect(uut.buildTimeString(10, 30, '2022-12-23')).toBe('2022-12-23 10:30:00');
68+
expect(uut.buildTimeString(15, 0, '2017-03-05')).toBe('2017-03-05 15:00:00');
69+
});
6570
});
6671
});

src/timeline/helpers/presenter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export function calcTimeByPosition(yPosition: number, hourBlockHeight: number) {
77
return {hour, minutes};
88
}
99

10-
export function buildTimeString(hour = 0, minutes = 0) {
11-
return `${hour.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:00`;
10+
export function buildTimeString(hour = 0, minutes = 0, date = '') {
11+
return `${date} ${hour.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:00`.trimStart();
1212
}

0 commit comments

Comments
 (0)