-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TablePagination] Add support for CSS variables (#32615)
- Loading branch information
1 parent
0240439
commit f6f6726
Showing
2 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
213 changes: 213 additions & 0 deletions
213
docs/pages/experiments/material-ui/table-pagination.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,213 @@ | ||
import * as React from 'react'; | ||
import { | ||
Experimental_CssVarsProvider as CssVarsProvider, | ||
useColorScheme, | ||
useTheme, | ||
} from '@mui/material/styles'; | ||
import CssBaseline from '@mui/material/CssBaseline'; | ||
import Box from '@mui/material/Box'; | ||
import Button from '@mui/material/Button'; | ||
import Container from '@mui/material/Container'; | ||
import Moon from '@mui/icons-material/DarkMode'; | ||
import Sun from '@mui/icons-material/LightMode'; | ||
import Table from '@mui/material/Table'; | ||
import TableBody from '@mui/material/TableBody'; | ||
import TableCell from '@mui/material/TableCell'; | ||
import TableContainer from '@mui/material/TableContainer'; | ||
import TableFooter from '@mui/material/TableFooter'; | ||
import TablePagination from '@mui/material/TablePagination'; | ||
import TableRow from '@mui/material/TableRow'; | ||
import Paper from '@mui/material/Paper'; | ||
import IconButton from '@mui/material/IconButton'; | ||
import FirstPageIcon from '@mui/icons-material/FirstPage'; | ||
import KeyboardArrowLeft from '@mui/icons-material/KeyboardArrowLeft'; | ||
import KeyboardArrowRight from '@mui/icons-material/KeyboardArrowRight'; | ||
import LastPageIcon from '@mui/icons-material/LastPage'; | ||
|
||
const ColorSchemePicker = () => { | ||
const { mode, setMode } = useColorScheme(); | ||
const [mounted, setMounted] = React.useState(false); | ||
React.useEffect(() => { | ||
setMounted(true); | ||
}, []); | ||
if (!mounted) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Button | ||
variant="outlined" | ||
onClick={() => { | ||
if (mode === 'light') { | ||
setMode('dark'); | ||
} else { | ||
setMode('light'); | ||
} | ||
}} | ||
> | ||
{mode === 'light' ? <Moon /> : <Sun />} | ||
</Button> | ||
); | ||
}; | ||
|
||
interface TablePaginationActionsProps { | ||
count: number; | ||
page: number; | ||
rowsPerPage: number; | ||
onPageChange: (event: React.MouseEvent<HTMLButtonElement>, newPage: number) => void; | ||
} | ||
|
||
function TablePaginationActions(props: TablePaginationActionsProps) { | ||
const theme = useTheme(); | ||
const { count, page, rowsPerPage, onPageChange } = props; | ||
|
||
const handleFirstPageButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, 0); | ||
}; | ||
|
||
const handleBackButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, page - 1); | ||
}; | ||
|
||
const handleNextButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, page + 1); | ||
}; | ||
|
||
const handleLastPageButtonClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
onPageChange(event, Math.max(0, Math.ceil(count / rowsPerPage) - 1)); | ||
}; | ||
|
||
return ( | ||
<Box sx={{ flexShrink: 0, ml: 2.5 }}> | ||
<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> | ||
</Box> | ||
); | ||
} | ||
|
||
function createData(name: string, calories: number, fat: number) { | ||
return { name, calories, fat }; | ||
} | ||
|
||
const rows = [ | ||
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)); | ||
|
||
function CustomPaginationActionsTable() { | ||
const [page, setPage] = React.useState(0); | ||
const [rowsPerPage, setRowsPerPage] = React.useState(5); | ||
|
||
// Avoid a layout jump when reaching the last page with empty rows. | ||
const emptyRows = page > 0 ? Math.max(0, (1 + page) * rowsPerPage - rows.length) : 0; | ||
|
||
const handleChangePage = (event: React.MouseEvent<HTMLButtonElement> | null, newPage: number) => { | ||
setPage(newPage); | ||
}; | ||
|
||
const handleChangeRowsPerPage = ( | ||
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, | ||
) => { | ||
setRowsPerPage(parseInt(event.target.value, 10)); | ||
setPage(0); | ||
}; | ||
|
||
return ( | ||
<TableContainer component={Paper}> | ||
<Table sx={{ minWidth: 500 }} aria-label="custom pagination table"> | ||
<TableBody> | ||
{(rowsPerPage > 0 | ||
? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) | ||
: rows | ||
).map((row) => ( | ||
<TableRow key={row.name}> | ||
<TableCell component="th" scope="row"> | ||
{row.name} | ||
</TableCell> | ||
<TableCell style={{ width: 160 }} align="right"> | ||
{row.calories} | ||
</TableCell> | ||
<TableCell style={{ width: 160 }} align="right"> | ||
{row.fat} | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
{emptyRows > 0 && ( | ||
<TableRow style={{ height: 53 * emptyRows }}> | ||
<TableCell colSpan={6} /> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
<TableFooter> | ||
<TableRow> | ||
<TablePagination | ||
rowsPerPageOptions={[5, 10, 25, { label: 'All', value: -1 }]} | ||
colSpan={3} | ||
count={rows.length} | ||
rowsPerPage={rowsPerPage} | ||
page={page} | ||
SelectProps={{ | ||
inputProps: { | ||
'aria-label': 'rows per page', | ||
}, | ||
native: true, | ||
}} | ||
onPageChange={handleChangePage} | ||
onRowsPerPageChange={handleChangeRowsPerPage} | ||
ActionsComponent={TablePaginationActions} | ||
/> | ||
</TableRow> | ||
</TableFooter> | ||
</Table> | ||
</TableContainer> | ||
); | ||
} | ||
|
||
export default function CssVarsTemplate() { | ||
return ( | ||
<CssVarsProvider> | ||
<CssBaseline /> | ||
<Container sx={{ my: 5 }}> | ||
<Box sx={{ pb: 2 }}> | ||
<ColorSchemePicker /> | ||
</Box> | ||
<Box> | ||
<CustomPaginationActionsTable /> | ||
</Box> | ||
</Container> | ||
</CssVarsProvider> | ||
); | ||
} |
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