| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { |
| 3 |
Button, |
| 4 |
OverflowMenu, |
| 5 |
OverflowMenuControl, |
| 6 |
OverflowMenuContent, |
| 7 |
OverflowMenuGroup, |
| 8 |
OverflowMenuItem, |
| 9 |
OverflowMenuDropdownItem, |
| 10 |
MenuToggle, |
| 11 |
Dropdown, |
| 12 |
DropdownList |
| 13 |
} from '@patternfly/react-core'; |
| 14 |
import { Table, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table'; |
| 15 |
import RhUiEllipsisVerticalFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-ellipsis-vertical-fill-icon'; |
| 16 |
|
| 17 |
interface Repository { |
| 18 |
name: string; |
| 19 |
branches: string; |
| 20 |
prs: string; |
| 21 |
workspaces: string; |
| 22 |
lastCommit: string; |
| 23 |
isMenuOpen: boolean; |
| 24 |
} |
| 25 |
|
| 26 |
export const TableActions: React.FunctionComponent = () => { |
| 27 |
// In real usage, this data would come from some external source like an API via props. |
| 28 |
const repositories: Repository[] = [ |
| 29 |
{ name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five', isMenuOpen: false }, |
| 30 |
{ name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five', isMenuOpen: false }, |
| 31 |
{ name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five', isMenuOpen: false }, |
| 32 |
{ name: '4', branches: '2', prs: 'b', workspaces: 'four', lastCommit: 'five', isMenuOpen: false }, |
| 33 |
{ name: '5', branches: '2', prs: 'b', workspaces: 'four', lastCommit: 'five', isMenuOpen: false } |
| 34 |
]; |
| 35 |
|
| 36 |
const [repos, setRepos] = useState(repositories); |
| 37 |
|
| 38 |
const columnNames = { |
| 39 |
name: 'Repositories', |
| 40 |
branches: 'Branches', |
| 41 |
prs: 'Pull requests', |
| 42 |
workspaces: 'Workspaces', |
| 43 |
lastCommit: 'Last commit' |
| 44 |
}; |
| 45 |
|
| 46 |
const dropdownItems = [ |
| 47 |
<OverflowMenuDropdownItem itemId={0} key="item1" isShared> |
| 48 |
Primary |
| 49 |
</OverflowMenuDropdownItem>, |
| 50 |
<OverflowMenuDropdownItem itemId={1} key="item2" isShared> |
| 51 |
Secondary |
| 52 |
</OverflowMenuDropdownItem>, |
| 53 |
<OverflowMenuDropdownItem itemId={2} key="item3" isShared> |
| 54 |
Tertiary |
| 55 |
</OverflowMenuDropdownItem> |
| 56 |
]; |
| 57 |
|
| 58 |
return ( |
| 59 |
<Fragment> |
| 60 |
<Table aria-label="Actions table"> |
| 61 |
<Thead> |
| 62 |
<Tr> |
| 63 |
<Th>{columnNames.name}</Th> |
| 64 |
<Th>{columnNames.branches}</Th> |
| 65 |
<Th>{columnNames.prs}</Th> |
| 66 |
<Th>{columnNames.workspaces}</Th> |
| 67 |
<Th>{columnNames.lastCommit}</Th> |
| 68 |
<Th screenReaderText="Actions" /> |
| 69 |
</Tr> |
| 70 |
</Thead> |
| 71 |
<Tbody> |
| 72 |
{repos.map((repo) => ( |
| 73 |
<Tr key={repo.name}> |
| 74 |
<Td dataLabel={columnNames.name}>{repo.name}</Td> |
| 75 |
<Td dataLabel={columnNames.branches}>{repo.branches}</Td> |
| 76 |
<Td dataLabel={columnNames.prs}>{repo.prs}</Td> |
| 77 |
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td> |
| 78 |
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> |
| 79 |
<Td isActionCell> |
| 80 |
<OverflowMenu breakpoint="lg"> |
| 81 |
<OverflowMenuContent> |
| 82 |
<OverflowMenuGroup groupType="button"> |
| 83 |
<OverflowMenuItem> |
| 84 |
<Button variant="primary">Primary</Button> |
| 85 |
</OverflowMenuItem> |
| 86 |
<OverflowMenuItem> |
| 87 |
<Button variant="secondary">Secondary</Button> |
| 88 |
</OverflowMenuItem> |
| 89 |
<OverflowMenuItem> |
| 90 |
<Button variant="tertiary">Tertiary</Button> |
| 91 |
</OverflowMenuItem> |
| 92 |
</OverflowMenuGroup> |
| 93 |
</OverflowMenuContent> |
| 94 |
<OverflowMenuControl> |
| 95 |
<Dropdown |
| 96 |
onSelect={() => |
| 97 |
setRepos(repos.map((r) => (r.name !== repo.name ? r : { ...r, isMenuOpen: !r.isMenuOpen }))) |
| 98 |
} |
| 99 |
toggle={(toggleRef) => ( |
| 100 |
<MenuToggle |
| 101 |
ref={toggleRef} |
| 102 |
variant="plain" |
| 103 |
aria-label="Table actions overflow menu" |
| 104 |
onClick={() => |
| 105 |
setRepos(repos.map((r) => (r.name !== repo.name ? r : { ...r, isMenuOpen: !r.isMenuOpen }))) |
| 106 |
} |
| 107 |
isExpanded={repo.isMenuOpen} |
| 108 |
icon={<RhUiEllipsisVerticalFillIcon />} |
| 109 |
/> |
| 110 |
)} |
| 111 |
isOpen={repo.isMenuOpen} |
| 112 |
onOpenChange={(isOpen) => |
| 113 |
setRepos(repos.map((r) => (r.name !== repo.name ? r : { ...r, isMenuOpen: isOpen }))) |
| 114 |
} |
| 115 |
> |
| 116 |
<DropdownList>{dropdownItems}</DropdownList> |
| 117 |
</Dropdown> |
| 118 |
</OverflowMenuControl> |
| 119 |
</OverflowMenu> |
| 120 |
</Td> |
| 121 |
</Tr> |
| 122 |
))} |
| 123 |
</Tbody> |
| 124 |
</Table> |
| 125 |
</Fragment> |
| 126 |
); |
| 127 |
}; |