Skip to content

Commit 48954b2

Browse files
charlaxoliviertassinari
authored andcommitted
[Table] Derive sorted rows from state at render time in demo (#11828)
* [Table] Derive sorted rows from state at render time in demo * Run prettier * Fix comparison * function
1 parent 258ceeb commit 48954b2

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

docs/src/pages/demos/tables/EnhancedTable.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function createData(name, calories, fat, carbs, protein) {
2525
return { id: counter, name, calories, fat, carbs, protein };
2626
}
2727

28+
function getSorting(order, orderBy) {
29+
return order === 'desc'
30+
? (a, b) => (b[orderBy] < a[orderBy] ? -1 : 1)
31+
: (a, b) => (a[orderBy] < b[orderBy] ? -1 : 1);
32+
}
33+
2834
const columnData = [
2935
{ id: 'name', numeric: false, disablePadding: true, label: 'Dessert (100g serving)' },
3036
{ id: 'calories', numeric: true, disablePadding: false, label: 'Calories' },
@@ -197,7 +203,7 @@ class EnhancedTable extends React.Component {
197203
createData('Marshmallow', 318, 0, 81, 2.0),
198204
createData('Nougat', 360, 19.0, 9, 37.0),
199205
createData('Oreo', 437, 18.0, 63, 4.0),
200-
].sort((a, b) => (a.calories < b.calories ? -1 : 1)),
206+
],
201207
page: 0,
202208
rowsPerPage: 5,
203209
};
@@ -211,12 +217,7 @@ class EnhancedTable extends React.Component {
211217
order = 'asc';
212218
}
213219

214-
const data =
215-
order === 'desc'
216-
? this.state.data.sort((a, b) => (b[orderBy] < a[orderBy] ? -1 : 1))
217-
: this.state.data.sort((a, b) => (a[orderBy] < b[orderBy] ? -1 : 1));
218-
219-
this.setState({ data, order, orderBy });
220+
this.setState({ order, orderBy });
220221
};
221222

222223
handleSelectAllClick = (event, checked) => {
@@ -277,31 +278,34 @@ class EnhancedTable extends React.Component {
277278
rowCount={data.length}
278279
/>
279280
<TableBody>
280-
{data.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map(n => {
281-
const isSelected = this.isSelected(n.id);
282-
return (
283-
<TableRow
284-
hover
285-
onClick={event => this.handleClick(event, n.id)}
286-
role="checkbox"
287-
aria-checked={isSelected}
288-
tabIndex={-1}
289-
key={n.id}
290-
selected={isSelected}
291-
>
292-
<TableCell padding="checkbox">
293-
<Checkbox checked={isSelected} />
294-
</TableCell>
295-
<TableCell component="th" scope="row" padding="none">
296-
{n.name}
297-
</TableCell>
298-
<TableCell numeric>{n.calories}</TableCell>
299-
<TableCell numeric>{n.fat}</TableCell>
300-
<TableCell numeric>{n.carbs}</TableCell>
301-
<TableCell numeric>{n.protein}</TableCell>
302-
</TableRow>
303-
);
304-
})}
281+
{data
282+
.sort(getSorting(order, orderBy))
283+
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
284+
.map(n => {
285+
const isSelected = this.isSelected(n.id);
286+
return (
287+
<TableRow
288+
hover
289+
onClick={event => this.handleClick(event, n.id)}
290+
role="checkbox"
291+
aria-checked={isSelected}
292+
tabIndex={-1}
293+
key={n.id}
294+
selected={isSelected}
295+
>
296+
<TableCell padding="checkbox">
297+
<Checkbox checked={isSelected} />
298+
</TableCell>
299+
<TableCell component="th" scope="row" padding="none">
300+
{n.name}
301+
</TableCell>
302+
<TableCell numeric>{n.calories}</TableCell>
303+
<TableCell numeric>{n.fat}</TableCell>
304+
<TableCell numeric>{n.carbs}</TableCell>
305+
<TableCell numeric>{n.protein}</TableCell>
306+
</TableRow>
307+
);
308+
})}
305309
{emptyRows > 0 && (
306310
<TableRow style={{ height: 49 * emptyRows }}>
307311
<TableCell colSpan={6} />

0 commit comments

Comments
 (0)