forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableExpandable.tsx
More file actions
200 lines (194 loc) · 8.06 KB
/
TableExpandable.tsx
File metadata and controls
200 lines (194 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { Fragment, useState } from 'react';
import { Table, Thead, Tr, Th, Tbody, Td, ExpandableRowContent } from '@patternfly/react-table';
import { Checkbox } from '@patternfly/react-core';
interface Repository {
name: string;
branches: string;
prs: string;
workspaces: string;
lastCommit: string;
details?: {
detail1?: string;
detail2?: string;
detail3?: string;
detailFormat: 0 | 1 | 2 | 3;
};
}
export const TableExpandable: React.FunctionComponent = () => {
// In real usage, this data would come from some external source like an API via props.
const repositories: Repository[] = [
{ name: 'one', branches: 'two', prs: 'a', workspaces: 'four', lastCommit: 'five' },
{
name: 'parent 1',
branches: 'two',
prs: 'k',
workspaces: 'four',
lastCommit: 'five',
// This `details` structure is just for this example. You can drive expanded content from any kind of data.
details: { detailFormat: 0, detail1: 'single cell' }
},
{
name: 'parent 2',
branches: 'two',
prs: 'b',
workspaces: 'four',
lastCommit: 'five',
details: {
detailFormat: 1,
detail1:
'Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. Lorem ipsum sit dolor. '
}
},
{
name: 'parent 3',
branches: '2',
prs: 'b',
workspaces: 'four',
lastCommit: 'five',
details: { detailFormat: 2, detail1: 'single cell - noPadding' }
},
{
name: 'parent 4',
branches: '2',
prs: 'b',
workspaces: 'four',
lastCommit: 'five',
details: { detailFormat: 3, detail1: 'single cell - fullWidth & noPadding' }
},
{
name: 'parent 5',
branches: '2',
prs: 'b',
workspaces: 'four',
lastCommit: 'five',
details: {
detailFormat: 0,
detail1: "spans 'Repositories and 'Branches'",
detail2: "spans 'Pull requests' and 'Workspaces', and 'Last commit'"
}
},
{
name: 'parent 6',
branches: '2',
prs: 'b',
workspaces: 'four',
lastCommit: 'five',
details: {
detailFormat: 1,
detail1: "fullWidth, spans the collapsible column and 'Repositories'",
detail2: "fullWidth, spans 'Branches' and 'Pull requests'",
detail3: "fullWidth, spans 'Workspaces' and 'Last commit'"
}
}
];
const columnNames = {
name: 'Repositories',
branches: 'Branches',
prs: 'Pull requests',
workspaces: 'Workspaces',
lastCommit: 'Last commit'
};
// In this example, expanded rows are tracked by the repo names from each row. This could be any unique identifier.
// This is to prevent state from being based on row order index in case we later add sorting.
// Note that this behavior is very similar to selection state.
const initialExpandedRepoNames = repositories.filter((repo) => !!repo.details).map((repo) => repo.name); // Default to all expanded
const [expandedRepoNames, setExpandedRepoNames] = useState<string[]>(initialExpandedRepoNames);
const setRepoExpanded = (repo: Repository, isExpanding = true) =>
setExpandedRepoNames((prevExpanded) => {
const otherExpandedRepoNames = prevExpanded.filter((r) => r !== repo.name);
return isExpanding ? [...otherExpandedRepoNames, repo.name] : otherExpandedRepoNames;
});
const isRepoExpanded = (repo: Repository) => expandedRepoNames.includes(repo.name);
const [isExampleCompact, setIsExampleCompact] = useState(true);
return (
<Fragment>
<Checkbox
label="Compact"
isChecked={isExampleCompact}
onChange={(_event, checked) => setIsExampleCompact(checked)}
aria-label="toggle compact variation"
id="toggle-compact"
name="toggle-compact"
/>
<Table isExpandable aria-label="Expandable table" variant={isExampleCompact ? 'compact' : undefined}>
<Thead>
<Tr>
<Th screenReaderText="Row expansion" />
<Th width={25}>{columnNames.name}</Th>
<Th width={10}>{columnNames.branches}</Th>
<Th width={15}>{columnNames.prs}</Th>
<Th width={30}>{columnNames.workspaces}</Th>
<Th width={10}>{columnNames.lastCommit}</Th>
</Tr>
</Thead>
{repositories.map((repo, rowIndex) => {
// Some arbitrary examples of how you could customize the child row based on your needs
let childIsFullWidth = false;
let childHasNoPadding = false;
let detail1Colspan = 1;
let detail2Colspan = 1;
let detail3Colspan = 1;
if (repo.details) {
const { detail1, detail2, detail3, detailFormat } = repo.details;
const numColumns = 5;
childIsFullWidth = [1, 3].includes(detailFormat);
childHasNoPadding = [2, 3].includes(detailFormat);
if (detail1 && !detail2 && !detail3) {
detail1Colspan = !childIsFullWidth ? numColumns : numColumns + 1; // Account for toggle column
} else if (detail1 && detail2 && !detail3) {
detail1Colspan = 2;
detail2Colspan = !childIsFullWidth ? 3 : 4;
} else if (detail1 && detail2 && detail3) {
detail1Colspan = 2;
detail2Colspan = 2;
detail3Colspan = !childIsFullWidth ? 1 : 2;
}
}
return (
<Tbody key={repo.name} isExpanded={isRepoExpanded(repo)}>
<Tr isExpanded={isRepoExpanded(repo)}>
<Td
expand={
repo.details
? {
rowIndex,
isExpanded: isRepoExpanded(repo),
onToggle: () => setRepoExpanded(repo, !isRepoExpanded(repo)),
expandId: 'composable-expandable-example'
}
: undefined
}
/>
<Td dataLabel={columnNames.name}>{repo.name}</Td>
<Td dataLabel={columnNames.branches}>{repo.branches}</Td>
<Td dataLabel={columnNames.prs}>{repo.prs}</Td>
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
<Td dataLabel={columnNames.lastCommit}>{repo.lastCommit}</Td>
</Tr>
{repo.details ? (
<Tr isExpandable isExpanded={isRepoExpanded(repo)}>
{!childIsFullWidth ? <Td /> : null}
{repo.details.detail1 ? (
<Td dataLabel="Repo detail 1" noPadding={childHasNoPadding} colSpan={detail1Colspan}>
<ExpandableRowContent>{repo.details.detail1}</ExpandableRowContent>
</Td>
) : null}
{repo.details.detail2 ? (
<Td dataLabel="Repo detail 2" noPadding={childHasNoPadding} colSpan={detail2Colspan}>
<ExpandableRowContent>{repo.details.detail2}</ExpandableRowContent>
</Td>
) : null}
{repo.details.detail3 ? (
<Td dataLabel="Repo detail 3" noPadding={childHasNoPadding} colSpan={detail3Colspan}>
<ExpandableRowContent>{repo.details.detail3}</ExpandableRowContent>
</Td>
) : null}
</Tr>
) : null}
</Tbody>
);
})}
</Table>
</Fragment>
);
};