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

Allow syncing of scrollLeft and scrollTop from props #624

Merged
merged 2 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
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
46 changes: 46 additions & 0 deletions source/MultiGrid/MultiGrid.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,50 @@ describe('MultiGrid', () => {
expect(bottomRightGrid.style.backgroundColor).toEqual('red')
})
})
describe('scrollTop and scrollLeft', () => {
it('should adjust :scrollLeft for top-right and main grids when scrollLeft is used', () => {
const rendered = findDOMNode(render(getMarkup({
columnWidth: 50,
fixedColumnCount: 2,
scrollLeft: 850
})))
const grids = rendered.querySelectorAll('.ReactVirtualized__Grid')
const topRightGrid = grids[1]
const bottomRightGrid = grids[3]
expect(topRightGrid.scrollLeft).toEqual(850)
expect(bottomRightGrid.scrollLeft).toEqual(850)
})

it('should adjust :scrollTop for bottom-left and main grids when scrollTop is used', () => {
const rendered = findDOMNode(render(getMarkup({
columnWidth: 50,
fixedColumnCount: 2,
scrollTop: 500
})))
const grids = rendered.querySelectorAll('.ReactVirtualized__Grid')
const bottomLeftGrid = grids[2]
const bottomRightGrid = grids[3]
expect(bottomLeftGrid.scrollTop).toEqual(500)
expect(bottomRightGrid.scrollTop).toEqual(500)
})

it('should adjust :scrollTop and :scrollLeft when scrollTop and scrollLeft change', () => {
render(getMarkup({
columnWidth: 50,
fixedColumnCount: 2
Copy link
Owner

Choose a reason for hiding this comment

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

These are default values so you can remove them if you want. Either that, or copy them below. Otherwise- if they weren't default values- then the second render would also change columnWidth and fixedColumnCount too (which is probably not what you want, since it potentially gives you a false positive)

}))
const rendered = findDOMNode(render(getMarkup({
scrollTop: 750,
scrollLeft: 900
})))
const grids = rendered.querySelectorAll('.ReactVirtualized__Grid')
const topRightGrid = grids[1]
const bottomLeftGrid = grids[2]
const bottomRightGrid = grids[3]
expect(topRightGrid.scrollLeft).toEqual(900)
expect(bottomRightGrid.scrollLeft).toEqual(900)
expect(bottomLeftGrid.scrollTop).toEqual(750)
expect(bottomRightGrid.scrollTop).toEqual(750)
})
})
})
34 changes: 33 additions & 1 deletion source/MultiGrid/MultiGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ export default class MultiGrid extends PureComponent {
}

componentDidMount () {
const { scrollLeft, scrollTop } = this.props

if (scrollLeft >= 0 || scrollTop >= 0) {
const newState = {}

if (scrollLeft >= 0) {
newState.scrollLeft = scrollLeft
}

if (scrollTop >= 0) {
newState.scrollTop = scrollTop
}

this.setState(newState)
}
this._handleInvalidatedGridSize()
}

Expand Down Expand Up @@ -144,6 +159,23 @@ export default class MultiGrid extends PureComponent {
this._topGridHeight = null
}

if (
nextProps.scrollLeft !== this.state.scrollLeft ||
nextProps.scrollTop !== this.state.scrollTop
Copy link
Owner

Choose a reason for hiding this comment

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

This should compare nextProps to this.props instead of this.state right? Otherwise, it will interfere with user scrolling.

This kind of half-controlled prop is troublesome. In hindsight, I should have probably used a different approach here. Maybe a public method you call on the ref instead, I dunno.

) {
const newState = {}

if (nextProps.scrollLeft != null) {
Copy link
Owner

Choose a reason for hiding this comment

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

This should probably be nextProps.scrollLeft !== this.props.scrollLeft ?

newState.scrollLeft = nextProps.scrollLeft
}

if (nextProps.scrollTop != null) {
newState.scrollTop = nextProps.scrollTop
}

this.setState(newState)
}

this._maybeCalculateCachedStyles(this.props, nextProps, this.state, nextState)
}

Expand All @@ -165,7 +197,7 @@ export default class MultiGrid extends PureComponent {
return null
}

// scrollTop and scrollToRow props are explicitly filtered out and ignored
// scrollTop and scrollLeft props are explicitly filtered out and ignored

const {
scrollLeft,
Expand Down