Skip to content

Commit 07b2fa4

Browse files
RusinovAntonjquense
authored andcommitted
fix: make sure time indicator is updated after navigation (#1082)
* fix: make sure time indicator is updated after navigation * Replace setInterval with setTimeout
1 parent aa8f08b commit 07b2fa4

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/DayColumn.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class DayColumn extends React.Component {
5454
timeslots: 2,
5555
}
5656

57-
state = { selecting: false }
57+
state = { selecting: false, timeIndicatorPosition: null }
5858

5959
constructor(...args) {
6060
super(...args)
@@ -66,14 +66,13 @@ class DayColumn extends React.Component {
6666
this.props.selectable && this._selectable()
6767

6868
if (this.props.isNow) {
69-
this.positionTimeIndicator()
70-
this.triggerTimeIndicatorUpdate()
69+
this.setTimeIndicatorPositionUpdateInterval()
7170
}
7271
}
7372

7473
componentWillUnmount() {
7574
this._teardownSelectable()
76-
window.clearTimeout(this._timeIndicatorTimeout)
75+
this.clearTimeIndicatorInterval()
7776
}
7877

7978
componentWillReceiveProps(nextProps) {
@@ -84,22 +83,49 @@ class DayColumn extends React.Component {
8483
this.slotMetrics = this.slotMetrics.update(nextProps)
8584
}
8685

87-
triggerTimeIndicatorUpdate() {
88-
// Update the position of the time indicator every minute
86+
componentDidUpdate(prevProps, prevState) {
87+
if (prevProps.isNow !== this.props.isNow) {
88+
this.clearTimeIndicatorInterval()
89+
90+
if (this.props.isNow) {
91+
this.setTimeIndicatorPositionUpdateInterval(
92+
prevState.timeIndicatorPosition === this.state.timeIndicatorPosition
93+
)
94+
}
95+
}
96+
}
97+
98+
intervalTriggered = false
99+
/**
100+
* @param tail {Boolean} - whether `positionTimeIndicator` call should be
101+
* deferred or called upon setting interval (`true` - if deferred);
102+
*/
103+
setTimeIndicatorPositionUpdateInterval(tail = false) {
104+
if (!this.intervalTriggered && !tail) {
105+
this.positionTimeIndicator()
106+
}
107+
89108
this._timeIndicatorTimeout = window.setTimeout(() => {
109+
this.intervalTriggered = true
90110
this.positionTimeIndicator()
91-
this.triggerTimeIndicatorUpdate()
111+
this.setTimeIndicatorPositionUpdateInterval()
92112
}, 60000)
93113
}
94114

115+
clearTimeIndicatorInterval() {
116+
this.intervalTriggered = false
117+
window.clearTimeout(this._timeIndicatorTimeout)
118+
}
119+
95120
positionTimeIndicator() {
96121
const { min, max, getNow } = this.props
97122
const current = getNow()
98-
const timeIndicator = this.refs.timeIndicator
99123

100124
if (current >= min && current <= max) {
101125
const { top } = this.slotMetrics.getRange(current, current)
102-
timeIndicator.style.top = `${top}%`
126+
this.setState({ timeIndicatorPosition: top })
127+
} else {
128+
this.clearTimeIndicatorInterval()
103129
}
104130
}
105131

@@ -162,7 +188,10 @@ class DayColumn extends React.Component {
162188
</div>
163189
)}
164190
{isNow && (
165-
<div ref="timeIndicator" className="rbc-current-time-indicator" />
191+
<div
192+
className="rbc-current-time-indicator"
193+
style={{ top: `${this.state.timeIndicatorPosition}%` }}
194+
/>
166195
)}
167196
</div>
168197
)

0 commit comments

Comments
 (0)