|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { Pagination } from '@patternfly/react-core'; |
| 3 | +import { BrowserRouter, useSearchParams } from 'react-router-dom'; |
| 4 | +import { useDataViewFilters, useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks'; |
| 5 | +import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView'; |
| 6 | +import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable'; |
| 7 | +import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar'; |
| 8 | +import { DataViewFilters } from '@patternfly/react-data-view/dist/dynamic/DataViewFilters'; |
| 9 | +import { DataViewTextFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewTextFilter'; |
| 10 | + |
| 11 | +const perPageOptions = [ |
| 12 | + { title: '5', value: 5 }, |
| 13 | + { title: '10', value: 10 } |
| 14 | +]; |
| 15 | + |
| 16 | +interface Repository { |
| 17 | + name: string; |
| 18 | + branch: string | null; |
| 19 | + prs: string | null; |
| 20 | + workspaces: string; |
| 21 | + lastCommit: string; |
| 22 | +} |
| 23 | + |
| 24 | +interface RepositoryFilters { |
| 25 | + name: string, |
| 26 | + branch: string |
| 27 | +} |
| 28 | + |
| 29 | +const repositories: Repository[] = [ |
| 30 | + { name: 'Repository one', branch: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' }, |
| 31 | + { name: 'Repository two', branch: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' }, |
| 32 | + { name: 'Repository three', branch: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' }, |
| 33 | + { name: 'Repository four', branch: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' }, |
| 34 | + { name: 'Repository five', branch: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' }, |
| 35 | + { name: 'Repository six', branch: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' } |
| 36 | +]; |
| 37 | + |
| 38 | +const columns = [ 'Name', 'Branch', 'Pull requests', 'Workspaces', 'Last commit' ]; |
| 39 | + |
| 40 | +const ouiaId = 'LayoutExample'; |
| 41 | + |
| 42 | +const MyTable: React.FunctionComponent = () => { |
| 43 | + const [ searchParams, setSearchParams ] = useSearchParams(); |
| 44 | + const pagination = useDataViewPagination({ perPage: 5 }); |
| 45 | + const { page, perPage } = pagination; |
| 46 | + const { filters, onSetFilters, clearAllFilters } = useDataViewFilters<RepositoryFilters>({ initialFilters: { name: '', branch: '' }, searchParams, setSearchParams }); |
| 47 | + |
| 48 | + const pageRows = useMemo(() => repositories |
| 49 | + .filter(item => (!filters.name || item.name?.toLocaleLowerCase().includes(filters.name?.toLocaleLowerCase())) && (!filters.branch || item.branch?.toLocaleLowerCase().includes(filters.branch?.toLocaleLowerCase()))) |
| 50 | + .slice((page - 1) * perPage, ((page - 1) * perPage) + perPage) |
| 51 | + .map(item => Object.values(item)), [ page, perPage, filters ]); |
| 52 | + |
| 53 | + return ( |
| 54 | + <DataView> |
| 55 | + <DataViewToolbar |
| 56 | + ouiaId='LayoutExampleHeader' |
| 57 | + clearAllFilters = {clearAllFilters} |
| 58 | + pagination={ |
| 59 | + <Pagination |
| 60 | + perPageOptions={perPageOptions} |
| 61 | + itemCount={repositories.length} |
| 62 | + {...pagination} |
| 63 | + /> |
| 64 | + } |
| 65 | + filters={ |
| 66 | + <DataViewFilters onChange={(_e, values) => onSetFilters(values)} values={filters}> |
| 67 | + <DataViewTextFilter filterId="name" title='Name' placeholder='Filter by name' /> |
| 68 | + <DataViewTextFilter filterId="branch" title='Branch' placeholder='Filter by branch' /> |
| 69 | + </DataViewFilters> |
| 70 | + } |
| 71 | + /> |
| 72 | + <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} /> |
| 73 | + <DataViewToolbar |
| 74 | + ouiaId='LayoutExampleFooter' |
| 75 | + pagination={ |
| 76 | + <Pagination |
| 77 | + isCompact |
| 78 | + perPageOptions={perPageOptions} |
| 79 | + itemCount={repositories.length} |
| 80 | + {...pagination} |
| 81 | + /> |
| 82 | + } |
| 83 | + /> |
| 84 | + </DataView> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +export const BasicExample: React.FunctionComponent = () => ( |
| 89 | + <BrowserRouter> |
| 90 | + <MyTable/> |
| 91 | + </BrowserRouter> |
| 92 | +) |
0 commit comments