Skip to content

Commit 071fa88

Browse files
committed
fix: time indicator placement
1 parent be81065 commit 071fa88

File tree

3 files changed

+40
-60
lines changed

3 files changed

+40
-60
lines changed

src/DayColumn.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DayColumn extends React.Component {
2121
min: PropTypes.instanceOf(Date).isRequired,
2222
max: PropTypes.instanceOf(Date).isRequired,
2323
getNow: PropTypes.func.isRequired,
24+
isNow: PropTypes.bool,
2425

2526
rtl: PropTypes.bool,
2627

@@ -63,10 +64,16 @@ class DayColumn extends React.Component {
6364

6465
componentDidMount() {
6566
this.props.selectable && this._selectable()
67+
68+
if (this.props.isNow) {
69+
this.positionTimeIndicator()
70+
this.triggerTimeIndicatorUpdate()
71+
}
6672
}
6773

6874
componentWillUnmount() {
6975
this._teardownSelectable()
76+
window.clearTimeout(this._timeIndicatorTimeout)
7077
}
7178

7279
componentWillReceiveProps(nextProps) {
@@ -77,12 +84,30 @@ class DayColumn extends React.Component {
7784
this.slotMetrics = this.slotMetrics.update(nextProps)
7885
}
7986

87+
triggerTimeIndicatorUpdate() {
88+
// Update the position of the time indicator every minute
89+
this._timeIndicatorTimeout = window.setTimeout(() => {
90+
this.positionTimeIndicator()
91+
this.triggerTimeIndicatorUpdate()
92+
}, 60000)
93+
}
94+
95+
positionTimeIndicator() {
96+
const { min, max, getNow } = this.props
97+
const current = getNow()
98+
const timeIndicator = this.refs.timeIndicator
99+
100+
if (current >= min && current <= max) {
101+
const { top } = this.slotMetrics.getRange(current, current)
102+
timeIndicator.style.top = `${top}%`
103+
}
104+
}
105+
80106
render() {
81107
const {
82108
max,
83109
rtl,
84-
date,
85-
getNow,
110+
isNow,
86111
resource,
87112
accessors,
88113
localizer,
@@ -96,7 +121,6 @@ class DayColumn extends React.Component {
96121
let selectDates = { start: startDate, end: endDate }
97122

98123
const { className, style } = dayProp(max)
99-
const current = getNow()
100124

101125
return (
102126
<div
@@ -105,8 +129,9 @@ class DayColumn extends React.Component {
105129
className,
106130
'rbc-day-slot',
107131
'rbc-time-column',
108-
selecting && 'rbc-slot-selecting',
109-
dates.eq(date, current, 'day') && 'rbc-today'
132+
isNow && 'rbc-now',
133+
isNow && 'rbc-today', // WHY
134+
selecting && 'rbc-slot-selecting'
110135
)}
111136
>
112137
{slotMetrics.groups.map((grp, idx) => (
@@ -136,6 +161,9 @@ class DayColumn extends React.Component {
136161
<span>{localizer.format(selectDates, 'selectRangeFormat')}</span>
137162
</div>
138163
)}
164+
{isNow && (
165+
<div ref="timeIndicator" className="rbc-current-time-indicator" />
166+
)}
139167
</div>
140168
)
141169
}

src/TimeGrid.js

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ export default class TimeGrid extends Component {
8282

8383
this.applyScroll()
8484

85-
this.positionTimeIndicator()
86-
this.triggerTimeIndicatorUpdate()
87-
8885
window.addEventListener('resize', this.handleResize)
8986
}
9087

@@ -99,7 +96,6 @@ export default class TimeGrid extends Component {
9996
this.rafHandle = raf(this.checkOverflow)
10097
}
10198
componentWillUnmount() {
102-
window.clearTimeout(this._timeIndicatorTimeout)
10399
window.removeEventListener('resize', this.handleResize)
104100

105101
raf.cancel(this.rafHandle)
@@ -111,7 +107,6 @@ export default class TimeGrid extends Component {
111107
}
112108

113109
this.applyScroll()
114-
this.positionTimeIndicator()
115110
//this.checkOverflow()
116111
}
117112

@@ -146,7 +141,7 @@ export default class TimeGrid extends Component {
146141
})
147142
}
148143

149-
renderEvents(range, events, today) {
144+
renderEvents(range, events, now) {
150145
let { min, max, components, accessors, localizer } = this.props
151146

152147
const groupedEvents = this.resources.groupEvents(events)
@@ -170,7 +165,7 @@ export default class TimeGrid extends Component {
170165
max={dates.merge(date, max)}
171166
resource={resource && id}
172167
components={components}
173-
className={cn({ 'rbc-now': dates.eq(date, today, 'day') })}
168+
isNow={dates.eq(date, now, 'day')}
174169
key={i + '-' + jj}
175170
date={date}
176171
events={daysEvents}
@@ -270,8 +265,6 @@ export default class TimeGrid extends Component {
270265
className="rbc-time-gutter"
271266
/>
272267
{this.renderEvents(range, rangeEvents, getNow())}
273-
274-
<div ref="timeIndicator" className="rbc-current-time-indicator" />
275268
</div>
276269
</div>
277270
)
@@ -321,45 +314,4 @@ export default class TimeGrid extends Component {
321314
})
322315
}
323316
}
324-
325-
positionTimeIndicator() {
326-
const { rtl, min, max, getNow, range } = this.props
327-
const current = getNow()
328-
329-
const secondsGrid = dates.diff(max, min, 'seconds')
330-
const secondsPassed = dates.diff(current, min, 'seconds')
331-
332-
const timeIndicator = this.refs.timeIndicator
333-
const factor = secondsPassed / secondsGrid
334-
const timeGutter = this.gutter
335-
336-
const content = this.refs.content
337-
338-
if (timeGutter && current >= min && current <= max) {
339-
const pixelHeight = timeGutter.offsetHeight
340-
const dayPixelWidth =
341-
(content.offsetWidth - timeGutter.offsetWidth) / this.slots
342-
const dayOffset =
343-
range.findIndex(d => dates.eq(d, dates.today(), 'day')) * dayPixelWidth
344-
const offset = Math.floor(factor * pixelHeight)
345-
346-
timeIndicator.style.display = dayOffset >= 0 ? 'block' : 'none'
347-
timeIndicator.style[rtl ? 'left' : 'right'] = 0
348-
timeIndicator.style[rtl ? 'right' : 'left'] =
349-
timeGutter.offsetWidth + dayOffset + 'px'
350-
timeIndicator.style.top = offset + 'px'
351-
timeIndicator.style.width = dayPixelWidth + 'px'
352-
} else {
353-
timeIndicator.style.display = 'none'
354-
}
355-
}
356-
357-
triggerTimeIndicatorUpdate() {
358-
// Update the position of the time indicator every minute
359-
this._timeIndicatorTimeout = window.setTimeout(() => {
360-
this.positionTimeIndicator()
361-
362-
this.triggerTimeIndicatorUpdate()
363-
}, 60000)
364-
}
365317
}

src/less/time-grid.less

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
box-sizing: content-box;
3232
width: 100%;
3333
position: relative;
34-
3534
}
3635
.rbc-allday-cell + .rbc-allday-cell {
3736
border-left: 1px solid @cell-border;
@@ -48,7 +47,6 @@
4847
}
4948
}
5049

51-
5250
.rbc-time-header {
5351
display: flex;
5452
flex: 0 0 auto; // should not shrink below height
@@ -64,11 +62,11 @@
6462
}
6563

6664
> .rbc-row:first-child {
67-
border-bottom: 1px solid @cell-border;
65+
border-bottom: 1px solid @cell-border;
6866
}
6967

7068
> .rbc-row.rbc-row-resource {
71-
border-bottom: 1px solid @cell-border;
69+
border-bottom: 1px solid @cell-border;
7270
}
7371

7472
// .rbc-gutter-cell {
@@ -87,7 +85,7 @@
8785
flex-direction: column;
8886
border-left: 1px solid @cell-border;
8987

90-
.rbc-rtl & {
88+
.rbc-rtl & {
9189
border-left-width: 0;
9290
border-right: 1px solid @cell-border;
9391
}
@@ -125,6 +123,8 @@
125123
.rbc-current-time-indicator {
126124
position: absolute;
127125
z-index: 3;
126+
left: 0;
127+
right: 0;
128128
height: 1px;
129129

130130
background-color: @current-time-color;

0 commit comments

Comments
 (0)