Skip to content

Commit 511d0b4

Browse files
committed
feat: add example and docs for sorting hook
1 parent de2b5d5 commit 511d0b4

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

packages/module/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/mod
1616
---
1717
import { useMemo } from 'react';
1818
import { BrowserRouter, useSearchParams } from 'react-router-dom';
19-
import { useDataViewPagination, useDataViewSelection, useDataViewFilters } from '@patternfly/react-data-view/dist/dynamic/Hooks';
19+
import { useDataViewPagination, useDataViewSelection, useDataViewFilters, useDataViewSort } from '@patternfly/react-data-view/dist/dynamic/Hooks';
2020
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
2121
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
2222
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
@@ -118,3 +118,34 @@ This example demonstrates the setup and usage of filters within the data view. I
118118
```js file="./FiltersExample.tsx"
119119

120120
```
121+
122+
### Sort state
123+
124+
The `useDataViewSort` hook manages the sorting state of a data view. It provides an easy way to handle sorting logic, including synchronization with URL parameters and defining default sorting behavior.
125+
126+
**Initial values:**
127+
- `initialSort` object to set default `sortBy` and `direction` values:
128+
- `sortBy`: key of the initial column to sort.
129+
- `direction`: default sorting direction (`asc` or `desc`).
130+
- Optional `searchParams` object to manage URL-based synchronization of sort state.
131+
- Optional `setSearchParams` function to update the URL parameters when sorting changes.
132+
- `defaultDirection` to set the default direction when no direction is specified.
133+
- Customizable parameter names for the URL:
134+
- `sortByParam`: name of the URL parameter for the column key.
135+
- `directionParam`: name of the URL parameter for the sorting direction.
136+
137+
The `useDataViewSort` hook integrates seamlessly with React Router to manage sort state via URL parameters. Alternatively, you can use `URLSearchParams` and `window.history.pushState` APIs, or other routing libraries. If URL synchronization is not configured, the sort state is managed internally within the component.
138+
139+
**Return values:**
140+
- `sortBy`: key of the column currently being sorted.
141+
- `direction`: current sorting direction (`asc` or `desc`).
142+
- `onSort`: function to handle sorting changes programmatically or via user interaction.
143+
144+
### Sorting example
145+
146+
This example demonstrates how to set up and use sorting functionality within a data view. The implementation includes dynamic sorting by column with persistence of sort state in the URL using React Router.
147+
148+
149+
```js file="./SortingExample.tsx"
150+
151+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable no-nested-ternary */
2+
import React, { useMemo } from 'react';
3+
import { useDataViewSort } from '@patternfly/react-data-view/dist/dynamic/Hooks';
4+
import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
5+
import { ThProps } from '@patternfly/react-table';
6+
import { BrowserRouter, useSearchParams } from 'react-router-dom';
7+
8+
interface Repository {
9+
name: string;
10+
branches: string;
11+
prs: string;
12+
workspaces: string;
13+
lastCommit: string;
14+
};
15+
16+
const COLUMNS = [
17+
{ label: 'Repository', key: 'name', index: 0 },
18+
{ label: 'Branch', key: 'branches', index: 1 },
19+
{ label: 'Pull request', key: 'prs', index: 2 },
20+
{ label: 'Workspace', key: 'workspaces', index: 3 },
21+
{ label: 'Last commit', key: 'lastCommit', index: 4 }
22+
];
23+
24+
const repositories: Repository[] = [
25+
{ name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
26+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
27+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
28+
{ name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
29+
{ name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
30+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
31+
];
32+
33+
const sortData = (data: Repository[], sortBy: string | undefined, direction: 'asc' | 'desc' | undefined) =>
34+
sortBy && direction
35+
? [ ...data ].sort((a, b) =>
36+
direction === 'asc'
37+
? a[sortBy] < b[sortBy] ? -1 : a[sortBy] > b[sortBy] ? 1 : 0
38+
: a[sortBy] > b[sortBy] ? -1 : a[sortBy] < b[sortBy] ? 1 : 0
39+
)
40+
: data;
41+
42+
const ouiaId = 'TableExample';
43+
44+
export const MyTable: React.FunctionComponent = () => {
45+
const [ searchParams, setSearchParams ] = useSearchParams();
46+
const { sortBy, direction, onSort } = useDataViewSort({ searchParams, setSearchParams });
47+
const sortByIndex = useMemo(() => COLUMNS.findIndex(item => item.key === sortBy), [ sortBy ]);
48+
49+
const getSortParams = (columnIndex: number): ThProps['sort'] => ({
50+
sortBy: {
51+
index: sortByIndex,
52+
direction,
53+
defaultDirection: 'asc'
54+
},
55+
onSort: (_event, index, direction) => onSort(_event, COLUMNS[index].key, direction),
56+
columnIndex
57+
});
58+
59+
const columns: DataViewTh[] = COLUMNS.map((column, index) => ({
60+
cell: column.label,
61+
props: { sort: getSortParams(index) }
62+
}));
63+
64+
const rows: DataViewTr[] = useMemo(() => sortData(repositories, sortBy, direction).map(({ name, branches, prs, workspaces, lastCommit }) => [
65+
name,
66+
branches,
67+
prs,
68+
workspaces,
69+
lastCommit,
70+
]), [ sortBy, direction ]);
71+
72+
return (
73+
<DataViewTable
74+
aria-label="Repositories table"
75+
ouiaId={ouiaId}
76+
columns={columns}
77+
rows={rows}
78+
/>
79+
);
80+
};
81+
82+
export const BasicExample: React.FunctionComponent = () => (
83+
<BrowserRouter>
84+
<MyTable/>
85+
</BrowserRouter>
86+
)
87+

0 commit comments

Comments
 (0)