Skip to content

Commit 4269ced

Browse files
committed
feat(selection): Add Selection to DataView
1 parent 67ba475 commit 4269ced

File tree

17 files changed

+398
-131
lines changed

17 files changed

+398
-131
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@ section: extensions
55
subsection: Data view
66
# Sidenav secondary level section
77
# should be the same for all markdown files
8-
id: Data view toolbar
8+
id: Components
99
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
1010
source: react
1111
# If you use typescript, the name of the interface to display props for
1212
# These are found through the sourceProps function provided in patternfly-docs.source.js
13+
sortValue: 4
1314
propComponents: ['DataViewToolbar']
14-
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/DataViewToolbar/DataViewToolbar.md
15+
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Components/Components.md
1516
---
17+
import { BulkSelect } from '@patternfly/react-component-groups';
1618
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
1719

18-
The **data view toolbar** component renders a default opinionated data view toolbar.
20+
## Data view toolbar
1921

20-
### Basic example
22+
The **data view toolbar** component renders a default opinionated data view toolbar above or below the data section.
23+
24+
Data view toolbar can contain a `pagination`, `bulkSelect` or any other children content passed. The preffered way of passing children toolbar items is using the [toolbar item](/components/toolbar#toolbar-items) component.
2125

22-
Data view toolbar can display a pagination using the `pagination` property accepting a React node. You can also pass a custom `ouiaId` for testing purposes.
26+
### Basic example
2327

2428
```js file="./DataViewToolbarExample.tsx"
2529

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import { Pagination } from '@patternfly/react-core';
3+
import { BulkSelect } from '@patternfly/react-component-groups';
4+
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
5+
6+
7+
export const BasicExample: React.FunctionComponent = () => (
8+
<DataViewToolbar
9+
pagination={
10+
<Pagination page={1} perPage={10} />
11+
}
12+
bulkSelect={
13+
<BulkSelect
14+
selectedCount={0}
15+
pageCount={5}
16+
onSelect={() => null}
17+
/>
18+
}
19+
/>
20+
)

packages/module/patternfly-docs/content/extensions/data-view/examples/DataViewToolbar/DataViewToolbarExample.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
# Sidenav top-level section
3+
# should be the same for all markdown files
4+
section: extensions
5+
subsection: Data view
6+
# Sidenav secondary level section
7+
# should be the same for all markdown files
8+
id: Functionality
9+
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
10+
source: react
11+
# If you use typescript, the name of the interface to display props for
12+
# These are found through the sourceProps function provided in patternfly-docs.source.js
13+
sortValue: 3
14+
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md
15+
---
16+
import { useMemo } from 'react';
17+
import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
18+
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
19+
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
20+
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
21+
22+
This is a list of functionality you can use to manage data displayed in the **data view**.
23+
24+
# Pagination
25+
Allows to display data records on multiple pages and display the pagination state.
26+
27+
### Toolbar usage
28+
Data view toolbar can display a pagination using the `pagination` property accepting a React node. You can also pass a custom `ouiaId` for testing purposes.
29+
30+
### Pagination state
31+
32+
The `useDataViewPagination` hook manages the pagination state of the data view.
33+
34+
**Initial values:**
35+
- `perPage` initial value
36+
- (optional) `page` initial value
37+
38+
The retrieved values are named to match the PatternFly [pagination](/components/pagination) component props, so you can easily spread them.
39+
40+
**Return values:**
41+
- current `page` number
42+
- `onSetPage` to modify current page
43+
- items `perPage` value
44+
- `onPerPageSelect` to modify per page value
45+
46+
### Pagination example
47+
```js file="./PaginationExample.tsx"
48+
49+
```
50+
51+
# Selection
52+
Allows to select data records inside the data view and show the selection state.
53+
54+
### Toolbar usage
55+
Data view toolbar can display a bulk selection component using the `bulkSelect` property accepting a React node. You can make use of a predefined [bulk select](/extensions/component-groups/bulk-select) from the [component groups](/extensions/component-groups/about-component-groups) extension.
56+
57+
### Selection state
58+
59+
The `useDataViewSelection` hook manages the selection state of the data view.
60+
61+
**Initial values:**
62+
- (optional) `initialSelected` array of record's identifiers selected by default
63+
- (optional) `matchOption` function to check if given record is selected
64+
65+
*When no `matchOption` is passed, the `Array.prototype.includes()` operation is performed on the `selected` array.*
66+
67+
**Return values:**
68+
- `selected` array of currently selected records
69+
- `isSelected` function returning the selection state for given record
70+
- `onSelect` callback to modify the selection state and accepting `isSelecting` flag indicating if records are changing to selected or deselected and `items` containing affected records
71+
72+
### Selection example
73+
74+
```js file="./SelectionExample.tsx"
75+
76+
```
77+
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const BasicExample: React.FunctionComponent = () => {
4545

4646
return (
4747
<DataView>
48-
<DataViewToolbar pagination={<Pagination ouiaId={`${ouiaId}Header-pagination`} perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
48+
<DataViewToolbar ouiaId='DataViewHeader' pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
4949
<Table aria-label="Repositories table" ouiaId={ouiaId}>
5050
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
5151
<Tr ouiaId={`${ouiaId}-tr-head`}>
@@ -60,6 +60,6 @@ export const BasicExample: React.FunctionComponent = () => {
6060
))}
6161
</Tbody>
6262
</Table>
63-
<DataViewToolbar pagination={<Pagination isCompact ouiaId={`${ouiaId}Footer-pagination`} perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
63+
<DataViewToolbar ouiaId='DataViewFooter' pagination={<Pagination isCompact perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
6464
</DataView>
6565
)}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React from 'react';
2+
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
3+
import { useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
4+
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
5+
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
6+
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
7+
8+
interface Repository {
9+
name: string;
10+
branches: string | null;
11+
prs: string | null;
12+
workspaces: string;
13+
lastCommit: string;
14+
}
15+
16+
const repositories: Repository[] = [
17+
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
18+
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
19+
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
20+
{ name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
21+
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
22+
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
23+
];
24+
25+
const cols = {
26+
name: 'Repositories',
27+
branches: 'Branches',
28+
prs: 'Pull requests',
29+
workspaces: 'Workspaces',
30+
lastCommit: 'Last commit'
31+
};
32+
33+
const ouiaId = 'LayoutExample';
34+
35+
export const BasicExample: React.FunctionComponent = () => {
36+
const selection = useDataViewSelection({});
37+
const { selected, onSelect, isSelected } = selection;
38+
39+
const handleBulkSelect = (value: BulkSelectValue) => {
40+
value === BulkSelectValue.none && onSelect(false);
41+
value === BulkSelectValue.all && onSelect(true, repositories);
42+
};
43+
44+
return (
45+
<DataView>
46+
<DataViewToolbar
47+
ouiaId='DataViewHeader'
48+
bulkSelect={
49+
<BulkSelect
50+
canSelectAll
51+
isDataPaginated={false}
52+
totalCount={repositories.length}
53+
selectedCount={selected.length}
54+
onSelect={handleBulkSelect}
55+
/>
56+
}
57+
/>
58+
<Table aria-label="Repositories table" ouiaId={ouiaId}>
59+
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
60+
<Tr ouiaId={`${ouiaId}-tr-head`}>
61+
<Th key="row-select"/>
62+
{Object.values(cols).map((column, index) => (
63+
<Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>
64+
))}
65+
</Tr>
66+
</Thead>
67+
<Tbody>
68+
{repositories.map((repo, rowIndex) => (
69+
<Tr key={repo.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
70+
<Td
71+
key={`select-${rowIndex}`}
72+
select={{
73+
rowIndex,
74+
onSelect: (_event, isSelecting) => onSelect(isSelecting, repo),
75+
isSelected: isSelected(repo),
76+
isDisabled: false
77+
}}
78+
/>
79+
{Object.keys(cols).map((column, colIndex) => (
80+
<Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>{repo[column]}</Td>
81+
))}
82+
</Tr>
83+
))}
84+
</Tbody>
85+
</Table>
86+
</DataView>
87+
);
88+
}

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

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ section: extensions
55
subsection: Data view
66
# Sidenav secondary level section
77
# should be the same for all markdown files
8-
id: Data view layout
8+
id: Layout
99
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility)
1010
source: react
1111
# If you use typescript, the name of the interface to display props for
1212
# These are found through the sourceProps function provided in patternfly-docs.source.js
13+
sortValue: 2
1314
propComponents: ['DataView']
14-
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/DataView/DataView.md
15+
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Layout/Layout.md
1516
---
1617
import { useMemo } from 'react';
17-
import { useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
18+
import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks';
19+
import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
1820
import DataView from '@patternfly/react-data-view/dist/dynamic/DataView';
1921
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
2022

@@ -24,14 +26,14 @@ The **data view** component renders record data in a configured layout.
2426

2527
Data view is expected to consist of header, data part and footer stacked below each other and passed as `children`.
2628

27-
```js file="./DataViewLayoutExample.tsx"
29+
```js file="./AbstractLayoutExample.tsx"
2830

2931
```
3032

3133
### Predefined layout components
3234

33-
You can make use of the predefined layout components to display a default header and footer. See [data view toolbar](/data-view/data-view-toolbar) for more information
35+
You can make use of the predefined layout components to display a default header and footer. See [data view toolbar](/extensions/data-view/components#dataviewtoolbar) for more information
3436

35-
```js file="./DataViewPredefinedLayoutExample.tsx"
37+
```js file="./PredefinedLayoutExample.tsx"
3638

3739
```

0 commit comments

Comments
 (0)