-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[docs] More Table TypeScript demos #15086
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e2c5101
two more tables
jasondashwang 1719353
require help on virtualized table
jasondashwang 8321d3d
CustomPaginationActionsTable demo
jasondashwang 9bb9ca7
reversed virtualized table prop changes
jasondashwang 6c8ad26
cannot figure out virtualized table
jasondashwang fe7b54f
Merge branch 'next' of github.com:mui-org/material-ui into next
jasondashwang 8fc0a4a
formatted JS
jasondashwang 602e53f
ran yarn prettier
jasondashwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
docs/src/pages/demos/tables/CustomPaginationActionsTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles, useTheme, Theme, createStyles } from '@material-ui/core/styles'; | ||
import Table from '@material-ui/core/Table'; | ||
import TableBody from '@material-ui/core/TableBody'; | ||
import TableCell from '@material-ui/core/TableCell'; | ||
import TableFooter from '@material-ui/core/TableFooter'; | ||
import TablePagination from '@material-ui/core/TablePagination'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
import Paper from '@material-ui/core/Paper'; | ||
import IconButton from '@material-ui/core/IconButton'; | ||
import FirstPageIcon from '@material-ui/icons/FirstPage'; | ||
import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft'; | ||
import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight'; | ||
import LastPageIcon from '@material-ui/icons/LastPage'; | ||
|
||
const useStyles1 = makeStyles((theme: Theme) => | ||
createStyles({ | ||
root: { | ||
flexShrink: 0, | ||
color: theme.palette.text.secondary, | ||
marginLeft: theme.spacing(2.5), | ||
}, | ||
}), | ||
); | ||
|
||
interface TablePaginationActionsProps { | ||
count: number; | ||
page: number; | ||
rowsPerPage: number; | ||
onChangePage: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>, newPage: number) => void; | ||
} | ||
|
||
function TablePaginationActions(props: TablePaginationActionsProps) { | ||
const classes = useStyles1(); | ||
const theme = useTheme(); | ||
const { count, page, rowsPerPage, onChangePage } = props; | ||
|
||
function handleFirstPageButtonClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) { | ||
onChangePage(event, 0); | ||
} | ||
|
||
function handleBackButtonClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) { | ||
onChangePage(event, page - 1); | ||
} | ||
|
||
function handleNextButtonClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) { | ||
onChangePage(event, page + 1); | ||
} | ||
|
||
function handleLastPageButtonClick(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) { | ||
onChangePage(event, Math.max(0, Math.ceil(count / rowsPerPage) - 1)); | ||
} | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<IconButton | ||
onClick={handleFirstPageButtonClick} | ||
disabled={page === 0} | ||
aria-label="First Page" | ||
> | ||
{theme.direction === 'rtl' ? <LastPageIcon /> : <FirstPageIcon />} | ||
</IconButton> | ||
<IconButton onClick={handleBackButtonClick} disabled={page === 0} aria-label="Previous Page"> | ||
{theme.direction === 'rtl' ? <KeyboardArrowRight /> : <KeyboardArrowLeft />} | ||
</IconButton> | ||
<IconButton | ||
onClick={handleNextButtonClick} | ||
disabled={page >= Math.ceil(count / rowsPerPage) - 1} | ||
aria-label="Next Page" | ||
> | ||
{theme.direction === 'rtl' ? <KeyboardArrowLeft /> : <KeyboardArrowRight />} | ||
</IconButton> | ||
<IconButton | ||
onClick={handleLastPageButtonClick} | ||
disabled={page >= Math.ceil(count / rowsPerPage) - 1} | ||
aria-label="Last Page" | ||
> | ||
{theme.direction === 'rtl' ? <FirstPageIcon /> : <LastPageIcon />} | ||
</IconButton> | ||
</div> | ||
); | ||
} | ||
|
||
TablePaginationActions.propTypes = { | ||
count: PropTypes.number.isRequired, | ||
onChangePage: PropTypes.func.isRequired, | ||
page: PropTypes.number.isRequired, | ||
rowsPerPage: PropTypes.number.isRequired, | ||
}; | ||
|
||
let counter = 0; | ||
function createData(name: string, calories: number, fat: number) { | ||
counter += 1; | ||
return { id: counter, name, calories, fat }; | ||
} | ||
|
||
const useStyles2 = makeStyles((theme: Theme) => | ||
createStyles({ | ||
root: { | ||
width: '100%', | ||
marginTop: theme.spacing(3), | ||
}, | ||
table: { | ||
minWidth: 500, | ||
}, | ||
tableWrapper: { | ||
overflowX: 'auto', | ||
}, | ||
}), | ||
); | ||
|
||
function CustomPaginationActionsTable() { | ||
const classes = useStyles2(); | ||
const [rows] = React.useState( | ||
[ | ||
createData('Cupcake', 305, 3.7), | ||
createData('Donut', 452, 25.0), | ||
createData('Eclair', 262, 16.0), | ||
createData('Frozen yoghurt', 159, 6.0), | ||
createData('Gingerbread', 356, 16.0), | ||
createData('Honeycomb', 408, 3.2), | ||
createData('Ice cream sandwich', 237, 9.0), | ||
createData('Jelly Bean', 375, 0.0), | ||
createData('KitKat', 518, 26.0), | ||
createData('Lollipop', 392, 0.2), | ||
createData('Marshmallow', 318, 0), | ||
createData('Nougat', 360, 19.0), | ||
createData('Oreo', 437, 18.0), | ||
].sort((a, b) => (a.calories < b.calories ? -1 : 1)), | ||
); | ||
const [page, setPage] = React.useState(0); | ||
const [rowsPerPage, setRowsPerPage] = React.useState(5); | ||
|
||
const emptyRows = rowsPerPage - Math.min(rowsPerPage, rows.length - page * rowsPerPage); | ||
|
||
function handleChangePage( | ||
event: React.MouseEvent<HTMLButtonElement, MouseEvent> | null, | ||
newPage: number, | ||
) { | ||
setPage(newPage); | ||
} | ||
|
||
function handleChangeRowsPerPage( | ||
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, | ||
) { | ||
setRowsPerPage(parseInt(event.target.value, 10)); | ||
} | ||
|
||
return ( | ||
<Paper className={classes.root}> | ||
<div className={classes.tableWrapper}> | ||
<Table className={classes.table}> | ||
<TableBody> | ||
{rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map(row => ( | ||
<TableRow key={row.id}> | ||
<TableCell component="th" scope="row"> | ||
{row.name} | ||
</TableCell> | ||
<TableCell align="right">{row.calories}</TableCell> | ||
<TableCell align="right">{row.fat}</TableCell> | ||
</TableRow> | ||
))} | ||
{emptyRows > 0 && ( | ||
<TableRow style={{ height: 48 * emptyRows }}> | ||
<TableCell colSpan={6} /> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
<TableFooter> | ||
<TableRow> | ||
<TablePagination | ||
rowsPerPageOptions={[5, 10, 25]} | ||
colSpan={3} | ||
count={rows.length} | ||
rowsPerPage={rowsPerPage} | ||
page={page} | ||
SelectProps={{ | ||
native: true, | ||
}} | ||
onChangePage={handleChangePage} | ||
onChangeRowsPerPage={handleChangeRowsPerPage} | ||
ActionsComponent={TablePaginationActions} | ||
/> | ||
</TableRow> | ||
</TableFooter> | ||
</Table> | ||
</div> | ||
</Paper> | ||
); | ||
} | ||
|
||
export default CustomPaginationActionsTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { withStyles, Theme, createStyles, WithStyles } from '@material-ui/core/styles'; | ||
import Table from '@material-ui/core/Table'; | ||
import TableBody from '@material-ui/core/TableBody'; | ||
import TableCell from '@material-ui/core/TableCell'; | ||
import TableHead from '@material-ui/core/TableHead'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
import Paper from '@material-ui/core/Paper'; | ||
|
||
const CustomTableCell = withStyles((theme: Theme) => | ||
createStyles({ | ||
head: { | ||
backgroundColor: theme.palette.common.black, | ||
color: theme.palette.common.white, | ||
}, | ||
body: { | ||
fontSize: 14, | ||
}, | ||
}), | ||
)(TableCell); | ||
|
||
const styles = (theme: Theme) => | ||
createStyles({ | ||
root: { | ||
width: '100%', | ||
marginTop: theme.spacing(3), | ||
overflowX: 'auto', | ||
}, | ||
table: { | ||
minWidth: 700, | ||
}, | ||
row: { | ||
'&:nth-of-type(odd)': { | ||
backgroundColor: theme.palette.background.default, | ||
}, | ||
}, | ||
}); | ||
|
||
let id = 0; | ||
function createData(name: string, calories: number, fat: number, carbs: number, protein: number) { | ||
id += 1; | ||
return { id, name, calories, fat, carbs, protein }; | ||
} | ||
|
||
const rows = [ | ||
createData('Frozen yoghurt', 159, 6.0, 24, 4.0), | ||
createData('Ice cream sandwich', 237, 9.0, 37, 4.3), | ||
createData('Eclair', 262, 16.0, 24, 6.0), | ||
createData('Cupcake', 305, 3.7, 67, 4.3), | ||
createData('Gingerbread', 356, 16.0, 49, 3.9), | ||
]; | ||
|
||
export interface CustomizedTableProps extends WithStyles<typeof styles> {} | ||
|
||
function CustomizedTable(props: CustomizedTableProps) { | ||
const { classes } = props; | ||
|
||
return ( | ||
<Paper className={classes.root}> | ||
<Table className={classes.table}> | ||
<TableHead> | ||
<TableRow> | ||
<CustomTableCell>Dessert (100g serving)</CustomTableCell> | ||
<CustomTableCell align="right">Calories</CustomTableCell> | ||
<CustomTableCell align="right">Fat (g)</CustomTableCell> | ||
<CustomTableCell align="right">Carbs (g)</CustomTableCell> | ||
<CustomTableCell align="right">Protein (g)</CustomTableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.map(row => ( | ||
<TableRow className={classes.row} key={row.id}> | ||
<CustomTableCell component="th" scope="row"> | ||
{row.name} | ||
</CustomTableCell> | ||
<CustomTableCell align="right">{row.calories}</CustomTableCell> | ||
<CustomTableCell align="right">{row.fat}</CustomTableCell> | ||
<CustomTableCell align="right">{row.carbs}</CustomTableCell> | ||
<CustomTableCell align="right">{row.protein}</CustomTableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</Paper> | ||
); | ||
} | ||
|
||
CustomizedTable.propTypes = { | ||
classes: PropTypes.object.isRequired, | ||
} as any; | ||
|
||
export default withStyles(styles)(CustomizedTable); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { createStyles, Theme, withStyles, WithStyles } from '@material-ui/core/styles'; | ||
import Table from '@material-ui/core/Table'; | ||
import TableBody from '@material-ui/core/TableBody'; | ||
import TableCell from '@material-ui/core/TableCell'; | ||
import TableHead from '@material-ui/core/TableHead'; | ||
import TableRow from '@material-ui/core/TableRow'; | ||
import Paper from '@material-ui/core/Paper'; | ||
|
||
const styles = (theme: Theme) => | ||
createStyles({ | ||
root: { | ||
width: '100%', | ||
}, | ||
paper: { | ||
marginTop: theme.spacing(3), | ||
width: '100%', | ||
overflowX: 'auto', | ||
marginBottom: theme.spacing(2), | ||
}, | ||
table: { | ||
minWidth: 650, | ||
}, | ||
}); | ||
|
||
let id = 0; | ||
function createData(name: string, calories: number, fat: number, carbs: number, protein: number) { | ||
id += 1; | ||
return { id, name, calories, fat, carbs, protein }; | ||
} | ||
|
||
const rows = [ | ||
createData('Frozen yoghurt', 159, 6.0, 24, 4.0), | ||
createData('Ice cream sandwich', 237, 9.0, 37, 4.3), | ||
createData('Eclair', 262, 16.0, 24, 6.0), | ||
createData('Cupcake', 305, 3.7, 67, 4.3), | ||
createData('Gingerbread', 356, 16.0, 49, 3.9), | ||
]; | ||
|
||
export interface DenseTableProps extends WithStyles<typeof styles> {} | ||
|
||
function DenseTable(props: DenseTableProps) { | ||
const { classes } = props; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Paper className={classes.paper}> | ||
<Table className={classes.table} size="small"> | ||
<TableHead> | ||
<TableRow> | ||
<TableCell>Dessert (100g serving)</TableCell> | ||
<TableCell align="right">Calories</TableCell> | ||
<TableCell align="right">Fat (g)</TableCell> | ||
<TableCell align="right">Carbs (g)</TableCell> | ||
<TableCell align="right">Protein (g)</TableCell> | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.map(row => ( | ||
<TableRow key={row.id}> | ||
<TableCell component="th" scope="row"> | ||
{row.name} | ||
</TableCell> | ||
<TableCell align="right">{row.calories}</TableCell> | ||
<TableCell align="right">{row.fat}</TableCell> | ||
<TableCell align="right">{row.carbs}</TableCell> | ||
<TableCell align="right">{row.protein}</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</Paper> | ||
</div> | ||
); | ||
} | ||
|
||
DenseTable.propTypes = { | ||
classes: PropTypes.object.isRequired, | ||
} as any; | ||
|
||
export default withStyles(styles)(DenseTable); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was auto generated by the
yarn docs:typescript:formatted
even though theTS
version does not have these spacesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine. This is a transpilation artifact. The whitespace is inserted because the types added additional whitespace in the TS version and
babel
tries to align the corresponding lines. Since JS naturally has less lines whitespace gets inserted