Skip to content

Commit deeb4bf

Browse files
committed
feat(table): Display a simple table in DataView
1 parent 20d290c commit deeb4bf

File tree

6 files changed

+89
-36
lines changed

6 files changed

+89
-36
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ source: react
1313
propComponents: ['DataView']
1414
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/DataView/DataView.md
1515
---
16-
import { Pagination } from '@patternfly/react-core';
16+
import { useMemo } from 'react';
17+
import { useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
1718
import DataView from '@patternfly/react-data-view/dist/dynamic/DataView';
1819
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
1920

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import { Pagination } from '@patternfly/react-core';
3+
import { Table, Tbody, Th, Thead, Tr, Td } from '@patternfly/react-table';
4+
import { useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
35
import DataView from '@patternfly/react-data-view/dist/dynamic/DataView';
46
import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
57

6-
const layoutItemStyling = {
7-
width: '100%',
8-
height: '5rem',
9-
padding: 'var(--pf-t--global--spacer--md)',
10-
border: 'var(--pf-t--global--border--width--box--default) dashed var(--pf-t--global--border--color--default)'
11-
};
8+
const perPageOptions = [
9+
{ title: '5', value: 5 },
10+
{ title: '10', value: 10 }
11+
]
1212

13-
const PAGINATION = {
14-
page: 1,
15-
perPage: 1
13+
interface Repository {
14+
name: string;
15+
branches: string | null;
16+
prs: string | null;
17+
workspaces: string;
18+
lastCommit: string;
1619
}
1720

18-
export const BasicExample: React.FunctionComponent = () => (
19-
<DataView>
20-
<DataViewToolbar pagination={<Pagination {...PAGINATION} />} />
21-
<div style={layoutItemStyling}>Data representation</div>
22-
<DataViewToolbar pagination={<Pagination isCompact {...PAGINATION} ouiaId="DataViewFooter" />} />
23-
</DataView>
24-
)
21+
const repositories: Repository[] = [
22+
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
23+
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
24+
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
25+
{ name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
26+
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
27+
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
28+
];
29+
30+
const cols = {
31+
name: 'Repositories',
32+
branches: 'Branches',
33+
prs: 'Pull requests',
34+
workspaces: 'Workspaces',
35+
lastCommit: 'Last commit'
36+
};
37+
38+
const ouiaId = 'LayoutExample';
39+
40+
export const BasicExample: React.FunctionComponent = () => {
41+
const pagination = useDataViewPagination({ perPage: 5 });
42+
const { page, perPage } = pagination;
43+
44+
const data = useMemo(() => repositories.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage ]);
45+
46+
return (
47+
<DataView>
48+
<DataViewToolbar pagination={<Pagination ouiaId={`${ouiaId}Header-pagination`} perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
49+
<Table aria-label="Repositories table" ouiaId={ouiaId}>
50+
<Thead data-ouia-component-id={`${ouiaId}-thead`}>
51+
<Tr ouiaId={`${ouiaId}-tr-head`}>
52+
{Object.values(cols).map((column, index) => <Th key={index} data-ouia-component-id={`${ouiaId}-th-${index}`}>{column}</Th>)}
53+
</Tr>
54+
</Thead>
55+
<Tbody>
56+
{data.map((repo, rowIndex) => (
57+
<Tr key={repo.name} ouiaId={`${ouiaId}-tr-${rowIndex}`}>
58+
{Object.keys(cols).map((column, colIndex) => <Td key={colIndex} data-ouia-component-id={`${ouiaId}-td-${rowIndex}-${colIndex}`}>{repo[column]}</Td>)}
59+
</Tr>
60+
))}
61+
</Tbody>
62+
</Table>
63+
<DataViewToolbar pagination={<Pagination isCompact ouiaId={`${ouiaId}Footer-pagination`} perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} />
64+
</DataView>
65+
)}

packages/module/src/DataView/DataView.test.tsx

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/module/src/Hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './useDataViewPagination';
1+
export * from './pagination';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useState } from "react";
2+
3+
export interface UseDataViewPaginationProps {
4+
page?: number;
5+
perPage: number;
6+
}
7+
8+
export interface DataViewPaginationProps extends UseDataViewPaginationProps {
9+
page: number;
10+
}
11+
12+
export const useDataViewPagination = ({ page = 1, perPage }: UseDataViewPaginationProps) => {
13+
const [ state, setState ] = useState({ page, perPage });
14+
15+
const onPerPageSelect = (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPerPage: number) => {
16+
setState(prev => ({ ...prev, perPage: newPerPage }));
17+
}
18+
19+
const onSetPage = (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newPage: number) => {
20+
setState(prev => ({ ...prev, page: newPage }));
21+
}
22+
23+
return {
24+
...state,
25+
onPerPageSelect,
26+
onSetPage
27+
}
28+
}

packages/module/src/Hooks/useDataViewPagination.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)