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] Add virtualized table demo #13786

Merged
merged 2 commits into from Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/pages/demos/tables/CustomizedTable.js
Expand Up @@ -16,7 +16,7 @@ const CustomTableCell = withStyles(theme => ({
body: {
fontSize: 14,
},
}))(TableCell);
}))(props => <TableCell {...props} />);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #13776

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But TableCell is no forwardRef component. Why would this be an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eps1lon withStyles uses it.


const styles = theme => ({
root: {
Expand Down
227 changes: 227 additions & 0 deletions docs/src/pages/demos/tables/ReactVirtualizedTable.js
@@ -0,0 +1,227 @@
/* eslint-disable no-console */

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { withStyles } from '@material-ui/core/styles';
import TableCell from '@material-ui/core/TableCell';
import TableSortLabel from '@material-ui/core/TableSortLabel';
import Paper from '@material-ui/core/Paper';
import { AutoSizer, Column, SortDirection, Table } from 'react-virtualized';

const styles = theme => ({
table: {
fontFamily: theme.typography.fontFamily,
},
flexContainer: {
display: 'flex',
alignItems: 'center',
boxSizing: 'border-box',
},
tableRow: {
cursor: 'pointer',
},
tableRowHover: {
'&:hover': {
backgroundColor: theme.palette.grey[200],
},
},
tableCell: {
flex: 1,
},
noClick: {
cursor: 'initial',
},
});

class MuiVirtualizedTable extends React.PureComponent {
getRowClassName = ({ index }) => {
const { classes, rowClassName, onRowClick } = this.props;

return classNames(classes.tableRow, classes.flexContainer, rowClassName, {
[classes.tableRowHover]: index !== -1 && onRowClick != null,
});
};

cellRenderer = ({ cellData, columnIndex = null }) => {
const { columns, classes, rowHeight, onRowClick } = this.props;
return (
<TableCell
component="div"
className={classNames(classes.tableCell, classes.flexContainer, {
[classes.noClick]: onRowClick == null,
})}
variant="body"
style={{ height: rowHeight }}
numeric={(columnIndex != null && columns[columnIndex].numeric) || false}
>
{cellData}
</TableCell>
);
};

headerRenderer = ({ label, columnIndex, dataKey, sortBy, sortDirection }) => {
const { headerHeight, columns, classes, sort } = this.props;
const direction = {
[SortDirection.ASC]: 'asc',
[SortDirection.DESC]: 'desc',
};

const inner =
!columns[columnIndex].disableSort && sort != null ? (
<TableSortLabel active={dataKey === sortBy} direction={direction[sortDirection]}>
{label}
</TableSortLabel>
) : (
label
);

return (
<TableCell
component="div"
className={classNames(classes.tableCell, classes.flexContainer, classes.noClick)}
variant="head"
style={{ height: headerHeight }}
numeric={columns[columnIndex].numeric || false}
>
{inner}
</TableCell>
);
};

render() {
const { classes, columns, ...tableProps } = this.props;
return (
<AutoSizer>
{({ height, width }) => (
<Table
className={classes.table}
height={height}
width={width}
{...tableProps}
rowClassName={this.getRowClassName}
>
{columns.map(({ cellContentRenderer = null, className, dataKey, ...other }, index) => {
let renderer;
if (cellContentRenderer != null) {
renderer = cellRendererProps =>
this.cellRenderer({
cellData: cellContentRenderer(cellRendererProps),
columnIndex: index,
});
} else {
renderer = this.cellRenderer;
}

return (
<Column
key={dataKey}
headerRenderer={headerProps =>
this.headerRenderer({
...headerProps,
columnIndex: index,
})
}
className={classNames(classes.flexContainer, className)}
cellRenderer={renderer}
dataKey={dataKey}
{...other}
/>
);
})}
</Table>
)}
</AutoSizer>
);
}
}

MuiVirtualizedTable.propTypes = {
classes: PropTypes.object.isRequired,
columns: PropTypes.arrayOf(
PropTypes.shape({
cellContentRenderer: PropTypes.func,
dataKey: PropTypes.string.isRequired,
width: PropTypes.number.isRequired,
}),
).isRequired,
headerHeight: PropTypes.number,
onRowClick: PropTypes.func,
rowClassName: PropTypes.string,
rowHeight: PropTypes.oneOfType([PropTypes.number, PropTypes.func]),
sort: PropTypes.func,
};

MuiVirtualizedTable.defaultProps = {
headerHeight: 56,
rowHeight: 56,
};

const WrappedVirtualizedTable = withStyles(styles)(MuiVirtualizedTable);

const data = [
['Frozen yoghurt', 159, 6.0, 24, 4.0],
['Ice cream sandwich', 237, 9.0, 37, 4.3],
['Eclair', 262, 16.0, 24, 6.0],
['Cupcake', 305, 3.7, 67, 4.3],
['Gingerbread', 356, 16.0, 49, 3.9],
];

let id = 0;
function createData(dessert, calories, fat, carbs, protein) {
id += 1;
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));
}

function ReactVirtualizedTable() {
return (
<Paper style={{ height: 400, width: '100%' }}>
<WrappedVirtualizedTable
rowCount={rows.length}
rowGetter={({ index }) => rows[index]}
onRowClick={event => console.log(event)}
columns={[
{
width: 200,
flexGrow: 1.0,
label: 'Dessert',
dataKey: 'dessert',
},
{
width: 120,
label: 'Calories (g)',
dataKey: 'calories',
numeric: true,
},
{
width: 120,
label: 'Fat (g)',
dataKey: 'fat',
numeric: true,
},
{
width: 120,
label: 'Carbs (g)',
dataKey: 'carbs',
numeric: true,
},
{
width: 120,
label: 'Protein (g)',
dataKey: 'protein',
numeric: true,
},
]}
/>
</Paper>
);
}

export default ReactVirtualizedTable;
6 changes: 6 additions & 0 deletions docs/src/pages/demos/tables/tables.md
Expand Up @@ -58,6 +58,12 @@ A simple example with spanning rows & columns.

{{"demo": "pages/demos/tables/SpanningTable.js"}}

## Virtualized Table

In the following example, we demonstrate how to use [react-virtualized](https://github.com/bvaughn/react-virtualized) with the `Table` component. It renders 200 rows and can easily handle more.

{{"demo": "pages/demos/tables/ReactVirtualizedTable.js"}}

## Complementary projects

For more advanced use cases you might be able to take advantage of:
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -147,6 +147,7 @@
"react-swipeable-views": "^0.13.0",
"react-test-renderer": "^16.1.1",
"react-text-mask": "^5.0.2",
"react-virtualized": "^9.21.0",
"recast": "^0.16.0",
"recharts": "^1.1.0",
"redux": "^4.0.0",
Expand Down
7 changes: 7 additions & 0 deletions pages/demos/tables.js
Expand Up @@ -42,6 +42,13 @@ module.exports = require('fs')
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/demos/tables/CustomizedTable'), 'utf8')
`,
},
'pages/demos/tables/ReactVirtualizedTable.js': {
js: require('docs/src/pages/demos/tables/ReactVirtualizedTable').default,
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/demos/tables/ReactVirtualizedTable'), 'utf8')
`,
},
}}
Expand Down
18 changes: 15 additions & 3 deletions yarn.lock
Expand Up @@ -3931,7 +3931,7 @@ class-utils@^0.3.5:
isobject "^3.0.0"
static-extend "^0.1.1"

