-
Notifications
You must be signed in to change notification settings - Fork 934
/
MUIDataTableBody.js
166 lines (149 loc) · 5.13 KB
/
MUIDataTableBody.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import Table from "material-ui/Table";
import Typography from "material-ui/Typography";
import { TableBody, TableRow } from "material-ui/Table";
import MUIDataTableBodyCell from "./MUIDataTableBodyCell";
import { getStyle, DataStyles } from "./DataStyles";
const defaultBodyStyles = {
root: {},
emptyTitle: {
textAlign: "center",
},
};
const defaultBodyRowStyles = {
root: {},
responsiveStacked: {
"@media screen and (max-width: 960px)": {
border: "solid 2px rgba(0, 0, 0, 0.15)",
},
},
};
const defaultBodyCellStyles = {
root: {},
responsiveStacked: {
"@media screen and (max-width: 960px)": {
display: "block",
fontSize: "16px",
position: "relative",
paddingLeft: "calc(50% - 8px)",
"&:before": {
position: "absolute",
top: "6px",
left: "6px",
width: "calc(45% - 10px)",
paddingRight: "10px",
whiteSpace: "nowrap",
},
},
},
};
class MUIDataTableBody extends React.Component {
static propTypes = {
/** Data used to describe table */
data: PropTypes.array.isRequired,
/** Columns used to describe table */
columns: PropTypes.array.isRequired,
/** Options used to describe table */
options: PropTypes.object.isRequired,
/** Data used to filter table against */
filterList: PropTypes.array,
/** Data used to search table against */
searchText: PropTypes.string,
/** Extend the style applied to components */
classes: PropTypes.object,
};
componentDidMount() {
this.rRowStyles = this.getRowStyles();
}
buildRows() {
const { data, page, rowsPerPage } = this.props;
let rows = [];
const fromIndex = page === 0 ? 0 : page * rowsPerPage;
const toIndex = Math.min(data.length, (page + 1) * rowsPerPage);
for (let rowIndex = fromIndex; rowIndex < data.length && rowIndex < toIndex; rowIndex++) {
rows.push(data[rowIndex]);
}
return rows.length ? rows : null;
}
getRowStyles() {
const { columns, options } = this.props;
if (!options.responsive && options.responsive !== "stacked") {
return defaultBodyRowStyles;
}
let stackStyles = defaultBodyRowStyles;
const breakpoint = "@media screen and (max-width: 960px)";
columns.forEach((column, index) => {
stackStyles["responsiveStacked"][breakpoint]["& td:nth-of-type(" + (index + 1) + "):before"] = {
content: '"' + column.name + '"',
};
});
return stackStyles;
}
render() {
const { columns, options } = this.props;
const tableRows = this.buildRows();
return (
<DataStyles
defaultStyles={defaultBodyStyles}
name="MUIDataTableBody"
styles={getStyle(options, "table.body.main")}>
{bodyStyles => (
<TableBody>
<DataStyles
defaultStyles={this.rRowStyles}
name="MUIDataTableBodyRow"
styles={getStyle(options, "table.body.row")}>
{rowStyles => (
<DataStyles
defaultStyles={defaultBodyCellStyles}
name="MUIDataTableBodyCell"
styles={getStyle(options, "table.body.cell")}>
{cellStyles => {
return tableRows ? (
tableRows.map((row, rowIndex) => (
<TableRow
hover={options.rowHover ? true : false}
className={classNames({
[rowStyles.root]: true,
[rowStyles.responsiveStacked]: options.responsive === "stacked",
})}
key={rowIndex}>
{row.map(
(column, index) =>
columns[index].display ? (
<MUIDataTableBodyCell
className={classNames({
[cellStyles.root]: true,
[cellStyles.responsiveStacked]: options.responsive === "stacked",
})}
key={index}>
{column}
</MUIDataTableBodyCell>
) : (
false
),
)}
</TableRow>
))
) : (
<TableRow>
<MUIDataTableBodyCell className={cellStyles.root} colSpan={columns.length}>
<Typography type="subheading" className={bodyStyles.emptyTitle}>
Sorry, no matching records found
</Typography>
</MUIDataTableBodyCell>
</TableRow>
);
}}
</DataStyles>
)}
</DataStyles>
</TableBody>
)}
</DataStyles>
);
}
}
export default MUIDataTableBody;