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

Add public methods for animation #641

Merged
merged 4 commits into from
Apr 6, 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
26 changes: 6 additions & 20 deletions source/Grid/Grid.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,34 +379,20 @@ describe('Grid', () => {
expect(grid.state.scrollTop).toEqual(50)
})

it('should support getScrollLeft() method', () => {
it('should support getOffsetForCell() public method', () => {
const grid = render(getMarkup())
const initialScrollLeft = grid.getScrollLeft()
const calculatedScrollLeft = grid.getScrollLeft({
...grid.props,
scrollToColumn: 24
const { scrollLeft, scrollTop } = grid.getOffsetForCell({
columnIndex: 24,
rowIndex: 49
Copy link
Owner

Choose a reason for hiding this comment

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

Much nicer 👍

})

expect(initialScrollLeft).toEqual(0)
// 100 columns * 50 item width = 5,000 total item width
// 4 columns can be visible at a time and :scrollLeft is initially 0,
// So the minimum amount of scrolling leaves the 25th item at the right (just scrolled into view).
expect(calculatedScrollLeft).toEqual(1050)
})

it('should support getScrollTop() method', () => {
const grid = render(getMarkup())
const initialScrollTop = grid.getScrollTop()
const calculatedScrollTop = grid.getScrollTop({
...grid.props,
scrollToRow: 49
})

expect(initialScrollTop).toEqual(0)
expect(scrollLeft).toEqual(1050)
// 100 rows * 20 item height = 2,000 total item height
// 5 rows can be visible at a time and :scrollTop is initially 0,
// So the minimum amount of scrolling leaves the 50th item at the bottom (just scrolled into view).
expect(calculatedScrollTop).toEqual(900)
expect(scrollTop).toEqual(900)
})

// See issue #565
Expand Down
36 changes: 20 additions & 16 deletions source/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,6 @@ export default class Grid extends PureComponent {
this._invokeOnGridRenderedHelper = this._invokeOnGridRenderedHelper.bind(this)
this._onScroll = this._onScroll.bind(this)
this._setScrollingContainerRef = this._setScrollingContainerRef.bind(this)
this._getCalculatedScrollLeft = this._getCalculatedScrollLeft.bind(this)
this._updateScrollLeftForScrollToColumn = this._updateScrollLeftForScrollToColumn.bind(this)
this._getCalculatedScrollTop = this._getCalculatedScrollTop.bind(this)
this._updateScrollTopForScrollToRow = this._updateScrollTopForScrollToRow.bind(this)
this._setScrollPosition = this._setScrollPosition.bind(this)

this._columnWidthGetter = this._wrapSizeGetter(props.columnWidth)
this._rowHeightGetter = this._wrapSizeGetter(props.rowHeight)
Expand Down Expand Up @@ -389,24 +384,33 @@ export default class Grid extends PureComponent {
* Useful for animating position changes
*/
scrollToPosition ({
scrollLeft = 0,
scrollTop = 0
scrollLeft,
scrollTop
} = {}) {
this._setScrollPosition({ scrollLeft, scrollTop })
}

/**
* Gets a calculated value to be used for `scrollLeft`
* Gets offsets for a given cell and alignment
*/
getScrollLeft (props = this.props, state = this.state) {
return this._getCalculatedScrollLeft(props, state) || 0;
}
getOffsetForCell ({
columnIndex,
rowIndex,
scrollToAlignment = this.props.scrollToAlignment
} = {}) {
const scrollToColumn = columnIndex >= 0 ? columnIndex : this.props.scrollToColumn
const scrollToRow = rowIndex >= 0 ? rowIndex : this.props.scrollToRow
Copy link
Owner

Choose a reason for hiding this comment

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

Tiny nit: You could set the default columnIndex and rowIndex values in the same way as you do the scrollToAlignment for consistency.

const offsetProps = {
...this.props,
scrollToColumn,
scrollToRow,
scrollToAlignment
}

/**
* Gets a calculated value to be used for `scrollTop`
*/
getScrollTop (props = this.props, state = this.state) {
return this._getCalculatedScrollTop(props, state) || 0;
return {
scrollLeft: this._getCalculatedScrollLeft(offsetProps),
scrollTop: this._getCalculatedScrollTop(offsetProps)
}
}

componentDidMount () {
Expand Down
14 changes: 11 additions & 3 deletions source/List/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,17 @@ export default class List extends PureComponent {
})
}

/** See Grid#getScrollTop */
getScrollTop (props = this.props) {
return this.Grid.getScrollTop(props)
/** See Grid#getOffsetForCell */
getOffsetForRow ({
rowIndex,
scrollToAlignment
}) {
const { scrollTop } = this.Grid.getOffsetForCell({
columnIndex: 0,
rowIndex,
scrollToAlignment
})
return scrollTop
}

/** See Grid#scrollToCell */
Expand Down