|
| 1 | +import React from 'react'; |
| 2 | +import SkeletonTable from '@patternfly/react-core/dist/js/components/Skeleton/SkeletonTable'; |
| 3 | +import { Table, Tbody, Td, Th, Tr, Thead } from '@patternfly/react-table'; |
| 4 | +import { Button, Stack, StackItem } from '@patternfly/react-core'; |
| 5 | + |
| 6 | +interface Repository { |
| 7 | + name: string; |
| 8 | + branches: string | null; |
| 9 | + prs: string | null; |
| 10 | + workspaces: string; |
| 11 | + lastCommit: string; |
| 12 | +} |
| 13 | + |
| 14 | +export const SkeletonTableExample: React.FC = () => { |
| 15 | + const [ isLoaded, setIsLoaded ] = React.useState(false); |
| 16 | + |
| 17 | + const simulatedAsyncCall = new Promise<boolean>((resolve) => { |
| 18 | + setTimeout(() => { |
| 19 | + resolve(true); |
| 20 | + }, 5000); |
| 21 | + }); |
| 22 | + |
| 23 | + const loadData = async () => { |
| 24 | + const result = await simulatedAsyncCall; |
| 25 | + setIsLoaded(result); |
| 26 | + }; |
| 27 | + |
| 28 | + const repositories: Repository[] = [ |
| 29 | + { name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' }, |
| 30 | + { name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' }, |
| 31 | + { name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' } |
| 32 | + ]; |
| 33 | + |
| 34 | + const columnNames = { |
| 35 | + name: 'Repositories', |
| 36 | + branches: 'Branches', |
| 37 | + prs: 'Pull requests', |
| 38 | + workspaces: 'Workspaces', |
| 39 | + lastCommit: 'Last commit' |
| 40 | + }; |
| 41 | + |
| 42 | + let table: React.ReactNode; |
| 43 | + |
| 44 | + if (!isLoaded) { |
| 45 | + table = ( |
| 46 | + <SkeletonTable |
| 47 | + rows={3} |
| 48 | + columns={[ |
| 49 | + columnNames.name, |
| 50 | + columnNames.branches, |
| 51 | + columnNames.prs, |
| 52 | + columnNames.workspaces, |
| 53 | + columnNames.lastCommit |
| 54 | + ]} |
| 55 | + /> |
| 56 | + ); |
| 57 | + } else { |
| 58 | + table = ( |
| 59 | + <Table> |
| 60 | + <Thead> |
| 61 | + <Tr> |
| 62 | + <Th>{columnNames.name}</Th> |
| 63 | + <Th>{columnNames.branches}</Th> |
| 64 | + <Th>{columnNames.prs}</Th> |
| 65 | + <Th>{columnNames.workspaces}</Th> |
| 66 | + <Th>{columnNames.lastCommit}</Th> |
| 67 | + </Tr> |
| 68 | + </Thead> |
| 69 | + <Tbody> |
| 70 | + {repositories.map((repo) => ( |
| 71 | + <Tr key={repo.name}> |
| 72 | + <Td dataLabel={columnNames.name}>{repo.name}</Td> |
| 73 | + <Td dataLabel={columnNames.branches}>{repo.branches}</Td> |
| 74 | + <Td dataLabel={columnNames.prs}>{repo.prs}</Td> |
| 75 | + <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td> |
| 76 | + <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td> |
| 77 | + </Tr> |
| 78 | + ))} |
| 79 | + </Tbody> |
| 80 | + </Table> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <> |
| 86 | + <Stack hasGutter> |
| 87 | + <StackItem>{table}</StackItem> |
| 88 | + <StackItem> |
| 89 | + <Button |
| 90 | + onClick={() => { |
| 91 | + setIsLoaded(false); |
| 92 | + loadData(); |
| 93 | + }} |
| 94 | + > |
| 95 | + Reload table |
| 96 | + </Button> |
| 97 | + </StackItem> |
| 98 | + </Stack> |
| 99 | + </> |
| 100 | + ); |
| 101 | +}; |
0 commit comments