Skip to content

Commit

Permalink
[docs] Refactor EnchancedTable demo (#19560)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmad-reza619 authored Feb 4, 2020
1 parent 1a949cd commit 6b3e858
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
18 changes: 10 additions & 8 deletions docs/src/pages/components/tables/EnhancedTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const rows = [
createData('Oreo', 437, 18.0, 63, 4.0),
];

function desc(a, b, orderBy) {
function descendingComparator(a, b, orderBy) {
if (b[orderBy] < a[orderBy]) {
return -1;
}
Expand All @@ -51,20 +51,22 @@ function desc(a, b, orderBy) {
return 0;
}

function stableSort(array, cmp) {
function getComparator(order, orderBy) {
return order === 'desc'
? (a, b) => descendingComparator(a, b, orderBy)
: (a, b) => -descendingComparator(a, b, orderBy);
}

function stableSort(array, comparator) {
const stabilizedThis = array.map((el, index) => [el, index]);
stabilizedThis.sort((a, b) => {
const order = cmp(a[0], b[0]);
const order = comparator(a[0], b[0]);
if (order !== 0) return order;
return a[1] - b[1];
});
return stabilizedThis.map(el => el[0]);
}

function getSorting(order, orderBy) {
return order === 'desc' ? (a, b) => desc(a, b, orderBy) : (a, b) => -desc(a, b, orderBy);
}

const headCells = [
{ id: 'name', numeric: false, disablePadding: true, label: 'Dessert (100g serving)' },
{ id: 'calories', numeric: true, disablePadding: false, label: 'Calories' },
Expand Down Expand Up @@ -293,7 +295,7 @@ export default function EnhancedTable() {
rowCount={rows.length}
/>
<TableBody>
{stableSort(rows, getSorting(order, orderBy))
{stableSort(rows, getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
const isItemSelected = isSelected(row.name);
Expand Down
28 changes: 15 additions & 13 deletions docs/src/pages/components/tables/EnhancedTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const rows = [
createData('Oreo', 437, 18.0, 63, 4.0),
];

function desc<T>(a: T, b: T, orderBy: keyof T) {
function descendingComparator<T>(a: T, b: T, orderBy: keyof T) {
if (b[orderBy] < a[orderBy]) {
return -1;
}
Expand All @@ -64,25 +64,27 @@ function desc<T>(a: T, b: T, orderBy: keyof T) {
return 0;
}

function stableSort<T>(array: T[], cmp: (a: T, b: T) => number) {
type Order = 'asc' | 'desc';

function getComparator<Key extends keyof any>(
order: Order,
orderBy: Key,
): (a: { [key in Key]: number | string }, b: { [key in Key]: number | string }) => number {
return order === 'desc'
? (a, b) => descendingComparator(a, b, orderBy)
: (a, b) => -descendingComparator(a, b, orderBy);
}

function stableSort<T>(array: T[], comparator: (a: T, b: T) => number) {
const stabilizedThis = array.map((el, index) => [el, index] as [T, number]);
stabilizedThis.sort((a, b) => {
const order = cmp(a[0], b[0]);
const order = comparator(a[0], b[0]);
if (order !== 0) return order;
return a[1] - b[1];
});
return stabilizedThis.map(el => el[0]);
}

type Order = 'asc' | 'desc';

function getSorting<K extends keyof any>(
order: Order,
orderBy: K,
): (a: { [key in K]: number | string }, b: { [key in K]: number | string }) => number {
return order === 'desc' ? (a, b) => desc(a, b, orderBy) : (a, b) => -desc(a, b, orderBy);
}

interface HeadCell {
disablePadding: boolean;
id: keyof Data;
Expand Down Expand Up @@ -321,7 +323,7 @@ export default function EnhancedTable() {
rowCount={rows.length}
/>
<TableBody>
{stableSort(rows, getSorting(order, orderBy))
{stableSort(rows, getComparator(order, orderBy))
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((row, index) => {
const isItemSelected = isSelected(row.name);
Expand Down

0 comments on commit 6b3e858

Please sign in to comment.