Skip to content

Commit

Permalink
[docs] Migrate sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Aug 29, 2020
1 parent 6f88bda commit 26a4ad9
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 86 deletions.
43 changes: 27 additions & 16 deletions docs/src/pages/components/data-grid/accessibility/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,37 @@ The grid responds to keyboard interactions from the user as well as emitting eve

Use the arrow keys to move the focus.

| Keys | Description |
| -----------------------------------: | :-------------------------------------------- |
| <kbd>Tab</kbd> | Navigate between selectable elements |
| <kbd>Arrow Left</kbd> | Navigate between cell elements |
| <kbd>Arrow Bottom</kbd> | Navigate between cell elements |
| <kbd>Arrow Right</kbd> | Navigate between cell elements |
| <kbd>Arrow Up</kbd> | Navigate between cell elements |
| <kbd>Home</kbd> | Navigate to the first cell of the current row |
| <kbd>End</kbd> | Navigate to the last cell of the current row |
| <kbd>Control</kbd> + <kbd>Home</kbd> | Navigate to the first cell of the first row |
| <kbd>Control</kbd> + <kbd>End</kbd> | Navigate to the last cell of the last row |
| <kbd>Space</kbd> | Navigate to the next scrollable page |
| <kbd>Page Up</kbd> | Navigate to the next scrollable page |
| <kbd>Page Down</kbd> | Navigate to the previous scrollable page |
| Keys | Description |
| --------------------------------: | :-------------------------------------------- |
| <kbd>Tab</kbd> | Navigate between selectable elements |
| <kbd>Arrow Left</kbd> | Navigate between cell elements |
| <kbd>Arrow Bottom</kbd> | Navigate between cell elements |
| <kbd>Arrow Right</kbd> | Navigate between cell elements |
| <kbd>Arrow Up</kbd> | Navigate between cell elements |
| <kbd>Home</kbd> | Navigate to the first cell of the current row |
| <kbd>End</kbd> | Navigate to the last cell of the current row |
| <kbd>CTRL</kbd> + <kbd>Home</kbd> | Navigate to the first cell of the first row |
| <kbd>CTRL</kbd> + <kbd>End</kbd> | Navigate to the last cell of the last row |
| <kbd>Space</kbd> | Navigate to the next scrollable page |
| <kbd>Page Up</kbd> | Navigate to the next scrollable page |
| <kbd>Page Down</kbd> | Navigate to the previous scrollable page |

### Selection

| Keys | Description |
| -------------------------------------------------------------: | :------------------------------------------------ |
| <kbd>Shift</kbd> + <kbd>Space</kbd> | Select the current row |
| <kbd>Shift</kbd> + <kbd>Space</kbd> + <kbd>Arrow Up/Down</kbd> | Select the current row and the row above or below |
| <kbd>Control</kbd> + <kbd>A</kbd> | Select all rows |
| <kbd>Control</kbd> + <kbd>C</kbd> | Copy the currently selected row |
| <kbd>CTRL</kbd> + <kbd>A</kbd> | Select all rows |
| <kbd>CTRL</kbd> + <kbd>C</kbd> | Copy the currently selected row |

### Sorting

| Keys | Description |
| ----------------------: | :------------------- |
| <kbd>CTRL</kbd> + Click | Enable multi-sorting |

### Key assignment conventions

The above key assignments are defined in the context of Windows and Linux.
On macOS, replace <kbd>CTRL</kbd> with <kbd>⌘ Command</kbd>.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import { XGrid } from '@material-ui/x-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

export default function BasisPaginationGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 1000,
maxColumns: 6,
});

return (
<div style={{ height: 400, width: '100%' }}>
<XGrid pagination pageSize={200} {...data} />
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import { XGrid } from '@material-ui/x-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

export default function BasisPaginationGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 1000,
maxColumns: 6,
});

return (
<div style={{ height: 400, width: '100%' }}>
<XGrid pagination pageSize={200} {...data} />
</div>
);
}
7 changes: 7 additions & 0 deletions docs/src/pages/components/data-grid/pagination/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ We exposed a set of methods that will let you achieve all the above features usi
Below is an example on how you can reset the page using the imperative `setPage` method.

{{"demo": "pages/components/data-grid/pagination/ApiRefPaginationGrid.js"}}

## Paginate > 100 rows ✨

The `DataGrid` component can display up to 100 rows per page.
The `XGrid` component removes this limitation, the following demo displays 200 rows per page:

{{"demo": "pages/components/data-grid/pagination/200PaginationGrid.js"}}
23 changes: 23 additions & 0 deletions docs/src/pages/components/data-grid/rows/DisableSortingGrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

export default function DisableSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 1000,
maxColumns: 6,
});

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
columns={data.columns.map((column) => ({
...column,
sortable: false,
}))}
/>
</div>
);
}
23 changes: 23 additions & 0 deletions docs/src/pages/components/data-grid/rows/DisableSortingGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

