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] Refactor EnchancedTable demo #19560

Merged
merged 4 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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