@patternfly/react-core 4.237.7 → 4.237.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.
- package/CHANGELOG.md +8 -0
- package/dist/umd/react-core.min.css +2167 -2167
- package/dist/umd/react-core.min.js +1 -1
- package/package.json +6 -6
- package/src/demos/Filters/FilterDemos.md +48 -0
- package/src/demos/Filters/examples/FilterCheckboxSelect.tsx +405 -0
- package/src/demos/Filters/examples/FilterSearchInput.tsx +331 -0
- package/src/demos/Filters/examples/FilterSingleSelect.tsx +392 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
Menu,
|
|
4
|
+
MenuContent,
|
|
5
|
+
MenuList,
|
|
6
|
+
MenuItem,
|
|
7
|
+
MenuToggle,
|
|
8
|
+
MenuToggleCheckbox,
|
|
9
|
+
Popper,
|
|
10
|
+
Toolbar,
|
|
11
|
+
ToolbarContent,
|
|
12
|
+
ToolbarItem,
|
|
13
|
+
Pagination
|
|
14
|
+
} from '@patternfly/react-core';
|
|
15
|
+
import { TableComposable, Thead, Tr, Th, Tbody, Td } from '@patternfly/react-table';
|
|
16
|
+
import FilterIcon from '@patternfly/react-icons/dist/esm/icons/filter-icon';
|
|
17
|
+
|
|
18
|
+
interface Repository {
|
|
19
|
+
name: string;
|
|
20
|
+
threads: string;
|
|
21
|
+
apps: string;
|
|
22
|
+
workspaces: string;
|
|
23
|
+
status: string;
|
|
24
|
+
location: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// In real usage, this data would come from some external source like an API via props.
|
|
28
|
+
const repositories: Repository[] = [
|
|
29
|
+
{ name: 'US-Node 1', threads: '5', apps: '25', workspaces: '5', status: 'Stopped', location: 'Raleigh' },
|
|
30
|
+
{ name: 'US-Node 2', threads: '5', apps: '30', workspaces: '2', status: 'Down', location: 'Westford' },
|
|
31
|
+
{ name: 'US-Node 3', threads: '13', apps: '35', workspaces: '12', status: 'Degraded', location: 'Boston' },
|
|
32
|
+
{ name: 'US-Node 4', threads: '2', apps: '5', workspaces: '18', status: 'Needs Maintainence', location: 'Raleigh' },
|
|
33
|
+
{ name: 'US-Node 5', threads: '7', apps: '30', workspaces: '5', status: 'Running', location: 'Boston' },
|
|
34
|
+
{ name: 'US-Node 6', threads: '5', apps: '20', workspaces: '15', status: 'Stopped', location: 'Raleigh' },
|
|
35
|
+
{ name: 'CZ-Node 1', threads: '12', apps: '48', workspaces: '13', status: 'Down', location: 'Brno' },
|
|
36
|
+
{ name: 'CZ-Node 2', threads: '3', apps: '8', workspaces: '20', status: 'Running', location: 'Brno' },
|
|
37
|
+
{ name: 'CZ-Remote-Node 1', threads: '1', apps: '15', workspaces: '20', status: 'Down', location: 'Brno' },
|
|
38
|
+
{ name: 'Bangalore-Node 1', threads: '1', apps: '20', workspaces: '20', status: 'Running', location: 'Bangalore' }
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
const columnNames = {
|
|
42
|
+
name: 'Servers',
|
|
43
|
+
threads: 'Threads',
|
|
44
|
+
apps: 'Applications',
|
|
45
|
+
workspaces: 'Workspaces',
|
|
46
|
+
status: 'Status',
|
|
47
|
+
location: 'Location'
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/* eslint-disable patternfly-react/no-anonymous-functions */
|
|
51
|
+
export const FilterSingleSelect: React.FunctionComponent = () => {
|
|
52
|
+
// Set up table selection
|
|
53
|
+
const isRepoSelectable = (repo: Repository) => repo.name !== 'a'; // Arbitrary logic for this example
|
|
54
|
+
const selectableRepos = repositories.filter(isRepoSelectable);
|
|
55
|
+
|
|
56
|
+
// In this example, selected rows are tracked by the repo names from each row. This could be any unique identifier.
|
|
57
|
+
// This is to prevent state from being based on row order index in case we later add sorting.
|
|
58
|
+
const [selectedRepoNames, setSelectedRepoNames] = React.useState<string[]>([]);
|
|
59
|
+
const setRepoSelected = (repo: Repository, isSelecting = true) =>
|
|
60
|
+
setSelectedRepoNames(prevSelected => {
|
|
61
|
+
const otherSelectedRepoNames = prevSelected.filter(r => r !== repo.name);
|
|
62
|
+
return isSelecting && isRepoSelectable(repo) ? [...otherSelectedRepoNames, repo.name] : otherSelectedRepoNames;
|
|
63
|
+
});
|
|
64
|
+
const selectAllRepos = (isSelecting = true) =>
|
|
65
|
+
setSelectedRepoNames(isSelecting ? selectableRepos.map(r => r.name) : []);
|
|
66
|
+
const areAllReposSelected = selectedRepoNames.length === selectableRepos.length;
|
|
67
|
+
const areSomeReposSelected = selectedRepoNames.length > 0;
|
|
68
|
+
const isRepoSelected = (repo: Repository) => selectedRepoNames.includes(repo.name);
|
|
69
|
+
|
|
70
|
+
// To allow shift+click to select/deselect multiple rows
|
|
71
|
+
const [recentSelectedRowIndex, setRecentSelectedRowIndex] = React.useState<number | null>(null);
|
|
72
|
+
const [shifting, setShifting] = React.useState(false);
|
|
73
|
+
|
|
74
|
+
const onSelectRepo = (repo: Repository, rowIndex: number, isSelecting: boolean) => {
|
|
75
|
+
// If the user is shift + selecting the checkboxes, then all intermediate checkboxes should be selected
|
|
76
|
+
if (shifting && recentSelectedRowIndex !== null) {
|
|
77
|
+
const numberSelected = rowIndex - recentSelectedRowIndex;
|
|
78
|
+
const intermediateIndexes =
|
|
79
|
+
numberSelected > 0
|
|
80
|
+
? Array.from(new Array(numberSelected + 1), (_x, i) => i + recentSelectedRowIndex)
|
|
81
|
+
: Array.from(new Array(Math.abs(numberSelected) + 1), (_x, i) => i + rowIndex);
|
|
82
|
+
intermediateIndexes.forEach(index => setRepoSelected(repositories[index], isSelecting));
|
|
83
|
+
} else {
|
|
84
|
+
setRepoSelected(repo, isSelecting);
|
|
85
|
+
}
|
|
86
|
+
setRecentSelectedRowIndex(rowIndex);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
React.useEffect(() => {
|
|
90
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
91
|
+
if (e.key === 'Shift') {
|
|
92
|
+
setShifting(true);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const onKeyUp = (e: KeyboardEvent) => {
|
|
96
|
+
if (e.key === 'Shift') {
|
|
97
|
+
setShifting(false);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
document.addEventListener('keydown', onKeyDown);
|
|
102
|
+
document.addEventListener('keyup', onKeyUp);
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
document.removeEventListener('keydown', onKeyDown);
|
|
106
|
+
document.removeEventListener('keyup', onKeyUp);
|
|
107
|
+
};
|
|
108
|
+
}, []);
|
|
109
|
+
|
|
110
|
+
// Set up bulk selection menu
|
|
111
|
+
const bulkSelectMenuRef = React.createRef<HTMLDivElement>();
|
|
112
|
+
const bulkSelectToggleRef = React.createRef<any>();
|
|
113
|
+
const bulkSelectcontainerRef = React.createRef<HTMLDivElement>();
|
|
114
|
+
|
|
115
|
+
const [isBulkSelectOpen, setIsBulkSelectOpen] = React.useState<boolean>(false);
|
|
116
|
+
|
|
117
|
+
React.useEffect(() => {
|
|
118
|
+
window.addEventListener('keydown', handleBulkSelectMenuKeys);
|
|
119
|
+
window.addEventListener('click', handleBulkSelectClickOutside);
|
|
120
|
+
return () => {
|
|
121
|
+
window.removeEventListener('keydown', handleBulkSelectMenuKeys);
|
|
122
|
+
window.removeEventListener('click', handleBulkSelectClickOutside);
|
|
123
|
+
};
|
|
124
|
+
}, [isBulkSelectOpen, bulkSelectMenuRef]);
|
|
125
|
+
|
|
126
|
+
const handleBulkSelectClickOutside = (event: MouseEvent) => {
|
|
127
|
+
if (isBulkSelectOpen && !bulkSelectMenuRef.current?.contains(event.target as Node)) {
|
|
128
|
+
setIsBulkSelectOpen(false);
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const handleBulkSelectMenuKeys = (event: KeyboardEvent) => {
|
|
133
|
+
if (!isBulkSelectOpen) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (
|
|
137
|
+
bulkSelectMenuRef.current?.contains(event.target as Node) ||
|
|
138
|
+
bulkSelectToggleRef.current?.contains(event.target as Node)
|
|
139
|
+
) {
|
|
140
|
+
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
141
|
+
setIsBulkSelectOpen(!isBulkSelectOpen);
|
|
142
|
+
bulkSelectToggleRef.current?.focus();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const onBulkSelectToggleClick = (ev: React.MouseEvent) => {
|
|
148
|
+
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
149
|
+
setTimeout(() => {
|
|
150
|
+
if (bulkSelectMenuRef.current) {
|
|
151
|
+
const firstElement = bulkSelectMenuRef.current.querySelector('li > button:not(:disabled)');
|
|
152
|
+
firstElement && (firstElement as HTMLElement).focus();
|
|
153
|
+
}
|
|
154
|
+
}, 0);
|
|
155
|
+
setIsBulkSelectOpen(!isBulkSelectOpen);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
let menuToggleCheckmark: boolean | null = false;
|
|
159
|
+
if (areAllReposSelected) {
|
|
160
|
+
menuToggleCheckmark = true;
|
|
161
|
+
} else if (areSomeReposSelected) {
|
|
162
|
+
menuToggleCheckmark = null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const bulkSelectToggle = (
|
|
166
|
+
<MenuToggle
|
|
167
|
+
ref={bulkSelectToggleRef}
|
|
168
|
+
onClick={onBulkSelectToggleClick}
|
|
169
|
+
isExpanded={isBulkSelectOpen}
|
|
170
|
+
splitButtonOptions={{
|
|
171
|
+
items: [
|
|
172
|
+
<MenuToggleCheckbox
|
|
173
|
+
id="select-checkbox"
|
|
174
|
+
key="select-checkbox"
|
|
175
|
+
aria-label="Select all"
|
|
176
|
+
isChecked={menuToggleCheckmark}
|
|
177
|
+
onChange={(checked, _event) => selectAllRepos(checked)}
|
|
178
|
+
/>
|
|
179
|
+
]
|
|
180
|
+
}}
|
|
181
|
+
aria-label="Full table selection checkbox"
|
|
182
|
+
/>
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
const bulkSelectMenu = (
|
|
186
|
+
// eslint-disable-next-line no-console
|
|
187
|
+
<Menu
|
|
188
|
+
ref={bulkSelectMenuRef}
|
|
189
|
+
onSelect={(_ev, itemId) => {
|
|
190
|
+
selectAllRepos(itemId === 1 || itemId === 2);
|
|
191
|
+
setIsBulkSelectOpen(!isBulkSelectOpen);
|
|
192
|
+
}}
|
|
193
|
+
>
|
|
194
|
+
<MenuContent>
|
|
195
|
+
<MenuList>
|
|
196
|
+
<MenuItem itemId={0}>Select none (0 items)</MenuItem>
|
|
197
|
+
<MenuItem itemId={1}>Select page ({repositories.length} items)</MenuItem>
|
|
198
|
+
<MenuItem itemId={2}>Select all ({repositories.length} items)</MenuItem>
|
|
199
|
+
</MenuList>
|
|
200
|
+
</MenuContent>
|
|
201
|
+
</Menu>
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const toolbarBulkSelect = (
|
|
205
|
+
<div ref={bulkSelectcontainerRef}>
|
|
206
|
+
<Popper
|
|
207
|
+
trigger={bulkSelectToggle}
|
|
208
|
+
popper={bulkSelectMenu}
|
|
209
|
+
appendTo={bulkSelectcontainerRef.current || undefined}
|
|
210
|
+
isVisible={isBulkSelectOpen}
|
|
211
|
+
popperMatchesTriggerWidth={false}
|
|
212
|
+
/>
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
|
|
216
|
+
// Set up table filtering
|
|
217
|
+
const [searchValue, setSearchValue] = React.useState('');
|
|
218
|
+
|
|
219
|
+
const onFilter = (repo: Repository) => {
|
|
220
|
+
if (searchValue === 'all') {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let input: RegExp;
|
|
225
|
+
try {
|
|
226
|
+
input = new RegExp(searchValue, 'i');
|
|
227
|
+
} catch (err) {
|
|
228
|
+
input = new RegExp(searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
|
|
229
|
+
}
|
|
230
|
+
return repo.status.search(input) >= 0;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// Set up single select menu & state
|
|
234
|
+
const [isOpen, setIsOpen] = React.useState<boolean>(false);
|
|
235
|
+
const [menuSelection, setMenuSelection] = React.useState<string>('all');
|
|
236
|
+
const toggleRef = React.useRef<HTMLButtonElement>(null);
|
|
237
|
+
const menuRef = React.useRef<HTMLDivElement>(null);
|
|
238
|
+
const containerRef = React.useRef<HTMLDivElement>(null);
|
|
239
|
+
|
|
240
|
+
const handleMenuKeys = (event: KeyboardEvent) => {
|
|
241
|
+
if (isOpen && menuRef.current?.contains(event.target as Node)) {
|
|
242
|
+
if (event.key === 'Escape' || event.key === 'Tab') {
|
|
243
|
+
setIsOpen(!isOpen);
|
|
244
|
+
toggleRef.current?.focus();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const handleClickOutside = (event: MouseEvent) => {
|
|
250
|
+
if (isOpen && !menuRef.current?.contains(event.target as Node)) {
|
|
251
|
+
setIsOpen(false);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
React.useEffect(() => {
|
|
256
|
+
window.addEventListener('keydown', handleMenuKeys);
|
|
257
|
+
window.addEventListener('click', handleClickOutside);
|
|
258
|
+
return () => {
|
|
259
|
+
window.removeEventListener('keydown', handleMenuKeys);
|
|
260
|
+
window.removeEventListener('click', handleClickOutside);
|
|
261
|
+
};
|
|
262
|
+
}, [isOpen, menuRef]);
|
|
263
|
+
|
|
264
|
+
const onToggleClick = (ev: React.MouseEvent) => {
|
|
265
|
+
ev.stopPropagation(); // Stop handleClickOutside from handling
|
|
266
|
+
setTimeout(() => {
|
|
267
|
+
if (menuRef.current) {
|
|
268
|
+
const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
|
|
269
|
+
firstElement && (firstElement as HTMLElement).focus();
|
|
270
|
+
}
|
|
271
|
+
}, 0);
|
|
272
|
+
setIsOpen(!isOpen);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
function onSelect(event: React.MouseEvent | undefined, itemId: string | number | undefined) {
|
|
276
|
+
if (typeof itemId === 'undefined') {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
setMenuSelection(itemId.toString());
|
|
281
|
+
setSearchValue(itemId.toString());
|
|
282
|
+
setIsOpen(!isOpen);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const menuToggleDisplay = {
|
|
286
|
+
all: 'All statuses',
|
|
287
|
+
down: 'Down',
|
|
288
|
+
stopped: 'Stopped',
|
|
289
|
+
degraded: 'Degraded',
|
|
290
|
+
running: 'Running',
|
|
291
|
+
maint: 'Needs maintenance'
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
const toggle = (
|
|
295
|
+
<MenuToggle
|
|
296
|
+
ref={toggleRef}
|
|
297
|
+
onClick={onToggleClick}
|
|
298
|
+
isExpanded={isOpen}
|
|
299
|
+
icon={<FilterIcon />}
|
|
300
|
+
style={
|
|
301
|
+
{
|
|
302
|
+
width: '200px'
|
|
303
|
+
} as React.CSSProperties
|
|
304
|
+
}
|
|
305
|
+
>
|
|
306
|
+
{menuToggleDisplay[menuSelection]}
|
|
307
|
+
</MenuToggle>
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
const menu = (
|
|
311
|
+
<Menu ref={menuRef} id="select-menu" onSelect={onSelect} selected={menuSelection}>
|
|
312
|
+
<MenuContent>
|
|
313
|
+
<MenuList>
|
|
314
|
+
<MenuItem itemId="all">All statuses</MenuItem>
|
|
315
|
+
<MenuItem itemId="stopped">Stopped</MenuItem>
|
|
316
|
+
<MenuItem itemId="down">Down</MenuItem>
|
|
317
|
+
<MenuItem itemId="degraded">Degraded</MenuItem>
|
|
318
|
+
<MenuItem itemId="running">Running</MenuItem>
|
|
319
|
+
<MenuItem itemId="maint">Needs maintenance</MenuItem>
|
|
320
|
+
</MenuList>
|
|
321
|
+
</MenuContent>
|
|
322
|
+
</Menu>
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
const select = (
|
|
326
|
+
<div ref={containerRef}>
|
|
327
|
+
<Popper trigger={toggle} popper={menu} appendTo={containerRef.current || undefined} isVisible={isOpen} />
|
|
328
|
+
</div>
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
// Set up pagination
|
|
332
|
+
const toolbarPagination = (
|
|
333
|
+
<Pagination
|
|
334
|
+
titles={{ paginationTitle: 'Single select filter pagination' }}
|
|
335
|
+
perPageComponent="button"
|
|
336
|
+
itemCount={repositories.length}
|
|
337
|
+
perPage={10}
|
|
338
|
+
page={1}
|
|
339
|
+
widgetId="single-select-mock-pagination"
|
|
340
|
+
isCompact
|
|
341
|
+
/>
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
const toolbar = (
|
|
345
|
+
<Toolbar id="single-select-filter-toolbar">
|
|
346
|
+
<ToolbarContent>
|
|
347
|
+
<ToolbarItem>{toolbarBulkSelect}</ToolbarItem>
|
|
348
|
+
<ToolbarItem>{select}</ToolbarItem>
|
|
349
|
+
<ToolbarItem variant="pagination">{toolbarPagination}</ToolbarItem>
|
|
350
|
+
</ToolbarContent>
|
|
351
|
+
</Toolbar>
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
return (
|
|
355
|
+
<React.Fragment>
|
|
356
|
+
{toolbar}
|
|
357
|
+
<TableComposable aria-label="Selectable table">
|
|
358
|
+
<Thead>
|
|
359
|
+
<Tr>
|
|
360
|
+
<Th />
|
|
361
|
+
<Th width={20}>{columnNames.name}</Th>
|
|
362
|
+
<Th width={20}>{columnNames.threads}</Th>
|
|
363
|
+
<Th width={20}>{columnNames.apps}</Th>
|
|
364
|
+
<Th width={20}>{columnNames.workspaces}</Th>
|
|
365
|
+
<Th width={20}>{columnNames.status}</Th>
|
|
366
|
+
<Th width={20}>{columnNames.location}</Th>
|
|
367
|
+
</Tr>
|
|
368
|
+
</Thead>
|
|
369
|
+
<Tbody>
|
|
370
|
+
{repositories.filter(onFilter).map((repo, rowIndex) => (
|
|
371
|
+
<Tr key={repo.name}>
|
|
372
|
+
<Td
|
|
373
|
+
select={{
|
|
374
|
+
rowIndex,
|
|
375
|
+
onSelect: (_event, isSelecting) => onSelectRepo(repo, rowIndex, isSelecting),
|
|
376
|
+
isSelected: isRepoSelected(repo),
|
|
377
|
+
disable: !isRepoSelectable(repo)
|
|
378
|
+
}}
|
|
379
|
+
/>
|
|
380
|
+
<Td dataLabel={columnNames.name}>{repo.name}</Td>
|
|
381
|
+
<Td dataLabel={columnNames.threads}>{repo.threads}</Td>
|
|
382
|
+
<Td dataLabel={columnNames.apps}>{repo.apps}</Td>
|
|
383
|
+
<Td dataLabel={columnNames.workspaces}>{repo.workspaces}</Td>
|
|
384
|
+
<Td dataLabel={columnNames.status}>{repo.status}</Td>
|
|
385
|
+
<Td dataLabel={columnNames.location}>{repo.location}</Td>
|
|
386
|
+
</Tr>
|
|
387
|
+
))}
|
|
388
|
+
</Tbody>
|
|
389
|
+
</TableComposable>
|
|
390
|
+
</React.Fragment>
|
|
391
|
+
);
|
|
392
|
+
};
|