From 4f73f1aaa18d3cb5d58e416df6a36ac519f465f7 Mon Sep 17 00:00:00 2001 From: Minjee Son <104430030+iammminzzy@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:18:08 +0100 Subject: [PATCH] [docs] Memoize array sorting --- .../components/table/TableSortAndSelection.js | 97 ++++----- .../table/TableSortAndSelection.tsx | 97 ++++----- .../order-dashboard/components/OrderTable.tsx | 8 +- docs/src/components/showcase/FolderTable.tsx | 8 +- .../modules/components/MaterialShowcase.js | 186 +++++++++--------- 5 files changed, 210 insertions(+), 186 deletions(-) diff --git a/docs/data/joy/components/table/TableSortAndSelection.js b/docs/data/joy/components/table/TableSortAndSelection.js index c7649aa9609b6d..cbfffb636abae7 100644 --- a/docs/data/joy/components/table/TableSortAndSelection.js +++ b/docs/data/joy/components/table/TableSortAndSelection.js @@ -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'); @@ -332,53 +340,50 @@ export default function TableSortAndSelection() { rowCount={rows.length} /> - {[...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 ( - 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)', - } - : {} - } - > - - - - - {row.name} - - {row.calories} - {row.fat} - {row.carbs} - {row.protein} - - ); - })} + return ( + 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)', + } + : {} + } + > + + + + + {row.name} + + {row.calories} + {row.fat} + {row.carbs} + {row.protein} + + ); + })} {emptyRows > 0 && ( + [...rows] + .sort(getComparator(order, orderBy)) + .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage), + [order, orderBy, page, rowsPerPage], + ); + const handleRequestSort = ( event: React.MouseEvent, property: keyof Data, @@ -374,53 +382,50 @@ export default function TableSortAndSelection() { rowCount={rows.length} /> - {[...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 ( - 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) - : {} - } - > - - - - - {row.name} - - {row.calories} - {row.fat} - {row.carbs} - {row.protein} - - ); - })} + return ( + 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) + : {} + } + > + + + + + {row.name} + + {row.calories} + {row.fat} + {row.carbs} + {row.protein} + + ); + })} {emptyRows > 0 && ( ('desc'); const [selected, setSelected] = React.useState([]); const [open, setOpen] = React.useState(false); + + const sortedRows = React.useMemo( + () => [...rows].sort(getComparator(order, 'id')), + [order], + ); + const renderFilters = () => ( @@ -435,7 +441,7 @@ export default function OrderTable() { - {[...rows].sort(getComparator(order, 'id')).map((row) => ( + {sortedRows.map((row) => ( ('asc'); const [orderBy, setOrderBy] = React.useState('name'); + + const sortedRows = React.useMemo( + () => [...data].sort(getComparator(order, orderBy)), + [order, orderBy], + ); + const handleRequestSort = (event: React.MouseEvent, property: keyof Data) => { const isAsc = orderBy === property && order === 'asc'; setOrder(isAsc ? 'desc' : 'asc'); @@ -122,7 +128,7 @@ export default function BasicTable() { - {[...data].sort(getComparator(order, orderBy)).map((row) => ( + {sortedRows.map((row) => ( diff --git a/docs/src/modules/components/MaterialShowcase.js b/docs/src/modules/components/MaterialShowcase.js index df36890c140347..acbf5385b9940a 100644 --- a/docs/src/modules/components/MaterialShowcase.js +++ b/docs/src/modules/components/MaterialShowcase.js @@ -462,6 +462,11 @@ export default function Showcase() { const sortFunction = sortFunctions[sortFunctionName]; const t = useTranslate(); + const sortedApps = React.useMemo( + () => appList.filter((item) => item[sortFunctionName] !== undefined).sort(sortFunction), + [sortFunction, sortFunctionName], + ); + const handleChangeSort = (event) => { setSortFunctionName(event.target.value); }; @@ -486,100 +491,97 @@ export default function Showcase() { - {appList - .filter((item) => item[sortFunctionName] !== undefined) - .sort(sortFunction) - .map((app) => ( - - {app.image ? ( - ({ - height: '100%', - display: 'flex', - flexDirection: 'column', - p: 2, - gap: 2, - borderRadius: 1, - backgroundColor: `${alpha(theme.palette.grey[50], 0.3)}`, + {sortedApps.map((app) => ( + + {app.image ? ( + ({ + height: '100%', + display: 'flex', + flexDirection: 'column', + p: 2, + gap: 2, + borderRadius: 1, + backgroundColor: `${alpha(theme.palette.grey[50], 0.3)}`, + borderColor: 'divider', + ...theme.applyDarkStyles({ + backgroundColor: `${alpha(theme.palette.primaryDark[700], 0.2)}`, borderColor: 'divider', - ...theme.applyDarkStyles({ - backgroundColor: `${alpha(theme.palette.primaryDark[700], 0.2)}`, + }), + })} + > + + ({ + height: 'auto', + borderRadius: '6px', + bgcolor: 'currentColor', + border: '1px solid', borderColor: 'divider', - }), - })} - > - - ({ - height: 'auto', - borderRadius: '6px', - bgcolor: 'currentColor', - border: '1px solid', - borderColor: 'divider', - color: 'grey.100', - ...theme.applyDarkStyles({ - color: 'primaryDark.900', - }), - })} - /> - - - - {app.title} - {app.source ? ( - - - - ) : null} - - - {app.description} - - - - {app.dateAdded} - - - - ) : ( - - {t('visit')} - - )} - - ))} + color: 'grey.100', + ...theme.applyDarkStyles({ + color: 'primaryDark.900', + }), + })} + /> + + + + {app.title} + {app.source ? ( + + + + ) : null} + + + {app.description} + + + + {app.dateAdded} + + + + ) : ( + + {t('visit')} + + )} + + ))} );