@patternfly/react-table 4.110.19 → 4.110.21

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,486 @@
1
+ import * as React from 'react';
2
+ import {
3
+ Button,
4
+ DataList,
5
+ DataListCheck,
6
+ DataListItem,
7
+ DataListItemRow,
8
+ DataListCell,
9
+ DataListItemCells,
10
+ Label,
11
+ Toolbar,
12
+ ToolbarContent,
13
+ ToolbarItem,
14
+ Modal,
15
+ OverflowMenu,
16
+ OverflowMenuGroup,
17
+ OverflowMenuItem,
18
+ OptionsMenu,
19
+ OptionsMenuToggle,
20
+ Pagination,
21
+ PaginationVariant,
22
+ Text,
23
+ TextContent,
24
+ Select,
25
+ SelectVariant
26
+ } from '@patternfly/react-core';
27
+ import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon';
28
+ import SortAmountDownIcon from '@patternfly/react-icons/dist/esm/icons/sort-amount-down-icon';
29
+ import { rows, columns } from '../../examples/Data.jsx';
30
+ import DashboardWrapper from '@patternfly/react-core/src/demos/examples/DashboardWrapper.js';
31
+ import { capitalize } from '@patternfly/react-table/src/components/Table/utils/utils';
32
+
33
+ export const ColumnManagementAction = () => {
34
+ const defaultColumns = columns;
35
+ const defaultRows = rows;
36
+
37
+ const [filters, setFilters] = React.useState([]);
38
+ const [filteredColumns, setFilteredColumns] = React.useState([]);
39
+ const [filteredRows, setFilteredRows] = React.useState([]);
40
+ const [managedColumns, setManagedColumns] = React.useState(defaultColumns);
41
+ const [managedRows, setManagedRows] = React.useState(defaultRows);
42
+ const [isModalOpen, setIsModalOpen] = React.useState(false);
43
+ const [checkedState, setCheckedState] = React.useState(Array(columns.length).fill(true));
44
+ const [page, setPage] = React.useState(1);
45
+ const [perPage, setPerPage] = React.useState(10);
46
+ const [paginatedRows, setPaginatedRows] = React.useState(rows);
47
+
48
+ const matchCheckboxNameToColumn = name => {
49
+ switch (name) {
50
+ case 'check1':
51
+ return 'Servers';
52
+ case 'check2':
53
+ return 'Threads';
54
+ case 'check3':
55
+ return 'Applications';
56
+ case 'check4':
57
+ return 'Workspaces';
58
+ case 'check5':
59
+ return 'Status';
60
+ case 'check6':
61
+ return 'Location';
62
+ case 'check7':
63
+ return 'Last Modified';
64
+ case 'check8':
65
+ return 'URL';
66
+ }
67
+ };
68
+ const matchSelectedColumnNameToAttr = name => {
69
+ switch (name) {
70
+ case 'Servers':
71
+ return 'name';
72
+ case 'Threads':
73
+ return 'threads';
74
+ case 'Applications':
75
+ return 'applications';
76
+ case 'Workspaces':
77
+ return 'workspaces';
78
+ case 'Status':
79
+ return 'status';
80
+ case 'Location':
81
+ return 'location';
82
+ case 'Last Modified':
83
+ return 'lastModified';
84
+ case 'URL':
85
+ return 'url';
86
+ }
87
+ };
88
+
89
+ // Pagination logic
90
+ const handleSetPage = (_evt, newPage) => {
91
+ setPage(newPage);
92
+ };
93
+
94
+ const handlePerPageSelect = (_evt, newPerPage) => {
95
+ setPerPage(newPerPage);
96
+ };
97
+
98
+ const renderPagination = (variant, isCompact) => (
99
+ <Pagination
100
+ isCompact={isCompact}
101
+ itemCount={rows.length}
102
+ page={page}
103
+ perPage={perPage}
104
+ onSetPage={handleSetPage}
105
+ onPerPageSelect={handlePerPageSelect}
106
+ variant={variant}
107
+ titles={{
108
+ paginationTitle: `${variant} pagination`
109
+ }}
110
+ />
111
+ );
112
+
113
+ React.useEffect(() => {
114
+ setPaginatedRows(managedRows.slice((page - 1) * perPage, page * perPage - 1));
115
+ }, [managedRows, page, perPage]);
116
+
117
+ // Removes attribute from each node object in Data.jsx
118
+ const removePropFromObject = (object, keys) =>
119
+ keys.reduce((obj, prop) => {
120
+ const { [prop]: _, ...keep } = obj;
121
+ return keep;
122
+ }, object);
123
+
124
+ // Filters columns out of table that are not selected in the column management modal
125
+ const filterData = (checked, name) => {
126
+ const selectedColumn = matchSelectedColumnNameToAttr(name);
127
+
128
+ const filteredRows = [];
129
+ if (checked) {
130
+ const updatedFilters = filters.filter(item => item !== selectedColumn);
131
+
132
+ // Only show the names of columns that were selected in the modal
133
+ const filteredColumns = defaultColumns.filter(
134
+ column => !updatedFilters.includes(matchSelectedColumnNameToAttr(column))
135
+ );
136
+
137
+ // Remove the attributes (i.e. "columns") that were not selected
138
+ defaultRows.forEach(item => filteredRows.push(removePropFromObject(item, updatedFilters)));
139
+
140
+ setFilters(updatedFilters);
141
+ setFilteredColumns(filteredColumns);
142
+ setFilteredRows(filteredRows);
143
+ } else {
144
+ let updatedFilters = filters;
145
+ updatedFilters.push(selectedColumn);
146
+
147
+ // Only show the names of columns that were selected in the modal
148
+ const filteredColumns = managedColumns.filter(column => !filters.includes(matchSelectedColumnNameToAttr(column)));
149
+
150
+ // Remove the attributes (i.e. "columns") that were not selected
151
+ managedRows.forEach(item => filteredRows.push(removePropFromObject(item, updatedFilters)));
152
+
153
+ setFilters(updatedFilters);
154
+ setFilteredColumns(filteredColumns);
155
+ setFilteredRows(filteredRows);
156
+ }
157
+ };
158
+ const unfilterAllData = () => {
159
+ setFilters([]);
160
+ setFilteredColumns(defaultColumns);
161
+ setFilteredRows(defaultRows);
162
+ };
163
+
164
+ const handleChange = (checked, event) => {
165
+ const target = event.target;
166
+ const value = target.type === 'checkbox' ? target.checked : target.value;
167
+
168
+ // Remove any columns from the table that aren't checked
169
+ filterData(checked, matchCheckboxNameToColumn(target.name));
170
+ const checkedIndex = columns.findIndex(element => element === matchCheckboxNameToColumn(target.name));
171
+
172
+ const updatedCheckedState = [...checkedState];
173
+ updatedCheckedState[checkedIndex] = value;
174
+ setCheckedState(updatedCheckedState);
175
+ };
176
+
177
+ const handleModalToggle = () => {
178
+ setIsModalOpen(!isModalOpen);
179
+ };
180
+
181
+ const onSave = () => {
182
+ setManagedColumns(filteredColumns);
183
+ setManagedRows(filteredRows);
184
+ setPaginatedRows(filteredRows);
185
+ setIsModalOpen(!isModalOpen);
186
+ };
187
+
188
+ const selectAllColumns = () => {
189
+ unfilterAllData();
190
+ setCheckedState(Array(columns.length).fill(true));
191
+ };
192
+
193
+ const renderModal = () => {
194
+ return (
195
+ <Modal
196
+ title="Manage columns"
197
+ isOpen={isModalOpen}
198
+ variant="small"
199
+ description={
200
+ <TextContent>
201
+ <Text component={TextVariants.p}>Selected categories will be displayed in the table.</Text>
202
+ <Button isInline onClick={selectAllColumns} variant="link">
203
+ Select all
204
+ </Button>
205
+ </TextContent>
206
+ }
207
+ onClose={handleModalToggle}
208
+ actions={[
209
+ <Button key="save" variant="primary" onClick={onSave}>
210
+ Save
211
+ </Button>,
212
+ <Button key="cancel" variant="secondary" onClick={handleModalToggle}>
213
+ Cancel
214
+ </Button>
215
+ ]}
216
+ >
217
+ <DataList aria-label="Table column management" id="table-column-management" isCompact>
218
+ <DataListItem aria-labelledby="table-column-management-item1">
219
+ <DataListItemRow>
220
+ <DataListCheck
221
+ aria-labelledby="table-column-management-item1"
222
+ checked={checkedState[0]}
223
+ name="check1"
224
+ id="check1"
225
+ onChange={handleChange}
226
+ />
227
+ <DataListItemCells
228
+ dataListCells={[
229
+ <DataListCell id="table-column-management-item1" key="table-column-management-item1">
230
+ <label htmlFor="check1">{columns[0]}</label>
231
+ </DataListCell>
232
+ ]}
233
+ />
234
+ </DataListItemRow>
235
+ </DataListItem>
236
+ <DataListItem aria-labelledby="table-column-management-item2">
237
+ <DataListItemRow>
238
+ <DataListCheck
239
+ aria-labelledby="table-column-management-item2"
240
+ checked={checkedState[1]}
241
+ name="check2"
242
+ id="check2"
243
+ onChange={handleChange}
244
+ />
245
+ <DataListItemCells
246
+ dataListCells={[
247
+ <DataListCell id="table-column-management-item2" key="table-column-management-item2">
248
+ <label htmlFor="check2">{columns[1]}</label>
249
+ </DataListCell>
250
+ ]}
251
+ />
252
+ </DataListItemRow>
253
+ </DataListItem>
254
+ <DataListItem aria-labelledby="table-column-management-item3">
255
+ <DataListItemRow>
256
+ <DataListCheck
257
+ aria-labelledby="table-column-management-item3"
258
+ checked={checkedState[2]}
259
+ name="check3"
260
+ id="check3"
261
+ onChange={handleChange}
262
+ />
263
+ <DataListItemCells
264
+ dataListCells={[
265
+ <DataListCell id="table-column-management-item3" key="table-column-management-item3">
266
+ <label htmlFor="check3">{columns[2]}</label>
267
+ </DataListCell>
268
+ ]}
269
+ />
270
+ </DataListItemRow>
271
+ </DataListItem>
272
+ <DataListItem aria-labelledby="table-column-management-item4">
273
+ <DataListItemRow>
274
+ <DataListCheck
275
+ aria-labelledby="table-column-management-item4"
276
+ checked={checkedState[3]}
277
+ name="check4"
278
+ id="check4"
279
+ onChange={handleChange}
280
+ />
281
+ <DataListItemCells
282
+ dataListCells={[
283
+ <DataListCell id="table-column-management-item4" key="table-column-management-item4">
284
+ <label htmlFor="check4">{columns[3]}</label>
285
+ </DataListCell>
286
+ ]}
287
+ />
288
+ </DataListItemRow>
289
+ </DataListItem>
290
+ <DataListItem aria-labelledby="table-column-management-item4">
291
+ <DataListItemRow>
292
+ <DataListCheck
293
+ aria-labelledby="table-column-management-item4"
294
+ checked={checkedState[4]}
295
+ name="check5"
296
+ id="check5"
297
+ onChange={handleChange}
298
+ />
299
+ <DataListItemCells
300
+ dataListCells={[
301
+ <DataListCell id="table-column-management-item4" key="table-column-management-item4">
302
+ <label htmlFor="check5">{columns[4]}</label>
303
+ </DataListCell>
304
+ ]}
305
+ />
306
+ </DataListItemRow>
307
+ </DataListItem>
308
+ <DataListItem aria-labelledby="table-column-management-item5">
309
+ <DataListItemRow>
310
+ <DataListCheck
311
+ aria-labelledby="table-column-management-item5"
312
+ checked={checkedState[5]}
313
+ name="check6"
314
+ id="check6"
315
+ onChange={handleChange}
316
+ />
317
+ <DataListItemCells
318
+ dataListCells={[
319
+ <DataListCell id="table-column-management-item5" key="table-column-management-item5">
320
+ <label htmlFor="check6">{columns[5]}</label>
321
+ </DataListCell>
322
+ ]}
323
+ />
324
+ </DataListItemRow>
325
+ </DataListItem>
326
+ <DataListItem aria-labelledby="table-column-management-item6">
327
+ <DataListItemRow>
328
+ <DataListCheck
329
+ aria-labelledby="table-column-management-item6"
330
+ checked={checkedState[6]}
331
+ name="check7"
332
+ id="check7"
333
+ onChange={handleChange}
334
+ />
335
+ <DataListItemCells
336
+ dataListCells={[
337
+ <DataListCell id="table-column-management-item6" key="table-column-management-item5">
338
+ <label htmlFor="check7">{columns[6]}</label>
339
+ </DataListCell>
340
+ ]}
341
+ />
342
+ </DataListItemRow>
343
+ </DataListItem>
344
+ <DataListItem aria-labelledby="table-column-management-item5">
345
+ <DataListItemRow>
346
+ <DataListCheck
347
+ aria-labelledby="table-column-management-item5"
348
+ checked={checkedState[7]}
349
+ name="check8"
350
+ id="check8"
351
+ onChange={handleChange}
352
+ />
353
+ <DataListItemCells
354
+ dataListCells={[
355
+ <DataListCell id="table-column-management-item7" key="table-column-management-item7">
356
+ <label htmlFor="check8">{columns[7]}</label>
357
+ </DataListCell>
358
+ ]}
359
+ />
360
+ </DataListItemRow>
361
+ </DataListItem>
362
+ </DataList>
363
+ </Modal>
364
+ );
365
+ };
366
+
367
+ const renderLabel = labelText => {
368
+ switch (labelText) {
369
+ case 'Running':
370
+ return <Label color="green">{labelText}</Label>;
371
+ case 'Stopped':
372
+ return <Label color="orange">{labelText}</Label>;
373
+ case 'Needs Maintenance':
374
+ return <Label color="blue">{labelText}</Label>;
375
+ case 'Down':
376
+ return <Label color="red">{labelText}</Label>;
377
+ }
378
+ };
379
+
380
+ const toolbarItems = (
381
+ <React.Fragment>
382
+ <Toolbar id="page-layout-table-column-management-action-toolbar-top">
383
+ <span id="page-layout-table-column-management-action-toolbar-top-select-checkbox-label" hidden>
384
+ Choose one
385
+ </span>
386
+ <ToolbarContent>
387
+ <ToolbarItem variant="overflow-menu">
388
+ <OverflowMenu breakpoint="md">
389
+ <OverflowMenuItem>
390
+ <Select
391
+ id="page-layout-table-column-management-action-toolbar-top-select-checkbox-toggle"
392
+ variant={SelectVariant.single}
393
+ aria-label="Select Input"
394
+ aria-labelledby="page-layout-table-column-management-action-toolbar-top-select-checkbox-label page-layout-table-column-management-action-toolbar-top-select-checkbox-toggle"
395
+ placeholderText={
396
+ <>
397
+ <FilterIcon /> Name
398
+ </>
399
+ }
400
+ />
401
+ </OverflowMenuItem>
402
+ <OverflowMenuItem>
403
+ <OptionsMenu
404
+ id="page-layout-table-column-management-action-toolbar-top-options-menu-toggle"
405
+ isPlain
406
+ menuItems={[]}
407
+ toggle={
408
+ <OptionsMenuToggle
409
+ toggleTemplate={<SortAmountDownIcon aria-hidden="true" />}
410
+ aria-label="Sort by"
411
+ hideCaret
412
+ />
413
+ }
414
+ />
415
+ </OverflowMenuItem>
416
+ <OverflowMenuGroup groupType="button" isPersistent>
417
+ <OverflowMenuItem>
418
+ <Button variant="primary">Action</Button>
419
+ </OverflowMenuItem>
420
+ <OverflowMenuItem breakpoint="md" isPersistent>
421
+ <Button variant="link" onClick={handleModalToggle}>
422
+ Manage columns
423
+ </Button>
424
+ </OverflowMenuItem>
425
+ </OverflowMenuGroup>
426
+ </OverflowMenu>
427
+ </ToolbarItem>
428
+ <ToolbarItem variant="pagination">{renderPagination()}</ToolbarItem>
429
+ </ToolbarContent>
430
+ </Toolbar>
431
+ </React.Fragment>
432
+ );
433
+
434
+ return (
435
+ <React.Fragment>
436
+ <DashboardWrapper hasPageTemplateTitle>
437
+ <PageSection isFilled>
438
+ <Card>
439
+ {toolbarItems}
440
+ <TableComposable variant="compact" aria-label="Column Management Table">
441
+ <Thead>
442
+ <Tr>
443
+ {managedColumns.map((column, columnIndex) => (
444
+ <Th key={columnIndex}>{column}</Th>
445
+ ))}
446
+ </Tr>
447
+ </Thead>
448
+ <Tbody>
449
+ {paginatedRows.map((row, rowIndex) => (
450
+ <Tr key={rowIndex}>
451
+ <>
452
+ {Object.entries(row).map(([key, value]) =>
453
+ key === 'status' ? (
454
+ <Td width={15} dataLabel="Status">
455
+ {renderLabel(value)}
456
+ </Td>
457
+ ) : key === 'url' ? (
458
+ <Td width={10} dataLabel="URL" modifier="truncate">
459
+ <TableText>
460
+ <a href="#">{row.url}</a>
461
+ </TableText>
462
+ </Td>
463
+ ) : (
464
+ <Td
465
+ width={key === 'name' ? 15 : 10}
466
+ dataLabel={
467
+ key === 'lastModified' ? 'Last modified' : capitalize(key)
468
+ }
469
+ >
470
+ {value}
471
+ </Td>
472
+ )
473
+ )}
474
+ </>
475
+ </Tr>
476
+ ))}
477
+ </Tbody>
478
+ </TableComposable>
479
+ {renderPagination()}
480
+ {renderModal()}
481
+ </Card>
482
+ </PageSection>
483
+ </DashboardWrapper>
484
+ </React.Fragment>
485
+ );
486
+ };