diff options
author | Eugene <eug-vs@keemail.me> | 2020-01-08 20:04:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-08 20:04:03 +0000 |
commit | 2a501e2877ee82a9d2cad4c714083b1d190d2aa2 (patch) | |
tree | 994b3c43d27d9dc29280210f8dfad15718f522a1 /src/components | |
parent | 4931f205e1250c649b3a48b96a7823a9d52615ff (diff) | |
parent | 20cb625343f4f46bcbea0f654f933f94de074ec1 (diff) | |
download | chrono-cube-ui-2a501e2877ee82a9d2cad4c714083b1d190d2aa2.tar.gz |
Merge pull request #30 from Eug-VS/react-window
Implement virtualized rendering
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/Scoreboard/Scoreboard.js | 56 | ||||
-rw-r--r-- | src/components/SmartList/SmartList.js | 38 |
2 files changed, 69 insertions, 25 deletions
diff --git a/src/components/Scoreboard/Scoreboard.js b/src/components/Scoreboard/Scoreboard.js index c7bc6d8..a101a5b 100644 --- a/src/components/Scoreboard/Scoreboard.js +++ b/src/components/Scoreboard/Scoreboard.js @@ -1,23 +1,24 @@ import React, { useEffect, useState } from 'react'; -import { - Container, - Typography, - Grid, -} from "@material-ui/core"; - import { makeStyles } from "@material-ui/core/styles"; import { get } from "../../requests"; + +import SmartList from "../SmartList/SmartList"; import SolutionCard from "../SolutionCard/SolutionCard"; import Loading from "../Loading/Loading"; const useStyles = makeStyles(theme => ({ - pageHeader: { - textAlign: 'center', - margin: theme.spacing(2), - }, + cell: { + display: 'flex', + justifyContent: 'center', + padding: theme.spacing(4), + + '& .MuiCard-root': { + width: '30%', + } + } })); const Scoreboard = () => { @@ -40,21 +41,26 @@ const Scoreboard = () => { updateSolutions(); }, []); - return ( - <Container maxWidth="xs"> - <Typography variant="h3" className={classes.pageHeader}> - Scoreboard - </Typography> - {(solutions.length === 0) && <Loading />} - <Grid container justify="center" direction="column" spacing={3}> - {solutions.slice(0, 30).map(solution => ( - <Grid item key={solution.id}> - <SolutionCard data={solution} removeThisCard={removeSolution}/> - </Grid> - ))} - </Grid> - </Container> - ); + const renderItem = ({ index, style }) => { + return ( + <div style={style} className={classes.cell}> + <SolutionCard data={solutions[index]} removeThisCard={removeSolution}/> + </div> + ) + }; + + + return (solutions.length === 0) ? + <div className={classes.cell}> + <Loading/> + </div> + : + <SmartList + cellHeight={300} + itemCount={solutions.length} + renderItem={renderItem} + /> }; + export default Scoreboard; diff --git a/src/components/SmartList/SmartList.js b/src/components/SmartList/SmartList.js new file mode 100644 index 0000000..6cd774b --- /dev/null +++ b/src/components/SmartList/SmartList.js @@ -0,0 +1,38 @@ +import React from 'react'; + +import { FixedSizeList } from "react-window"; + +import { makeStyles } from "@material-ui/core"; + + +const useStyles = makeStyles(theme => ({ + root: { + scrollbarColor: `${theme.palette.primary.dark} ${theme.palette.primary.light}`, + } +})); + + +const SmartList = ({ height, width, cellHeight, itemCount, renderItem }) => { + const classes = useStyles(); + + if (!height) { + const windowHeight = window.innerHeight; + const headerHeight = document.getElementsByClassName("MuiAppBar-root")[0].clientHeight; + height = windowHeight - headerHeight + } + + return ( + <FixedSizeList + height={height} + width={width} + itemSize={cellHeight} + itemCount={itemCount} + className={classes.root} + > + {renderItem} + </FixedSizeList> + ); +}; + + +export default SmartList; |