|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { Pagination, ToggleGroup, ToggleGroupItem } from '@patternfly/react-core'; |
| 3 | +import { useDataViewPagination, useDataViewSelection } from '@patternfly/react-data-view/dist/dynamic/Hooks'; |
| 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'; |
| 7 | +import { BulkSelect, BulkSelectValue } from '@patternfly/react-component-groups'; |
| 8 | + |
| 9 | +const perPageOptions = [ |
| 10 | + { title: '5', value: 5 }, |
| 11 | + { title: '10', value: 10 } |
| 12 | +]; |
| 13 | + |
| 14 | +interface Repository { |
| 15 | + name: string; |
| 16 | + branches: string | null; |
| 17 | + prs: string | null; |
| 18 | + workspaces: string; |
| 19 | + lastCommit: string; |
| 20 | +}; |
| 21 | + |
| 22 | +const repositories: Repository[] = [ |
| 23 | + { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' }, |
| 24 | + { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' }, |
| 25 | + { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' }, |
| 26 | + { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' }, |
| 27 | + { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' }, |
| 28 | + { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' } |
| 29 | +]; |
| 30 | + |
| 31 | +const rows = repositories.map(item => Object.values(item)); |
| 32 | + |
| 33 | +const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ]; |
| 34 | + |
| 35 | +const ouiaId = 'LayoutExample'; |
| 36 | + |
| 37 | +export const BasicExample: React.FunctionComponent = () => { |
| 38 | + const pagination = useDataViewPagination({ perPage: 5 }); |
| 39 | + const selection = useDataViewSelection({ matchOption: (a, b) => a[0] === b[0] }); |
| 40 | + const [ selectedToggle, setSelectedToggle ] = useState('all') |
| 41 | + const { selected, onSelect } = selection; |
| 42 | + const { page, perPage, onSetPage } = pagination; |
| 43 | + |
| 44 | + const pageRows = useMemo(() => (selectedToggle === 'selected' ? selected : rows).slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage, selectedToggle ]); |
| 45 | + |
| 46 | + const handleBulkSelect = (value: BulkSelectValue) => { |
| 47 | + value === BulkSelectValue.none && onSelect(false); |
| 48 | + value === BulkSelectValue.all && onSelect(true, rows); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleItemClick = (event, _isSelected: boolean) => { |
| 52 | + const id = event.currentTarget.id; |
| 53 | + if (selectedToggle !== id) { |
| 54 | + setSelectedToggle(id); |
| 55 | + onSetPage(undefined, 1); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + useEffect(() => { |
| 60 | + if (selected.length === 0) { |
| 61 | + setSelectedToggle('all'); |
| 62 | + } |
| 63 | + }, [ selected ]); |
| 64 | + |
| 65 | + return ( |
| 66 | + <DataView selection={selection}> |
| 67 | + <DataViewToolbar |
| 68 | + ouiaId='DataViewHeader' |
| 69 | + bulkSelect={ |
| 70 | + <BulkSelect |
| 71 | + canSelectAll |
| 72 | + isDataPaginated={false} |
| 73 | + totalCount={repositories.length} |
| 74 | + selectedCount={selected.length} |
| 75 | + onSelect={handleBulkSelect} |
| 76 | + /> |
| 77 | + } |
| 78 | + toggleGroup={<ToggleGroup aria-label="Default with single selectable"> |
| 79 | + <ToggleGroupItem |
| 80 | + text="All" |
| 81 | + buttonId="all" |
| 82 | + isSelected={selectedToggle === 'all'} |
| 83 | + onChange={handleItemClick} |
| 84 | + /> |
| 85 | + <ToggleGroupItem |
| 86 | + isDisabled={selected.length === 0} |
| 87 | + text="Selected" |
| 88 | + buttonId="selected" |
| 89 | + isSelected={selectedToggle === 'selected'} |
| 90 | + onChange={handleItemClick} |
| 91 | + /> |
| 92 | + </ToggleGroup> |
| 93 | + } |
| 94 | + pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} |
| 95 | + /> |
| 96 | + <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} /> |
| 97 | + </DataView> |
| 98 | + ); |
| 99 | +}; |
0 commit comments