|
| 1 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 2 | +import { Pagination, ToggleGroup, ToggleGroupItem, Tooltip } 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 TOGGLE_ALL = 'all'; |
| 10 | +const TOGGLE_SELECTED = 'selected'; |
| 11 | + |
| 12 | +const perPageOptions = [ |
| 13 | + { title: '5', value: 5 }, |
| 14 | + { title: '10', value: 10 } |
| 15 | +]; |
| 16 | + |
| 17 | +interface Repository { |
| 18 | + name: string; |
| 19 | + branches: string | null; |
| 20 | + prs: string | null; |
| 21 | + workspaces: string; |
| 22 | + lastCommit: string; |
| 23 | +}; |
| 24 | + |
| 25 | +const repositories: Repository[] = [ |
| 26 | + { name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' }, |
| 27 | + { name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' }, |
| 28 | + { name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' }, |
| 29 | + { name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' }, |
| 30 | + { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' }, |
| 31 | + { name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' } |
| 32 | +]; |
| 33 | + |
| 34 | +const rows = repositories.map(item => Object.values(item)); |
| 35 | + |
| 36 | +const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ]; |
| 37 | + |
| 38 | +const ouiaId = 'LayoutExample'; |
| 39 | + |
| 40 | +export const BasicExample: React.FunctionComponent = () => { |
| 41 | + const pagination = useDataViewPagination({ perPage: 5 }); |
| 42 | + const selection = useDataViewSelection({ matchOption: (a, b) => a[0] === b[0] }); |
| 43 | + const [ selectedToggle, setSelectedToggle ] = useState(TOGGLE_ALL); |
| 44 | + const { selected, onSelect } = selection; |
| 45 | + const { page, perPage, onSetPage } = pagination; |
| 46 | + |
| 47 | + const pageRows = useMemo(() => (selectedToggle === TOGGLE_SELECTED ? selected : rows).slice((page - 1) * perPage, ((page - 1) * perPage) + perPage), [ page, perPage, selectedToggle, selected ]); |
| 48 | + |
| 49 | + const handleBulkSelect = (value: BulkSelectValue) => { |
| 50 | + value === BulkSelectValue.none && onSelect(false); |
| 51 | + value === BulkSelectValue.all && onSelect(true, rows); |
| 52 | + }; |
| 53 | + |
| 54 | + const handleItemClick = (event: React.MouseEvent<any> | React.KeyboardEvent | MouseEvent, _isSelected: boolean) => { |
| 55 | + const id = event.currentTarget.id; |
| 56 | + |
| 57 | + if (id === TOGGLE_SELECTED && selected.length === 0) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (selectedToggle !== id) { |
| 62 | + setSelectedToggle(id); |
| 63 | + onSetPage(undefined, 1); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + if (selected.length === 0) { |
| 69 | + setSelectedToggle(TOGGLE_ALL); |
| 70 | + } |
| 71 | + }, [ selected ]); |
| 72 | + |
| 73 | + return ( |
| 74 | + <DataView selection={selection}> |
| 75 | + <DataViewToolbar |
| 76 | + ouiaId='DataViewHeader' |
| 77 | + bulkSelect={ |
| 78 | + <BulkSelect |
| 79 | + canSelectAll |
| 80 | + isDataPaginated={false} |
| 81 | + totalCount={repositories.length} |
| 82 | + selectedCount={selected.length} |
| 83 | + onSelect={handleBulkSelect} |
| 84 | + /> |
| 85 | + } |
| 86 | + toggleGroup={ |
| 87 | + <ToggleGroup aria-label="Toggle group to switch between all / selected table rows"> |
| 88 | + <ToggleGroupItem |
| 89 | + text="All" |
| 90 | + buttonId={TOGGLE_ALL} |
| 91 | + isSelected={selectedToggle === TOGGLE_ALL} |
| 92 | + onChange={handleItemClick} |
| 93 | + /> |
| 94 | + <ToggleGroupItem |
| 95 | + id="selected-row-switch" |
| 96 | + text="Selected" |
| 97 | + buttonId={TOGGLE_SELECTED} |
| 98 | + isSelected={selectedToggle === TOGGLE_SELECTED} |
| 99 | + onChange={handleItemClick} |
| 100 | + aria-disabled={selected.length === 0} |
| 101 | + /> |
| 102 | + </ToggleGroup> |
| 103 | + } |
| 104 | + pagination={<Pagination perPageOptions={perPageOptions} itemCount={repositories.length} {...pagination} />} |
| 105 | + /> |
| 106 | + |
| 107 | + {selected.length === 0 && (<Tooltip |
| 108 | + id="selected-row-switch-tooltip" |
| 109 | + content="Select at least one row to enable this filter" |
| 110 | + triggerRef={() => document.getElementById('selected-row-switch') as HTMLButtonElement} |
| 111 | + />)} |
| 112 | + |
| 113 | + <DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} /> |
| 114 | + </DataView> |
| 115 | + ); |
| 116 | +}; |
0 commit comments