classnames@^2.2.5, classnames@~2.2.5:
classnames@^2.2.3, classnames@^2.2.5, classnames@~2.2.5:
version "2.2.6"
resolved "https://registry.yarnpkg.com/classnames/-/classnames-2.2.6.tgz#43935bffdd291f326dad0a205309b38d00f650ce"
integrity sha512-JR/iSQOSt+LQIWwrwEzJ9uk0xfN3mTVYMwt1Ir5mUcSN6pU+V4zQFFaJsclJbPuAUQH+yfWef6tm7l1quW3C8Q==
Expand Down Expand Up @@ -5100,7 +5100,7 @@ doctrine@^3.0.0:
dependencies:
esutils "^2.0.2"

dom-helpers@^3.2.1, dom-helpers@^3.3.1:
"dom-helpers@^2.4.0 || ^3.0.0", dom-helpers@^3.2.1, dom-helpers@^3.3.1:
version "3.4.0"
resolved "https://registry.yarnpkg.com/dom-helpers/-/dom-helpers-3.4.0.tgz#e9b369700f959f62ecde5a6babde4bccd9169af8"
integrity sha512-LnuPJ+dwqKDIyotW1VzmOZ5TONUN7CwkCR5hrgawTUbkBGYdeoNLZo6nNfGkCrjtE1nXXaj7iMMpDa8/d9WoIA==
Expand Down Expand Up @@ -8436,7 +8436,7 @@ lolex@^3.0.0:
resolved "https://registry.yarnpkg.com/lolex/-/lolex-3.0.0.tgz#f04ee1a8aa13f60f1abd7b0e8f4213ec72ec193e"
integrity sha512-hcnW80h3j2lbUfFdMArd5UPA/vxZJ+G8vobd+wg3nVEQA0EigStbYcrG030FJxL6xiDDPEkoMatV9xIh5OecQQ==

loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.2.0, loose-envify@^1.3.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
Expand Down Expand Up @@ -10766,6 +10766,18 @@ react-transition-group@^2.2.1, react-transition-group@^2.5.0:
prop-types "^15.6.2"
react-lifecycles-compat "^3.0.4"

react-virtualized@^9.21.0:
version "9.21.0"
resolved "https://registry.yarnpkg.com/react-virtualized/-/react-virtualized-9.21.0.tgz#8267c40ffb48db35b242a36dea85edcf280a6506"
integrity sha512-duKD2HvO33mqld4EtQKm9H9H0p+xce1c++2D5xn59Ma7P8VT7CprfAe5hwjd1OGkyhqzOZiTMlTal7LxjH5yBQ==
dependencies:
babel-runtime "^6.26.0"
classnames "^2.2.3"
dom-helpers "^2.4.0 || ^3.0.0"
loose-envify "^1.3.0"
prop-types "^15.6.0"
react-lifecycles-compat "^3.0.4"

react@^16.7.0-alpha.0:
version "16.7.0-alpha.2"
resolved "https://registry.yarnpkg.com/react/-/react-16.7.0-alpha.2.tgz#924f2ae843a46ea82d104a8def7a599fbf2c78ce"
Expand Down