Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure time indicator is updated after navigation #1082

Merged
merged 2 commits into from
Nov 27, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions src/DayColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DayColumn extends React.Component {
timeslots: 2,
}

state = { selecting: false }
state = { selecting: false, timeIndicatorPosition: null }

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

if (this.props.isNow) {
this.positionTimeIndicator()
this.triggerTimeIndicatorUpdate()
this.setTimeIndicatorPositionUpdateInterval()
}
}

componentWillUnmount() {
this._teardownSelectable()
window.clearTimeout(this._timeIndicatorTimeout)
this.clearTimeIndicatorInterval()
}

componentWillReceiveProps(nextProps) {
Expand All @@ -84,22 +83,45 @@ class DayColumn extends React.Component {
this.slotMetrics = this.slotMetrics.update(nextProps)
}

triggerTimeIndicatorUpdate() {
// Update the position of the time indicator every minute
this._timeIndicatorTimeout = window.setTimeout(() => {
componentDidUpdate(prevProps, prevState) {
if (prevProps.isNow !== this.props.isNow) {
this.clearTimeIndicatorInterval()

if (this.props.isNow) {
this.setTimeIndicatorPositionUpdateInterval(
prevState.timeIndicatorPosition === this.state.timeIndicatorPosition
)
}
}
}

/**
* @param tail {Boolean} - whether `positionTimeIndicator` call should be
* deferred or called upon setting interval (`true` - if deferred);
*/
setTimeIndicatorPositionUpdateInterval(tail = false) {
if (!tail) {
this.positionTimeIndicator()
}

this._timeIndicatorInterval = window.setInterval(() => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use a setTimeout instead of setInterval, which can get weird on long running timers

this.positionTimeIndicator()
this.triggerTimeIndicatorUpdate()
}, 60000)
}

clearTimeIndicatorInterval() {
window.clearInterval(this._timeIndicatorInterval)
}

positionTimeIndicator() {
const { min, max, getNow } = this.props
const current = getNow()
const timeIndicator = this.refs.timeIndicator

if (current >= min && current <= max) {
const { top } = this.slotMetrics.getRange(current, current)
timeIndicator.style.top = `${top}%`
this.setState({ timeIndicatorPosition: top })
} else {
this.clearTimeIndicatorInterval()
}
}

Expand Down Expand Up @@ -162,7 +184,10 @@ class DayColumn extends React.Component {
</div>
)}
{isNow && (
<div ref="timeIndicator" className="rbc-current-time-indicator" />
<div
className="rbc-current-time-indicator"
style={{ top: `${this.state.timeIndicatorPosition}%` }}
/>
)}
</div>
)
Expand All @@ -188,7 +213,7 @@ class DayColumn extends React.Component {
events,
accessors,
slotMetrics,
minimumStartDifference: Math.ceil(step * timeslots / 2),
minimumStartDifference: Math.ceil((step * timeslots) / 2),
})

return styledEvents.map(({ event, style }, idx) => {
Expand Down