@patternfly/react-table 4.110.6 → 4.110.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,326 @@
1
+ import React from 'react';
2
+ import {
3
+ Button,
4
+ Card,
5
+ Flex,
6
+ FlexItem,
7
+ Toolbar,
8
+ ToolbarContent,
9
+ ToolbarGroup,
10
+ ToolbarItem,
11
+ OptionsMenu,
12
+ OptionsMenuToggle,
13
+ Pagination,
14
+ Text,
15
+ TextContent,
16
+ Dropdown,
17
+ KebabToggle,
18
+ OptionsMenuItem,
19
+ OptionsMenuSeparator,
20
+ OptionsMenuItemGroup,
21
+ OverflowMenu,
22
+ OverflowMenuContent,
23
+ OverflowMenuControl,
24
+ OverflowMenuDropdownItem,
25
+ OverflowMenuGroup,
26
+ OverflowMenuItem,
27
+ PageSection
28
+ } from '@patternfly/react-core';
29
+ import { TableComposable, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table';
30
+ import { rows, columns } from '../../examples/Data.jsx';
31
+ import SortAmountDownIcon from '@patternfly/react-icons/dist/esm/icons/sort-amount-down-icon';
32
+ import CloneIcon from '@patternfly/react-icons/dist/esm/icons/clone-icon';
33
+ import EditIcon from '@patternfly/react-icons/dist/esm/icons/edit-icon';
34
+ import SyncIcon from '@patternfly/react-icons/dist/esm/icons/sync-icon';
35
+ import CodeIcon from '@patternfly/react-icons/dist/esm/icons/code-icon';
36
+ import CodeBranchIcon from '@patternfly/react-icons/dist/esm/icons/code-branch-icon';
37
+ import CubeIcon from '@patternfly/react-icons/dist/esm/icons/cube-icon';
38
+ import DashboardWrapper from '@patternfly/react-core/src/demos/examples/DashboardWrapper';
39
+
40
+ export const ComposableTableSortable = () => {
41
+ const [isKebabDropdownOpen, setIsKebabDropdownOpen] = React.useState(false);
42
+
43
+ const sortRows = (r, sortIndex, sortDirection) => {
44
+ return [...r].sort((a, b) => {
45
+ let returnValue = 0;
46
+ if (sortIndex === 0 || sortIndex === 7) {
47
+ returnValue = 1
48
+ } else if (typeof Object.values(a)[sortIndex] === 'number') {
49
+ // numeric sort
50
+ returnValue = Object.values(a)[sortIndex] - Object.values(b)[sortIndex];
51
+ } else {
52
+ // string sort
53
+ returnValue = Object.values(a)[sortIndex].localeCompare(Object.values(b)[sortIndex]);
54
+ }
55
+ if (sortDirection === 'desc') {
56
+ return returnValue * -1;
57
+ }
58
+ return returnValue;
59
+ })
60
+ };
61
+
62
+ const [sortedData, setSortedData] = React.useState([
63
+ ...sortRows(rows, 0, 'asc')
64
+ ]);
65
+ const [sortedRows, setSortedRows] = React.useState([
66
+ ...sortedData
67
+ ]);
68
+ const [page, setPage] = React.useState(1);
69
+ const [perPage, setPerPage] = React.useState(10);
70
+
71
+ // index of the currently active column
72
+ const [activeSortIndex, setActiveSortIndex] = React.useState(0);
73
+ // sort direction of the currently active column
74
+ const [activeSortDirection, setActiveSortDirection] = React.useState('asc');
75
+ // sort dropdown expansion
76
+ const [isSortDropdownOpen, setIsSortDropdownOpen] = React.useState(false);
77
+
78
+ const onSort = (event, index, direction) => {
79
+ setActiveSortIndex(index);
80
+ setActiveSortDirection(direction);
81
+
82
+ setSortedData(sortRows(rows, index, direction));
83
+ };
84
+
85
+ const kebabDropdownItems = [<OverflowMenuDropdownItem key="kebab-1">Some action</OverflowMenuDropdownItem>];
86
+
87
+ React.useEffect(() => {
88
+ setSortedRows(sortedData.slice((page - 1) * perPage, page * perPage -1));
89
+ }, [sortedData, page, perPage]);
90
+
91
+ const handleSetPage = (_evt, newPage) => {
92
+ setPage(newPage);
93
+ }
94
+
95
+ const handlePerPageSelect = (_evt, newPerPage) => {
96
+ setPerPage(newPerPage);
97
+ }
98
+
99
+ renderPagination = (variant) => (
100
+ <Pagination
101
+ isCompact
102
+ itemCount={rows.length}
103
+ page={page}
104
+ perPage={perPage}
105
+ onSetPage={handleSetPage}
106
+ onPerPageSelect={handlePerPageSelect}
107
+ perPageOptions={[
108
+ { title: '10', value: 10 },
109
+ { title: '20', value: 20 },
110
+ { title: '50', value: 50 },
111
+ { title: '100', value: 100 }
112
+ ]}
113
+ variant={variant}
114
+ titles={{
115
+ paginationTitle: `${variant} pagination`
116
+ }}
117
+ />
118
+ );
119
+
120
+ const renderLabel = labelText => {
121
+ switch (labelText) {
122
+ case 'Running':
123
+ return <Label color="green">{labelText}</Label>;
124
+ case 'Stopped':
125
+ return <Label color="orange">{labelText}</Label>;
126
+ case 'Needs Maintenance':
127
+ return <Label color="blue">{labelText}</Label>;
128
+ case 'Down':
129
+ return <Label color="red">{labelText}</Label>;
130
+ }
131
+ };
132
+
133
+ const tableToolbar = (
134
+ <Toolbar id="sortable-toolbar">
135
+ <ToolbarContent>
136
+ <ToolbarItem visibility={{ md: 'hidden' }}>
137
+ <OptionsMenu
138
+ id="options-menu-multiple-options-example"
139
+ menuItems={[
140
+ <OptionsMenuItemGroup key="first group" aria-label="Sort column">
141
+ {columns.map((column, columnIndex) => (
142
+ <OptionsMenuItem
143
+ key={column}
144
+ isSelected={activeSortIndex === columnIndex}
145
+ onSelect={evt =>
146
+ onSort(evt, columnIndex, activeSortDirection !== 'none' ? activeSortDirection : 'asc')
147
+ }
148
+ >
149
+ {column}
150
+ </OptionsMenuItem>
151
+ ))}
152
+ </OptionsMenuItemGroup>,
153
+ <OptionsMenuSeparator key="separator" />,
154
+ <OptionsMenuItemGroup key="second group" aria-label="Sort direction">
155
+ <OptionsMenuItem
156
+ onSelect={evt => onSort(evt, activeSortIndex, 'asc')}
157
+ isSelected={activeSortDirection === 'asc'}
158
+ id="ascending"
159
+ key="ascending"
160
+ >
161
+ Ascending
162
+ </OptionsMenuItem>
163
+ <OptionsMenuItem
164
+ onSelect={evt => onSort(evt, activeSortIndex, 'desc')}
165
+ isSelected={activeSortDirection === 'desc'}
166
+ id="descending"
167
+ key="descending"
168
+ >
169
+ Descending
170
+ </OptionsMenuItem>
171
+ </OptionsMenuItemGroup>
172
+ ]}
173
+ isOpen={isSortDropdownOpen}
174
+ toggle={
175
+ <OptionsMenuToggle
176
+ hideCaret
177
+ onToggle={() => setIsSortDropdownOpen(!isSortDropdownOpen)}
178
+ toggleTemplate={<SortAmountDownIcon />}
179
+ />
180
+ }
181
+ isPlain
182
+ isGrouped
183
+ menuAppendTo="parent"
184
+ isFlipEnabled
185
+ />
186
+ </ToolbarItem>
187
+ <OverflowMenu breakpoint="lg">
188
+ <OverflowMenuContent isPersistent>
189
+ <OverflowMenuGroup isPersistent groupType="button">
190
+ <OverflowMenuItem>
191
+ <Button variant="primary">Create instance</Button>
192
+ </OverflowMenuItem>
193
+ <OverflowMenuItem>
194
+ <Button variant="secondary">Action</Button>
195
+ </OverflowMenuItem>
196
+ </OverflowMenuGroup>
197
+ </OverflowMenuContent>
198
+ <OverflowMenuControl hasAdditionalOptions>
199
+ <Dropdown
200
+ isPlain
201
+ onSelect={() => setIsKebabDropdownOpen(!isKebabDropdownOpen)}
202
+ toggle={<KebabToggle onToggle={() => setIsKebabDropdownOpen(!isKebabDropdownOpen)} />}
203
+ isOpen={isKebabDropdownOpen}
204
+ dropdownItems={kebabDropdownItems}
205
+ isFlipEnabled
206
+ menuAppendTo="parent"
207
+ />
208
+ </OverflowMenuControl>
209
+ </OverflowMenu>
210
+ <ToolbarGroup variant="icon-button-group">
211
+ <ToolbarItem>
212
+ <Button aria-label="Edit" variant="plain" icon={<EditIcon />} />
213
+ </ToolbarItem>
214
+ <ToolbarItem>
215
+ <Button aria-label="Clone" variant="plain" icon={<CloneIcon />} />
216
+ </ToolbarItem>
217
+ <ToolbarItem>
218
+ <Button aria-label="Sync" variant="plain" icon={<SyncIcon />} />
219
+ </ToolbarItem>
220
+ </ToolbarGroup>
221
+ <ToolbarItem variant="pagination">{renderPagination('top', true)}</ToolbarItem>
222
+ </ToolbarContent>
223
+ </Toolbar>
224
+ );
225
+
226
+ return (
227
+ <React.Fragment>
228
+ <DashboardWrapper>
229
+ <PageSection isWidthLimited variant={PageSectionVariants.light}>
230
+ <TextContent>
231
+ <Text component="h1">Table demos</Text>
232
+ <Text component="p">
233
+ Below is an example of a responsive sortable table. When the screen size shrinks the table into a compact
234
+ form, the toolbar will display a dropdown menu containing sorting options.
235
+ </Text>
236
+ </TextContent>
237
+ </PageSection>
238
+ <PageSection
239
+ padding={{
240
+ default: 'noPadding',
241
+ xl: 'padding'
242
+ }}
243
+ >
244
+ <Card component="div">
245
+ {tableToolbar}
246
+ <TableComposable aria-label="Sortable Table">
247
+ <Thead>
248
+ <Tr>
249
+ {columns.map((column, columnIndex) => {
250
+ const sortParams = {
251
+ sort: {
252
+ sortBy: {
253
+ index: activeSortIndex,
254
+ direction: activeSortDirection
255
+ },
256
+ onSort,
257
+ columnIndex
258
+ }
259
+ };
260
+ return (
261
+ <Th modifier={columnIndex !== 6 ? "wrap" : undefined} key={columnIndex} {...sortParams}>
262
+ {column}
263
+ </Th>
264
+ );
265
+ })}
266
+ </Tr>
267
+ </Thead>
268
+ <Tbody>
269
+ {sortedRows.map((row, rowIndex) => (
270
+ <Tr key={rowIndex}>
271
+ <>
272
+ <Td dataLabel={columns[0]} width={15} >
273
+ <div>{row.name}</div>
274
+ </Td>
275
+ <Td dataLabel={columns[1]} width={10}>
276
+ <Flex spaceItems={{ default: 'spaceItemsSm' }}>
277
+ <FlexItem>
278
+ <CodeBranchIcon key="icon" />
279
+ </FlexItem>
280
+ <FlexItem>{row.threads}</FlexItem>
281
+ </Flex>
282
+ </Td>
283
+ <Td dataLabel={columns[2]} width={10}>
284
+ <Flex spaceItems={{ default: 'spaceItemsSm' }}>
285
+ <FlexItem>
286
+ <CodeIcon key="icon" />
287
+ </FlexItem>
288
+ <FlexItem>{row.applications}</FlexItem>
289
+ </Flex>
290
+ </Td>
291
+ <Td dataLabel={columns[3]} width={10}>
292
+ <Flex spaceItems={{ default: 'spaceItemsSm' }}>
293
+ <FlexItem>
294
+ <CubeIcon key="icon" />
295
+ </FlexItem>
296
+ <FlexItem>{row.workspaces}</FlexItem>
297
+ </Flex>
298
+ </Td>
299
+ <Td dataLabel={columns[4]} width={15}>
300
+ <Flex spaceItems={{ default: 'spaceItemsSm' }}>
301
+ <FlexItem>{renderLabel(row.status)}</FlexItem>
302
+ </Flex>
303
+ </Td>
304
+ <Td dataLabel={columns[5]} width={10}>
305
+ <Flex spaceItems={{ default: 'spaceItemsSm' }}>
306
+ <FlexItem>{row.location}</FlexItem>
307
+ </Flex>
308
+ </Td>
309
+ <Td dataLabel={columns[6]} width={10}>
310
+ <Flex spaceItems={{ default: 'spaceItemsSm' }}>
311
+ <FlexItem>{row.lastModified[0]} days ago</FlexItem>
312
+ </Flex>
313
+ </Td>
314
+ <Td dataLabel={columns[7]} modifier="truncate"><a href="#">{row.url}</a></Td>
315
+ </>
316
+ </Tr>
317
+ ))}
318
+ </Tbody>
319
+ </TableComposable>
320
+ {renderPagination('bottom', false)}
321
+ </Card>
322
+ </PageSection>
323
+ </DashboardWrapper>
324
+ </React.Fragment>
325
+ );
326
+ };