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] Memoize array sorting #42010

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
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
97 changes: 51 additions & 46 deletions docs/data/joy/components/table/TableSortAndSelection.js
Expand Up @@ -242,6 +242,14 @@ export default function TableSortAndSelection() {
const [page, setPage] = React.useState(0);
const [rowsPerPage, setRowsPerPage] = React.useState(5);

const visibleRows = React.useMemo(
() =>
[...rows]
.sort(getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage),
[order, orderBy, page, rowsPerPage],
);

const handleRequestSort = (event, property) => {
const isAsc = orderBy === property && order === 'asc';
setOrder(isAsc ? 'desc' : 'asc');
Expand Down Expand Up @@ -332,53 +340,50 @@ export default function TableSortAndSelection() {
rowCount={rows.length}
/>
<tbody>
{[...rows]
.sort(getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
const isItemSelected = isSelected(row.name);
const labelId = `enhanced-table-checkbox-${index}`;
{visibleRows.map((row, index) => {
const isItemSelected = isSelected(row.name);
const labelId = `enhanced-table-checkbox-${index}`;

return (
<tr
onClick={(event) => handleClick(event, row.name)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.name}
// selected={isItemSelected}
style={
isItemSelected
? {
'--TableCell-dataBackground':
'var(--TableCell-selectedBackground)',
'--TableCell-headBackground':
'var(--TableCell-selectedBackground)',
}
: {}
}
>
<th scope="row">
<Checkbox
checked={isItemSelected}
slotProps={{
input: {
'aria-labelledby': labelId,
},
}}
sx={{ verticalAlign: 'top' }}
/>
</th>
<th id={labelId} scope="row">
{row.name}
</th>
<td>{row.calories}</td>
<td>{row.fat}</td>
<td>{row.carbs}</td>
<td>{row.protein}</td>
</tr>
);
})}
return (
<tr
onClick={(event) => handleClick(event, row.name)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.name}
// selected={isItemSelected}
style={
isItemSelected
? {
'--TableCell-dataBackground':
'var(--TableCell-selectedBackground)',
'--TableCell-headBackground':
'var(--TableCell-selectedBackground)',
}
: {}
}
>
<th scope="row">
<Checkbox
checked={isItemSelected}
slotProps={{
input: {
'aria-labelledby': labelId,
},
}}
sx={{ verticalAlign: 'top' }}
/>
</th>
<th id={labelId} scope="row">
{row.name}
</th>
<td>{row.calories}</td>
<td>{row.fat}</td>
<td>{row.carbs}</td>
<td>{row.protein}</td>
</tr>
);
})}
{emptyRows > 0 && (
<tr
style={{
Expand Down
97 changes: 51 additions & 46 deletions docs/data/joy/components/table/TableSortAndSelection.tsx
Expand Up @@ -281,6 +281,14 @@ export default function TableSortAndSelection() {
const [page, setPage] = React.useState(0);
const [rowsPerPage, setRowsPerPage] = React.useState(5);

const visibleRows = React.useMemo(
() =>
[...rows]
.sort(getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage),
[order, orderBy, page, rowsPerPage],
);

const handleRequestSort = (
event: React.MouseEvent<unknown>,
property: keyof Data,
Expand Down Expand Up @@ -374,53 +382,50 @@ export default function TableSortAndSelection() {
rowCount={rows.length}
/>
<tbody>
{[...rows]
.sort(getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
const isItemSelected = isSelected(row.name);
const labelId = `enhanced-table-checkbox-${index}`;
{visibleRows.map((row, index) => {
const isItemSelected = isSelected(row.name);
const labelId = `enhanced-table-checkbox-${index}`;

return (
<tr
onClick={(event) => handleClick(event, row.name)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.name}
// selected={isItemSelected}
style={
isItemSelected
? ({
'--TableCell-dataBackground':
'var(--TableCell-selectedBackground)',
'--TableCell-headBackground':
'var(--TableCell-selectedBackground)',
} as React.CSSProperties)
: {}
}
>
<th scope="row">
<Checkbox
checked={isItemSelected}
slotProps={{
input: {
'aria-labelledby': labelId,
},
}}
sx={{ verticalAlign: 'top' }}
/>
</th>
<th id={labelId} scope="row">
{row.name}
</th>
<td>{row.calories}</td>
<td>{row.fat}</td>
<td>{row.carbs}</td>
<td>{row.protein}</td>
</tr>
);
})}
return (
<tr
onClick={(event) => handleClick(event, row.name)}
role="checkbox"
aria-checked={isItemSelected}
tabIndex={-1}
key={row.name}
// selected={isItemSelected}
style={
isItemSelected
? ({
'--TableCell-dataBackground':
'var(--TableCell-selectedBackground)',
'--TableCell-headBackground':
'var(--TableCell-selectedBackground)',
} as React.CSSProperties)
: {}
}
>
<th scope="row">
<Checkbox
checked={isItemSelected}
slotProps={{
input: {
'aria-labelledby': labelId,
},
}}
sx={{ verticalAlign: 'top' }}
/>
</th>
<th id={labelId} scope="row">
{row.name}
</th>
<td>{row.calories}</td>
<td>{row.fat}</td>
<td>{row.carbs}</td>
<td>{row.protein}</td>
</tr>
);
})}
{emptyRows > 0 && (
<tr
style={
Expand Down
Expand Up @@ -266,6 +266,12 @@ export default function OrderTable() {
const [order, setOrder] = React.useState<Order>('desc');
const [selected, setSelected] = React.useState<readonly string[]>([]);
const [open, setOpen] = React.useState(false);

const sortedRows = React.useMemo(
() => [...rows].sort(getComparator(order, 'id')),
[order],
);

const renderFilters = () => (
<React.Fragment>
<FormControl size="sm">
Expand Down Expand Up @@ -435,7 +441,7 @@ export default function OrderTable() {
</tr>
</thead>
<tbody>
{[...rows].sort(getComparator(order, 'id')).map((row) => (
{sortedRows.map((row) => (
<tr key={row.id}>
<td style={{ textAlign: 'center', width: 120 }}>
<Checkbox
Expand Down
8 changes: 7 additions & 1 deletion docs/src/components/showcase/FolderTable.tsx
Expand Up @@ -56,6 +56,12 @@ function formatSize(size: number) {
export default function BasicTable() {
const [order, setOrder] = React.useState<Order>('asc');
const [orderBy, setOrderBy] = React.useState<keyof Data>('name');

const sortedRows = React.useMemo(
() => [...data].sort(getComparator(order, orderBy)),
[order, orderBy],
);

const handleRequestSort = (event: React.MouseEvent<unknown>, property: keyof Data) => {
const isAsc = orderBy === property && order === 'asc';
setOrder(isAsc ? 'desc' : 'asc');
Expand Down Expand Up @@ -122,7 +128,7 @@ export default function BasicTable() {
</TableRow>
</TableHead>
<TableBody>
{[...data].sort(getComparator(order, orderBy)).map((row) => (
{sortedRows.map((row) => (
<TableRow key={row.name} sx={{ '&:last-child td, &:last-child th': { border: 0 } }}>
<TableCell component="th" scope="row">
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
Expand Down