export default function DisableSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 1000,
maxColumns: 6,
});

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
columns={data.columns.map((column) => ({
...column,
sortable: false,
}))}
/>
</div>
);
}
28 changes: 28 additions & 0 deletions docs/src/pages/components/data-grid/rows/MultiSortingGrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { XGrid } from '@material-ui/x-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

const sortModel = [
{
field: 'commodity',
sort: 'asc',
},
{
field: 'desk',
sort: 'desc',
},
];

export default function MultiSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 1000,
maxColumns: 6,
});

return (
<div style={{ height: 400, width: '100%' }}>
<XGrid sortModel={sortModel} {...data} />
</div>
);
}
28 changes: 28 additions & 0 deletions docs/src/pages/components/data-grid/rows/MultiSortingGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { XGrid, SortDirection } from '@material-ui/x-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

const sortModel = [
{
field: 'commodity',
sort: 'asc' as SortDirection,
},
{
field: 'desk',
sort: 'desc' as SortDirection,
},
];

export default function MultiSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 1000,
maxColumns: 6,
});

return (
<div style={{ height: 400, width: '100%' }}>
<XGrid sortModel={sortModel} {...data} />
</div>
);
}
88 changes: 88 additions & 0 deletions docs/src/pages/components/data-grid/rows/ServerSortingGrid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as React from 'react';
import { DataGrid } from '@material-ui/data-grid';
import { useDemoData } from '@material-ui/x-grid-data-generator';

function loadServerRows(sortModel, data) {
return new Promise((resolve) => {
setTimeout(() => {
if (sortModel.length === 0) {
resolve(data.rows);
return;
}

const sortedColumn = sortModel[0];

let sortedRows = [
...data.rows.sort((a, b) =>
String(a[sortedColumn.field]).localeCompare(
String(b[sortedColumn.field]),
),
),
];

if (sortModel[0].sort === 'desc') {
sortedRows = sortedRows.reverse();
}

resolve(sortedRows);
}, Math.random() * 500 + 100); // simulate network latency
});
}

export default function ServerSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 10,
maxColumns: 6,
});

const [sortModel, setSortModel] = React.useState([
{ field: 'commodity', sort: 'asc' },
]);

const [rows, setRows] = React.useState([]);
const [loading, setLoading] = React.useState(false);

const handleSortModelChange = (params) => {
if (params.sortModel !== sortModel) {
setSortModel(params.sortModel);
}
};

React.useEffect(() => {
let active = true;

(async () => {
setLoading(true);
const newRows = await loadServerRows(sortModel, data);

if (!active) {
return;
}

setRows(newRows);
setLoading(false);
})();

return () => {
active = false;
};
}, [sortModel, data]);

if (data.columns.length === 0) {
return null;
}

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={data.columns}
sortingMode="server"
sortModel={sortModel}
onSortModelChange={handleSortModelChange}
loading={loading}
/>
</div>
);
}
86 changes: 86 additions & 0 deletions docs/src/pages/components/data-grid/rows/ServerSortingGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as React from 'react';
import { RowsProp, DataGrid, SortModel } from '@material-ui/data-grid';
import { useDemoData, GridData } from '@material-ui/x-grid-data-generator';

function loadServerRows(sortModel: SortModel, data: GridData): Promise<any> {
return new Promise<any>((resolve) => {
setTimeout(() => {
if (sortModel.length === 0) {
resolve(data.rows);
return;
}

const sortedColumn = sortModel[0];

let sortedRows = [
...data.rows.sort((a, b) =>
String(a[sortedColumn.field]).localeCompare(
String(b[sortedColumn.field]),
),
),
];

if (sortModel[0].sort === 'desc') {
sortedRows = sortedRows.reverse();
}

resolve(sortedRows);
}, Math.random() * 500 + 100); // simulate network latency
});
}

export default function ServerSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 10,
maxColumns: 6,
});
const [sortModel, setSortModel] = React.useState<SortModel>([
{ field: 'commodity', sort: 'asc' },
]);
const [rows, setRows] = React.useState<RowsProp>([]);
const [loading, setLoading] = React.useState<boolean>(false);

const handleSortModelChange = (params) => {
if (params.sortModel !== sortModel) {
setSortModel(params.sortModel);
}
};

React.useEffect(() => {
let active = true;

(async () => {
setLoading(true);
const newRows = await loadServerRows(sortModel, data);

if (!active) {
return;
}

setRows(newRows);
setLoading(false);
})();

return () => {
active = false;
};
}, [sortModel, data]);

if (data.columns.length === 0) {
return null;
}

return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={data.columns}
sortingMode="server"
sortModel={sortModel}
onSortModelChange={handleSortModelChange}
loading={loading}
/>
</div>
);
}
Loading

0 comments on commit 26a4ad9

Please sign in to comment.