Skip to content

Commit d8900b3

Browse files
authored
Merge pull request #14 from fhlavac/table-renderer
2 parents 37e31c1 + 50f46b1 commit d8900b3

29 files changed

+926
-328
lines changed

cypress/component/DataView.cy.tsx

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import { Pagination } from '@patternfly/react-core';
3-
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
43
import { BulkSelect } from '@patternfly/react-component-groups/dist/dynamic/BulkSelect';
5-
import DataView from '../../packages/module/dist/dynamic/DataView';
6-
import DataViewToolbar from '../../packages/module/dist/dynamic/DataViewToolbar';
4+
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
5+
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
6+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
77

88
interface Repository {
99
name: string;
@@ -26,14 +26,9 @@ const repositories: Repository[] = [
2626
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
2727
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
2828
];
29+
const rows = repositories.map(item => Object.values(item));
2930

30-
const cols: Record<keyof Repository, string> = {
31-
name: 'Repositories',
32-
branches: 'Branches',
33-
prs: 'Pull requests',
34-
workspaces: 'Workspaces',
35-
lastCommit: 'Last commit'
36-
};
31+
const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
3732

3833
describe('DataView', () => {
3934
it('renders the data view layout', () => {
@@ -61,24 +56,7 @@ describe('DataView', () => {
6156
/>
6257
}
6358
/>
64-
<Table aria-label="Repositories table" ouiaId={ouiaId}>
65-
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
66-
<Tr ouiaId={`${ouiaId}-tr-head`}>
67-
{Object.values(cols).map((column, index) => <Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>)}
68-
</Tr>
69-
</Thead>
70-
<Tbody>
71-
{repositories.map((repo, rowIndex) => (
72-
<Tr key={repo.name}>
73-
<Td data-ouia-component-id={`${ouiaId}-td-${rowIndex}-name`} dataLabel={cols.name}>{repo.name}</Td>
74-
<Td data-ouia-component-id={`${ouiaId}-td-${rowIndex}-branches`} dataLabel={cols.branches}>{repo.branches}</Td>
75-
<Td data-ouia-component-id={`${ouiaId}-td-${rowIndex}-prs`} dataLabel={cols.prs}>{repo.prs}</Td>
76-
<Td data-ouia-component-id={`${ouiaId}-td-${rowIndex}-workspaces`} dataLabel={cols.workspaces}>{repo.workspaces}</Td>
77-
<Td data-ouia-component-id={`${ouiaId}-td-${rowIndex}-last-commit`} dataLabel={cols.lastCommit}>{repo.lastCommit}</Td>
78-
</Tr>
79-
))}
80-
</Tbody>
81-
</Table>
59+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
8260
<DataViewToolbar ouiaId="DataViewFooter" pagination={<Pagination isCompact {...PAGINATION} />} />
8361
</DataView>
8462
);
@@ -91,11 +69,11 @@ describe('DataView', () => {
9169
cy.get('[data-ouia-component-id="data-th-3"]').contains('Workspaces');
9270
cy.get('[data-ouia-component-id="data-th-4"]').contains('Last commit');
9371

94-
cy.get('[data-ouia-component-id="data-td-0-name"]').contains('one');
95-
cy.get('[data-ouia-component-id="data-td-2-branches"]').contains('two - 3');
96-
cy.get('[data-ouia-component-id="data-td-3-prs"]').contains('null');
97-
cy.get('[data-ouia-component-id="data-td-4-workspaces"]').contains('four - 5');
98-
cy.get('[data-ouia-component-id="data-td-5-last-commit"]').contains('five - 6');
72+
cy.get('[data-ouia-component-id="data-td-0-0"]').contains('one');
73+
cy.get('[data-ouia-component-id="data-td-2-1"]').contains('two - 3');
74+
cy.get('[data-ouia-component-id="data-td-3-2"]').contains('null');
75+
cy.get('[data-ouia-component-id="data-td-4-3"]').contains('four - 5');
76+
cy.get('[data-ouia-component-id="data-td-5-4"]').contains('five - 6');
9977

10078
cy.get('[data-ouia-component-id="DataViewFooter-pagination"]').should('exist');
10179
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
3+
4+
interface Repository {
5+
name: string;
6+
branches: string | null;
7+
prs: string | null;
8+
workspaces: string;
9+
lastCommit: string;
10+
}
11+
12+
const repositories: Repository[] = [
13+
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
14+
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
15+
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
16+
{ name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
17+
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
18+
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
19+
];
20+
const rows = repositories.map(item => Object.values(item));
21+
22+
const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
23+
24+
describe('DataViewTable', () => {
25+
26+
it('renders the data view table', () => {
27+
const ouiaId = 'data';
28+
29+
cy.mount(
30+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
31+
);
32+
33+
cy.get('[data-ouia-component-id="data-th-0"]').contains('Repositories');
34+
cy.get('[data-ouia-component-id="data-th-1"]').contains('Branches');
35+
cy.get('[data-ouia-component-id="data-th-2"]').contains('Pull requests');
36+
cy.get('[data-ouia-component-id="data-th-3"]').contains('Workspaces');
37+
cy.get('[data-ouia-component-id="data-th-4"]').contains('Last commit');
38+
39+
cy.get('[data-ouia-component-id="data-td-0-0"]').contains('one');
40+
cy.get('[data-ouia-component-id="data-td-2-1"]').contains('two - 3');
41+
cy.get('[data-ouia-component-id="data-td-3-2"]').contains('null');
42+
cy.get('[data-ouia-component-id="data-td-4-3"]').contains('four - 5');
43+
cy.get('[data-ouia-component-id="data-td-5-4"]').contains('five - 6');
44+
});
45+
});

cypress/e2e/DataViewEvents.spec.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ describe('Test the Data view docs page', () => {
33
it('displays a table and opens detail', () => {
44
const ouiaId = 'ContextExample';
55

6-
cy.visit('http://localhost:8006/extensions/data-view/context');
6+
cy.visit('http://localhost:8006/extensions/data-view/events-context');
77

88
cy.get(`[data-ouia-component-id="${ouiaId}-th-0"]`).contains('Repositories');
99
cy.get(`[data-ouia-component-id="${ouiaId}-th-4"]`).contains('Last commit');

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,51 @@ 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
1313
sortValue: 4
14-
propComponents: ['DataViewToolbar']
14+
propComponents: ['DataViewToolbar', 'DataViewTable']
1515
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Components/Components.md
1616
---
1717
import { BulkSelect } from '@patternfly/react-component-groups';
18-
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
18+
import { ExclamationCircleIcon } from '@patternfly/react-icons';
19+
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
20+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
1921

2022
## Data view toolbar
2123

2224
The **data view toolbar** component renders a default opinionated data view toolbar above or below the data section.
2325

2426
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.
2527

26-
### Basic example
28+
### Basic toolbar example
2729

2830
```js file="./DataViewToolbarExample.tsx"
2931

3032
```
3133

34+
## Data view table
35+
36+
The **data view table** component renders your columns and rows definition into a [table](/components/table) component.
37+
38+
### Rows and columns customization
39+
40+
This example shows possible formats of `rows` and `columns` passed to the `DataViewTable` which allow you various customizations of the table header and body.
41+
42+
```js file="./DataViewTableExample.tsx"
43+
44+
```
45+
46+
The `DataViewTable` component accepts the following props:
47+
48+
- `columns` defining the column headers of the table. Each item in the array can be a `ReactNode` (for simple headers) or an object with the following properties:
49+
- `cell` (`ReactNode`) content to display in the column header.
50+
- optional `props` (`ThProps`) to pass to the `<Th>` component, such as `width`, `sort`, and other table header cell properties.
51+
52+
- `rows` defining the rows to be displayed in the table. Each item in the array can be either an array of `DataViewTd` (for simple rows) or an object with the following properties:
53+
- `row` (`DataViewTd[]`) defining the content for each cell in the row.
54+
- optional `id` (`string`) for the row (can be used to match items in selection).
55+
- optional `props` (`TrProps`) to pass to the `<Tr>` component, such as `isHoverable`, `isRowSelected`, and other table row properties.
56+
57+
- optional `ouiaId`
58+
59+
- optional `props` (`TableProps`) that are passed down to the `<Table>` component, except for `onSelect`, which is managed internally.
60+
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React from 'react';
2+
import { DataViewTable, DataViewTh, DataViewTr } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
3+
import { ExclamationCircleIcon } from '@patternfly/react-icons';
4+
import { Button } from '@patternfly/react-core';
5+
6+
interface Repository {
7+
id: number;
8+
name: string;
9+
branches: string | null;
10+
prs: string | null;
11+
workspaces: string;
12+
lastCommit: string;
13+
}
14+
15+
const repositories: Repository[] = [
16+
{ id: 1, name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
17+
{ id: 2, name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
18+
{ id: 3, name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
19+
{ id: 4, name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
20+
{ id: 5, name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
21+
{ id: 6, name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
22+
];
23+
24+
// you can also pass props to Tr by returning { row: DataViewTd[], props: TrProps } }
25+
const rows: DataViewTr[] = repositories.map(({ id, name, branches, prs, workspaces, lastCommit }) => [
26+
{ id, cell: workspaces, props: { favorites: { isFavorited: true } } },
27+
{ cell: <Button href='#' variant='link' isInline>{name}</Button> },
28+
branches,
29+
prs,
30+
workspaces,
31+
lastCommit
32+
]);
33+
34+
const columns: DataViewTh[] = [
35+
null,
36+
'Repositories',
37+
{ cell: <>Branches<ExclamationCircleIcon className='pf-v5-u-ml-sm' color='var(--pf-v5-global--danger-color--100)'/></> },
38+
'Pull requests',
39+
{ cell: 'Workspaces', props: { info: { tooltip: 'More information' } } },
40+
{ cell: 'Last commit', props: { sort: { sortBy: {}, columnIndex: 4 } } }
41+
];
42+
43+
const ouiaId = 'TableExample';
44+
45+
export const BasicExample: React.FunctionComponent = () => (
46+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
47+
);

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

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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: Events context
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/EventsContext/EventsContext.md
15+
---
16+
import { useState, useEffect, useRef, useMemo } from 'react';
17+
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
18+
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
19+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
20+
import { useDataViewEventsContext, DataViewEventsContext, DataViewEventsProvider, EventTypes } from '@patternfly/react-data-view/dist/dynamic/DataViewEventsContext';
21+
import { Drawer, DrawerContent, DrawerContentBody } from '@patternfly/react-core';
22+
23+
The **data view events context** provides a way of listening to the data view events from the outside of the component.
24+
25+
### Row click subscription example
26+
The following example demonstrates how to use the `DataViewEventsContext` to manage shared state and handle events. The `DataViewEventsProvider` is used to wrap components that need access to the shared context. This example illustrates how to set up a layout that listens for data view row click events and displays detailed information about the selected row in a [drawer component](/components/drawer).
27+
28+
29+
```js file="./EventsExample.tsx"
30+
31+
```
32+

0 commit comments

Comments
 (0)