Skip to content
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 8 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions docs/src/pages/demos/tables/CustomizedTable.tsx
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);
82 changes: 82 additions & 0 deletions docs/src/pages/demos/tables/DenseTable.tsx
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);
6 changes: 2 additions & 4 deletions docs/src/pages/demos/tables/ReactVirtualizedTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,15 @@ const data = [
['Gingerbread', 356, 16.0, 49, 3.9],
];

let id = 0;
function createData(dessert, calories, fat, carbs, protein) {
id += 1;
function createData(id, dessert, calories, fat, carbs, protein) {
return { id, dessert, calories, fat, carbs, protein };
}

const rows = [];

for (let i = 0; i < 200; i += 1) {
const randomSelection = data[Math.floor(Math.random() * data.length)];
rows.push(createData(...randomSelection));
rows.push(createData(i, ...randomSelection));
}

function ReactVirtualizedTable() {
Expand Down
Loading