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 all commits
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
43 changes: 43 additions & 0 deletions source/MultiGrid/MultiGrid.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,47 @@ 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())
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)
})
})
})
40 changes: 39 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,29 @@ export default class MultiGrid extends PureComponent {
this._topGridHeight = null
}

if (
nextProps.scrollLeft !== this.props.scrollLeft ||
nextProps.scrollTop !== this.props.scrollTop
) {
const newState = {}

if (
nextProps.scrollLeft != null &&
nextProps.scrollLeft >= 0
) {
newState.scrollLeft = nextProps.scrollLeft
}

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

this.setState(newState)
}

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

Expand All @@ -165,7 +203,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