Debian Codegraph @

node/@patternfly/react-table/src/components/Table/examples/TableFavoritable.tsx

raw
1 import { useCallback, useState } from 'react';
2 import { Table, Thead, Tr, Th, Tbody, Td, ThProps } from '@patternfly/react-table';
3
4 interface Repository {
5 name: string;
6 branches: string;
7 prs: string;
8 workspaces: string;
9 lastCommit: string;
10 }
11
12 export const TableFavoritable: React.FunctionComponent = () => {
13 // In real usage, this data would come from some external source like an API via props.
14 const repositories: Repository[] = [
15 { name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
16 { name: 'a', branches: 'two', prs: 'k', workspaces: 'four', lastCommit: 'five' },
17 { name: 'p', branches: 'two', prs: 'b', workspaces: 'four', lastCommit: 'five' }
18 ];
19
20 const columnNames = {
21 name: 'Repositories',
22 branches: 'Branches',
23 prs: 'Pull requests',
24 workspaces: 'Workspaces',
25 lastCommit: 'Last commit'
26 };
27
28 // Index of the currently sorted column
29 // Note: if you intend to make columns reorderable, you may instead want to use a non-numeric key
30 // as the identifier of the sorted column. See the "Compound expandable" example.
31 const [activeSortIndex, setActiveSortIndex] = useState<number>();
32
33 // Sort direction of the currently sorted column
34 const [activeSortDirection, setActiveSortDirection] = useState<'asc' | 'desc'>();
35
36 // Favorite state is similar to selection state, see Selectable with checkbox.
37 const [favoriteRepoNames, setFavoriteRepoNames] = useState<string[]>([]);
38 const setRepoFavorited = (repo: Repository, isFavoriting = true) =>
39 setFavoriteRepoNames((prevFavorites) => {
40 const otherFavorites = prevFavorites.filter((r) => r !== repo.name);
41 return isFavoriting ? [...otherFavorites, repo.name] : otherFavorites;
42 });
43 const isRepoFavorited = (repo: Repository) => favoriteRepoNames.includes(repo.name);
44
45 // State of the header cell to favorite / unfavorite all rows
46 const [headerFavorited, setHeaderFavorited] = useState(false);
47
48 // Since OnSort specifies sorted columns by index, we need sortable values for our object by column index.
49 // In this example we only deal with booleans here because we only sort on the favorites column.
50 // For more complex sorting, see Sortable.
51 // Note: We also memoize the sortable values with useCallback to prevent rows jumping around when you change
52 // the favorites while sorting on that column. Only updating the sort state will reorder the rows.
53 const getSortableRowValues = useCallback(
54 (repo: Repository): boolean[] => [isRepoFavorited(repo)],
55 [activeSortIndex, activeSortDirection]
56 );
57
58 // Note that we perform the sort as part of the component's render logic and not in onSort.
59 // We shouldn't store the list of data in state because we don't want to have to sync that with props.
60 let sortedRepositories = repositories;
61 if (activeSortIndex !== undefined) {
62 sortedRepositories = repositories.sort((a, b) => {
63 const aValue = getSortableRowValues(a)[activeSortIndex];
64 const bValue = getSortableRowValues(b)[activeSortIndex];
65 if (aValue === bValue) {
66 return 0;
67 }
68 if (activeSortDirection === 'asc') {
69 return aValue > bValue ? 1 : -1;
70 } else {
71 return bValue > aValue ? 1 : -1;
72 }
73 });
74 }
75
76 const getSortParams = (columnIndex: number): ThProps['sort'] => ({
77 isFavorites: columnIndex === 0, // Not just statically true in case we add sorting on other columns later
78 sortBy: {
79 index: activeSortIndex,
80 direction: activeSortDirection
81 },
82 onSort: (_event, index, direction) => {
83 setActiveSortIndex(index);
84 setActiveSortDirection(direction);
85 },
86 'aria-label': 'Sort favorites',
87 columnIndex,
88 favoriteButtonProps: {
89 favorited: headerFavorited,
90 onClick: (_event) => {
91 repositories.forEach((repo) => setRepoFavorited(repo, !headerFavorited));
92 setHeaderFavorited(!headerFavorited);
93 },
94 'aria-label': headerFavorited ? 'Unfavorite all' : 'Favorite all'
95 }
96 });
97
98 return (
99 <Table aria-label="Favoritable table" variant="compact">
100 <Thead>
101 <Tr>
102 <Th sort={getSortParams(0)} />
103 <Th>{columnNames.name}</Th>
104 <Th>{columnNames.branches}</Th>
105 <Th>{columnNames.prs}</Th>
106 <Th>{columnNames.workspaces}</Th>
107 <Th>{columnNames.lastCommit}</Th>
108 </Tr>
109 </Thead>
110 <Tbody>
111 {sortedRepositories.map((repo, rowIndex) => (
112 <Tr key={rowIndex}>
113 <Td
114 favorites={{
115 isFavorited: isRepoFavorited(repo),
116 onFavorite: (_event, isFavoriting) => {
117 setRepoFavorited(repo, isFavoriting);
118
119 if (
120 isFavoriting &&
121 repositories.filter((r) => r !== repo).every((r) => favoriteRepoNames.includes(r.name))
122 ) {
123 setHeaderFavorited(true);
124 }
125
126 if (!isFavoriting && favoriteRepoNames.length === 1 && favoriteRepoNames.includes(repo.name)) {
127 setHeaderFavorited(false);
128 }
129 },
130 rowIndex
131 }}
132 />
133 <Td dataLabel={columnNames.name}>{repo.name}</Td>
134 <Td dataLabel={columnNames.branches}>{repo.branches}</Td>
135 <Td dataLabel={columnNames.prs}>{repo.prs}</Td>
136 <Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
137 <Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
138 </Tr>
139 ))}
140 </Tbody>
141 </Table>
142 );
143 };