|
1 | | -import React from 'react'; |
| 1 | +import React, { useMemo } from 'react'; |
2 | 2 | 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'; |
3 | 5 | import DataView from '@patternfly/react-data-view/dist/dynamic/DataView'; |
4 | 6 | import DataViewToolbar from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar'; |
5 | 7 |
|
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 | +] |
12 | 12 |
|
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; |
16 | 19 | } |
17 | 20 |
|
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 | + )} |
0 commit comments