Skip to content

Commit 3cdc905

Browse files
authored
Merge pull request #1739 from wix/feat/TimelineNowIndicator
Support Timeline now indicator and fix UI for timeline grid
2 parents 57aa6fb + a37a5f1 commit 3cdc905

File tree

6 files changed

+72
-11
lines changed

6 files changed

+72
-11
lines changed

example/src/screens/timelineCalendar.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export default class TimelineCalendarScreen extends Component {
164164
scrollToFirst
165165
onBackgroundLongPress={this.createNewEvent}
166166
onBackgroundLongPressOut={this.approveNewEvent}
167+
showNowIndicator
167168
// start={0}
168169
// end={24}
169170
/>

src/timeline/NowIndicator.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import {View, TextStyle, ViewStyle} from 'react-native';
3+
import {HOUR_BLOCK_HEIGHT} from './Packer';
4+
5+
export {HOUR_BLOCK_HEIGHT} from './Packer';
6+
7+
export interface NowIndicatorProps {
8+
styles: {[key: string]: ViewStyle | TextStyle};
9+
}
10+
11+
const NowIndicator = (props: NowIndicatorProps) => {
12+
const {styles} = props;
13+
const now = new Date();
14+
const hour = now.getHours();
15+
const minutes = now.getMinutes();
16+
17+
const indicatorPosition = (hour + minutes / 60) * HOUR_BLOCK_HEIGHT;
18+
19+
return (
20+
<View style={[styles.nowIndicator, {top: indicatorPosition}]}>
21+
<View style={styles.nowIndicatorLine} />
22+
<View style={styles.nowIndicatorKnob} />
23+
</View>
24+
);
25+
};
26+
27+
export default NowIndicator;

src/timeline/Timeline.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import min from 'lodash/min';
44
import map from 'lodash/map';
55

66
import {Theme} from '../types';
7-
import styleConstructor from './style';
7+
import styleConstructor, {HOURS_SIDEBAR_WIDTH} from './style';
88
import populateEvents, {HOUR_BLOCK_HEIGHT} from './Packer';
99
import TimelineHours, {TimelineHoursProps} from './TimelineHours';
1010
import EventBlock, {Event, PackedEvent} from './EventBlock';
11+
import NowIndicator from './NowIndicator';
1112

12-
const LEFT_MARGIN = 60 - 1;
1313
const {width: dimensionWidth} = Dimensions.get('window');
1414

1515
export interface TimelineProps {
@@ -50,6 +50,10 @@ export interface TimelineProps {
5050
* Render a custom event block
5151
*/
5252
renderEvent?: (event: PackedEvent) => JSX.Element;
53+
/**
54+
* Whether to show now indicator
55+
*/
56+
showNowIndicator?: boolean;
5357
}
5458

5559
const Timeline = (props: TimelineProps) => {
@@ -64,6 +68,7 @@ const Timeline = (props: TimelineProps) => {
6468
renderEvent,
6569
theme,
6670
scrollToFirst,
71+
showNowIndicator,
6772
eventTapped
6873
} = props;
6974

@@ -72,7 +77,7 @@ const Timeline = (props: TimelineProps) => {
7277
const styles = useRef(styleConstructor(theme || props.styles, calendarHeight.current));
7378

7479
const packedEvents = useMemo(() => {
75-
const width = dimensionWidth - LEFT_MARGIN;
80+
const width = dimensionWidth - HOURS_SIDEBAR_WIDTH;
7681
return populateEvents(events, width, start);
7782
}, [events, start]);
7883

@@ -123,7 +128,7 @@ const Timeline = (props: TimelineProps) => {
123128

124129
return (
125130
<View>
126-
<View style={{marginLeft: LEFT_MARGIN}}>{events}</View>
131+
<View style={{marginLeft: HOURS_SIDEBAR_WIDTH}}>{events}</View>
127132
</View>
128133
);
129134
};
@@ -140,6 +145,7 @@ const Timeline = (props: TimelineProps) => {
140145
onBackgroundLongPressOut={onBackgroundLongPressOut}
141146
/>
142147
{renderEvents()}
148+
{showNowIndicator && <NowIndicator styles={styles.current} />}
143149
</ScrollView>
144150
);
145151
};

src/timeline/TimelineHours.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const TimelineHours = (props: TimelineHoursProps) => {
9595
</React.Fragment>
9696
);
9797
})}
98+
<View style={styles.verticalLine}/>
9899
</>
99100
);
100101
};

src/timeline/style.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Platform, StyleSheet} from 'react-native';
33
import {Theme} from '../types';
44

55
// const eventPaddingLeft = 4
6-
const leftMargin = 50 - 1;
6+
export const HOURS_SIDEBAR_WIDTH = 72;
77

88
export default function styleConstructor(theme: Theme = {}, calendarHeight: number) {
99
return StyleSheet.create({
@@ -86,24 +86,49 @@ export default function styleConstructor(theme: Theme = {}, calendarHeight: numb
8686
line: {
8787
height: 1,
8888
position: 'absolute',
89-
left: leftMargin,
89+
left: HOURS_SIDEBAR_WIDTH - 16,
9090
backgroundColor: 'rgb(216,216,216)',
9191
...theme.line
9292
},
93-
lineNow: {
93+
verticalLine: {
94+
position: 'absolute',
95+
width: 1,
96+
height: '105%',
97+
backgroundColor: 'rgb(216,216,216)',
98+
left: HOURS_SIDEBAR_WIDTH
99+
},
100+
nowIndicator: {
101+
position: 'absolute',
102+
left: HOURS_SIDEBAR_WIDTH,
103+
right: 0,
104+
},
105+
nowIndicatorLine: {
94106
height: 1,
95107
position: 'absolute',
96-
left: leftMargin,
108+
left: 0,
109+
right: 0,
110+
backgroundColor: 'red',
111+
...theme.nowIndicatorLine
112+
},
113+
nowIndicatorKnob: {
114+
position: 'absolute',
115+
left: -3,
116+
top: -3,
117+
width: 7,
118+
height: 7,
119+
borderRadius: 4,
97120
backgroundColor: 'red',
98-
...theme.lineNow
121+
...theme.nowIndicatorKnob
99122
},
100123
timeLabel: {
101124
position: 'absolute',
102-
left: 15,
103125
color: 'rgb(170,170,170)',
104126
fontSize: 10,
105127
fontFamily: Platform.OS === 'ios' ? 'Helvetica Neue' : 'Roboto',
106128
fontWeight: '500',
129+
paddingLeft: 12,
130+
textAlign: 'center',
131+
width: HOURS_SIDEBAR_WIDTH - 16,
107132
...theme.timeLabel
108133
}
109134
});

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export interface Theme {
2323
eventSummary?: object;
2424
eventTimes?: object;
2525
line?: object;
26-
lineNow?: object;
26+
nowIndicatorLine?: object;
27+
nowIndicatorKnob?: object;
2728
timeLabel?: object;
2829
todayTextColor?: string;
2930
calendarBackground?: string;

0 commit comments

Comments
 (0)