Ensure that scrolling does not go out of bounds for table graph

pull/4215/head
Brandon Farmer 2018-08-14 11:14:39 -07:00
parent ecd0d85b6d
commit 3232b80aa7
1 changed files with 16 additions and 4 deletions

View File

@ -29,6 +29,7 @@ export interface PropsMultiGrid {
} }
interface State { interface State {
scrollToRow: number
scrollLeft: number scrollLeft: number
scrollTop: number scrollTop: number
scrollbarSize: number scrollbarSize: number
@ -53,15 +54,22 @@ class MultiGrid extends React.PureComponent<PropsMultiGrid, State> {
} }
public static getDerivedStateFromProps(nextProps: PropsMultiGrid) { public static getDerivedStateFromProps(nextProps: PropsMultiGrid) {
const {scrollToRow} = nextProps const {scrollToRow, rowCount, height} = nextProps
const scrollTop = scrollToRow * ROW_HEIGHT const scrollTop = scrollToRow * ROW_HEIGHT
const maxScrollTop = (rowCount + 1) * ROW_HEIGHT - (height + ROW_HEIGHT)
const maxRow = rowCount - Math.floor(height / ROW_HEIGHT) / 2
if (scrollTop < 0) { if (scrollTop <= 0) {
return null
}
if (scrollTop > maxScrollTop || scrollToRow > maxRow) {
return null return null
} }
return { return {
scrollTop, scrollToRow,
scrollTop: scrollTop - height / 2,
} }
} }
@ -81,6 +89,7 @@ class MultiGrid extends React.PureComponent<PropsMultiGrid, State> {
super(props, context) super(props, context)
this.state = { this.state = {
scrollToRow: props.scrollToRow,
scrollLeft: 0, scrollLeft: 0,
scrollTop: 0, scrollTop: 0,
scrollbarSize: 0, scrollbarSize: 0,
@ -267,6 +276,7 @@ class MultiGrid extends React.PureComponent<PropsMultiGrid, State> {
private renderBottomLeftGrid(props) { private renderBottomLeftGrid(props) {
const {fixedColumnCount, fixedRowCount, rowCount, columnWidth} = props const {fixedColumnCount, fixedRowCount, rowCount, columnWidth} = props
const {scrollToRow} = this.state
if (!fixedColumnCount) { if (!fixedColumnCount) {
return null return null
@ -289,6 +299,7 @@ class MultiGrid extends React.PureComponent<PropsMultiGrid, State> {
> >
<Grid <Grid
{...props} {...props}
scrollToRow={scrollToRow}
cellRenderer={this.cellRendererBottomLeftGrid} cellRenderer={this.cellRendererBottomLeftGrid}
className={this.props.classNameBottomLeftGrid} className={this.props.classNameBottomLeftGrid}
columnCount={fixedColumnCount} columnCount={fixedColumnCount}
@ -317,11 +328,12 @@ class MultiGrid extends React.PureComponent<PropsMultiGrid, State> {
fixedColumnCount, fixedColumnCount,
fixedRowCount, fixedRowCount,
rowCount, rowCount,
scrollToRow,
scrollLeft, scrollLeft,
scrollTop, scrollTop,
} = props } = props
const {scrollToRow} = this.state
const calculatedRowCount = Math.max(0, rowCount - fixedRowCount) const calculatedRowCount = Math.max(0, rowCount - fixedRowCount)
const leftWidth = this.getLeftGridWidth(props) const leftWidth = this.getLeftGridWidth(props)