Skip to content

Instantly share code, notes, and snippets.

@taylorlee
Last active July 24, 2017 18:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taylorlee/01954d237d61a3ec9b4a324f92a87291 to your computer and use it in GitHub Desktop.
Save taylorlee/01954d237d61a3ec9b4a324f92a87291 to your computer and use it in GitHub Desktop.
react-virtualized-usage

this is example code for bvaughn/react-virtualized#147 (comment)

disclaimer: I simplified the code and renamed things, so I might have totally broken it, or omitted crucial css.

class Spreadsheet extends React.Component {
    render () {
        let rowCount    = this.props.rows.length;
        let columnCount = this.props.columns.length;
        let width       = CELL_WIDTH  * columnCount;
        let height      = CELL_HEIGHT * rowCount;

        let windowedHeight = window.innerHeight - BOTTOM_MARGIN;

        if (windowedHeight < height) {
            height = windowedHeight;
            width += SCROLLBAR_SIZE
        }
        return (
            <div onWheel={
                event => {
                    // handle horizontal scroll natively
                    document
                        .getElementById("scroll-wrapper")
                        .scrollLeft += event.deltaX;

                    //handle vertical scroll virtually
                    event.target = this.grid._scrollingContainer;
                    event.target.scrollTop += event.deltaY;
                    this.grid._onScroll(event);
                }
            }
            >
                <ScrollSync>
                {({...scrolling}) => (
                    <div
                        id="scroll-wrapper"
                        style={{
                            width : window.innerWidth,
                        }}
                    >
                        <Grid
                            id           = "header"

                            rowCount     = {1}
                            columnCount  = {columnCount}
                            height       = {CELL_HEIGHT}
                            width        = {width}
                            rowHeight    = {CELL_HEIGHT}
                            columnWidth  = {CELL_WIDTH}

                            cellRenderer = {colRenderer}
                            scrollLeft   = {scrolling.scrollLeft}
                        />
                        <Grid
                            id           = "body"
                            ref          = {grid=>this.grid=grid}

                            rowCount     = {rowCount}
                            columnCount  = {columnCount}
                            height       = {height}
                            width        = {width}
                            rowHeight    = {CELL_HEIGHT}
                            columnWidth  = {CELL_WIDTH}

                            cellRenderer = {cellRenderer}
                            onScroll     = {scrolling.onScroll}
                        />
                    </div>
                )}
                </ScrollSync>
            </div>
        ) 
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment