Implement shouldComponentUpdate for ScrollbarComponent

It checks that the incoming scrollTop/Left and scrollHeight/Width differ
from their current values. The scrollTop/Left value are updated in
the component properties to always reflect the state of the DOM when
scrolling or when assigning a new value.
This commit is contained in:
Nathan Sobo
2014-04-15 13:35:02 -06:00
parent ddc677fb30
commit 64a487eebb
2 changed files with 19 additions and 12 deletions

View File

@@ -54,7 +54,7 @@
"temp": "0.5.0",
"text-buffer": "^2.1.0",
"theorist": "1.x",
"underscore-plus": "^1.1.2",
"underscore-plus": "^1.2.0",
"vm-compatibility-layer": "0.1.0"
},
"packageDependencies": {

View File

@@ -1,11 +1,9 @@
React = require 'react'
{div} = require 'reactionary'
{isEqualForProperties} = require 'underscore-plus'
module.exports =
ScrollbarComponent = React.createClass
lastScrollTop: null
lastScrollLeft: null
render: ->
{orientation, className, scrollHeight, scrollWidth} = @props
@@ -22,19 +20,24 @@ ScrollbarComponent = React.createClass
unless orientation is 'vertical' or orientation is 'horizontal'
throw new Error("Must specify an orientation property of 'vertical' or 'horizontal'")
shouldComponentUpdate: (newProps) ->
switch @props.orientation
when 'vertical'
not isEqualForProperties(newProps, @props, 'scrollHeight', 'scrollTop')
when 'horizontal'
not isEqualForProperties(newProps, @props, 'scrollWidth', 'scrollLeft')
componentDidUpdate: ->
{orientation, scrollTop, scrollLeft} = @props
node = @getDOMNode()
switch orientation
when 'vertical'
unless scrollTop is @lastScrollTop
node.scrollTop = scrollTop
@lastScrollTop = node.scrollTop
node.scrollTop = scrollTop
@props.scrollTop = node.scrollTop # Ensure scrollTop reflects actual DOM without triggering another update
when 'horizontal'
unless scrollLeft is @lastScrollLeft
node.scrollLeft = scrollLeft
@lastScrollLeft = node.scrollLeft
node.scrollLeft = scrollLeft
@props.scrollLeft = node.scrollLeft # Ensure scrollLeft reflects actual DOM without triggering another update
onScroll: ->
{orientation, onScroll} = @props
@@ -42,6 +45,10 @@ ScrollbarComponent = React.createClass
switch orientation
when 'vertical'
onScroll(node.scrollTop)
scrollTop = node.scrollTop
@props.scrollTop = scrollTop # Ensure scrollTop reflects actual DOM without triggering another update
onScroll(scrollTop)
when 'horizontal'
onScroll(node.scrollLeft)
scrollLeft = node.scrollLeft
@props.scrollLeft = scrollLeft # Ensure scrollLeft reflects actual DOM without triggering another update
onScroll(scrollLeft)