@patternfly/react-core 4.178.0 → 4.179.0

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.
@@ -86,8 +86,6 @@ class CardViewBasic extends React.Component {
86
86
  constructor(props) {
87
87
  super(props);
88
88
 
89
- this.handleCheckboxClick = this.handleCheckboxClick.bind(this);
90
-
91
89
  this.state = {
92
90
  filters: {
93
91
  products: []
@@ -199,6 +197,39 @@ class CardViewBasic extends React.Component {
199
197
  });
200
198
  }
201
199
  };
200
+
201
+ this.onKeyDown = (event, productId) => {
202
+ console.log(productId);
203
+ if (event.target !== event.currentTarget) {
204
+ return;
205
+ }
206
+ if ([' ', 'Enter'].includes(event.key)) {
207
+ event.preventDefault();
208
+ this.setState(prevState => {
209
+ return prevState.selectedItems.includes(productId*1) ?
210
+ {
211
+ selectedItems: [...prevState.selectedItems.filter(id => productId * 1 != id)],
212
+ areAllSelected: false
213
+ } : {
214
+ selectedItems: [...prevState.selectedItems, productId * 1],
215
+ areAllSelected: prevState.totalItemCount === prevState.selectedItems.length + 1
216
+ };
217
+ });
218
+ }
219
+ };
220
+
221
+ this.onClick = productId => {
222
+ this.setState(prevState => {
223
+ return prevState.selectedItems.includes(productId*1) ?
224
+ {
225
+ selectedItems: [...prevState.selectedItems.filter(id => productId * 1 != id)],
226
+ areAllSelected: false
227
+ } : {
228
+ selectedItems: [...prevState.selectedItems, productId * 1],
229
+ areAllSelected: prevState.totalItemCount === prevState.selectedItems.length + 1
230
+ };
231
+ });
232
+ };
202
233
  }
203
234
 
204
235
  selectedItems(e) {
@@ -294,24 +325,6 @@ class CardViewBasic extends React.Component {
294
325
  return collection;
295
326
  }
296
327
 
297
- handleCheckboxClick(checked, e) {
298
- const { value } = e.target;
299
- const { totalItemCount } = this.state;
300
-
301
- if (checked) {
302
- const collection = this.getAllItems();
303
- this.setState(prevState => ({
304
- selectedItems: [...prevState.selectedItems, value * 1],
305
- areAllSelected: totalItemCount === prevState.selectedItems.length + 1
306
- }));
307
- } else {
308
- this.setState(prevState => ({
309
- selectedItems: prevState.selectedItems.filter(item => item != value),
310
- areAllSelected: false
311
- }));
312
- }
313
- }
314
-
315
328
  updateSelected() {
316
329
  const { res, selectedItems } = this.state;
317
330
  let rows = res.map(post => {
@@ -547,7 +560,11 @@ class CardViewBasic extends React.Component {
547
560
  </PageSection>
548
561
  <PageSection isFilled>
549
562
  <Gallery hasGutter>
550
- <Card isHoverable isCompact>
563
+ <Card
564
+ isSelectable
565
+ selectableVariant="raised"
566
+ isCompact
567
+ >
551
568
  <Bullseye>
552
569
  <EmptyState variant={EmptyStateVariant.xs}>
553
570
  <EmptyStateIcon icon={PlusCircleIcon} />
@@ -561,7 +578,15 @@ class CardViewBasic extends React.Component {
561
578
  </Bullseye>
562
579
  </Card>
563
580
  {filtered.map((product, key) => (
564
- <Card isHoverable isCompact key={product.name}>
581
+ <Card
582
+ isSelectable
583
+ selectableVariant="raised"
584
+ isCompact
585
+ key={product.name}
586
+ onKeyDown={(e) => this.onKeyDown(e, product.id)}
587
+ onClick={() => this.onClick(product.id)}
588
+ isSelected={selectedItems.includes(product.id)}
589
+ >
565
590
  <CardHeader>
566
591
  <img src={icons[product.icon]} alt={`${product.name} icon`} style={{ maxWidth: '60px' }} />
567
592
  <CardActions>
@@ -587,7 +612,6 @@ class CardViewBasic extends React.Component {
587
612
  <Checkbox
588
613
  checked={isChecked}
589
614
  value={product.id}
590
- onChange={this.handleCheckboxClick}
591
615
  isChecked={selectedItems.includes(product.id)}
592
616
  aria-label="card checkbox example"
593
617
  id={`check-${product.id}`}
@@ -24,255 +24,17 @@ Additionally, attributes can be selected by typing the full (case sensitive) nam
24
24
 
25
25
  Attributes can be deselected (returning you to attribute selection mode) by hitting `escape`, or by hitting `backspace` when the only text in the text input is the attribute.
26
26
 
27
- ```js
28
- import React from 'react';
29
- import {
30
- TextInputGroup,
31
- TextInputGroupMain,
32
- TextInputGroupUtilities,
33
- Button,
34
- Menu,
35
- MenuContent,
36
- MenuList,
37
- MenuItem,
38
- Popper,
39
- Chip,
40
- ChipGroup,
41
- Divider
42
- } from '@patternfly/react-core';
43
- import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
44
- import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
45
-
46
- export const KeyValueFiltering = () => {
47
- const [inputValue, setInputValue] = React.useState('');
48
- const [selectedKey, setSelectedKey] = React.useState('');
49
- const [menuIsOpen, setMenuIsOpen] = React.useState(false);
50
- const [currentChips, setCurrentChips] = React.useState([]);
51
-
52
- /** key and value data to be shown in the menu */
53
- const data = {
54
- Cluster: ['acmeqe-managed-1', 'local-cluster'],
55
- Kind: ['Template', 'ReplicationController', 'ReplicaSet', 'Deployment'],
56
- Label: ['release', 'environment', 'partition'],
57
- Name: ['backup-1', 'backup-2', 'production-1', 'production-2', 'testing'],
58
- Namespace: ['default', 'public'],
59
- Status: ['running', 'idle', 'stopped']
60
- };
61
- const keyNames = ['Cluster', 'Kind', 'Label', 'Name', 'Namespace', 'Status'];
62
- const [menuItemsText, setMenuItemsText] = React.useState(keyNames);
63
- const [menuItems, setMenuItems] = React.useState([]);
64
-
65
- /** refs used to detect when clicks occur inside vs outside of the textInputGroup and menu popper */
66
- const menuRef = React.useRef();
67
- const textInputGroupRef = React.useRef();
68
-
69
- /** callback for updating the inputValue state in this component so that the input can be controlled */
70
- const handleInputChange = (value, _event) => {
71
- setInputValue(value);
72
- };
73
-
74
- /** callback for removing a chip from the chip selections */
75
- const deleteChip = chipToDelete => {
76
- const newChips = currentChips.filter(chip => !Object.is(chip, chipToDelete));
77
- setCurrentChips(newChips);
78
- };
79
-
80
- /** reset state hooks associated with key selection */
81
- const clearSelectedKey = () => {
82
- setInputValue('');
83
- setSelectedKey('');
84
- setMenuItemsText(keyNames);
85
- };
86
-
87
- /** callback for clearing all selected chips, the text input, and any selected keys */
88
- const clearChipsAndInput = () => {
89
- setCurrentChips([]);
90
- clearSelectedKey();
91
- };
92
-
93
- React.useEffect(() => {
94
- /** in the menu only show items that include the text in the input */
95
- const filteredMenuItems = menuItemsText
96
- .filter(
97
- item =>
98
- !inputValue ||
99
- item.toLowerCase().includes(
100
- inputValue
101
- .toString()
102
- .slice(selectedKey.length && selectedKey.length + 2)
103
- .toLowerCase()
104
- )
105
- )
106
- .map((currentValue, index) => (
107
- <MenuItem key={currentValue} itemId={index}>
108
- {currentValue}
109
- </MenuItem>
110
- ));
111
-
112
- /** in the menu show a disabled "no result" when all menu items are filtered out */
113
- if (filteredMenuItems.length === 0) {
114
- const noResultItem = (
115
- <MenuItem isDisabled key="no result">
116
- No results found
117
- </MenuItem>
118
- );
119
- setMenuItems([noResultItem]);
120
- return;
121
- }
122
-
123
- /** determine the menu heading text based on key selection; or lack thereof */
124
- const headingItem = (
125
- <MenuItem isDisabled key="heading">
126
- {selectedKey.length ? `${selectedKey} values` : 'Attributes'}
127
- </MenuItem>
128
- );
129
-
130
- const divider = <Divider key="divider" />;
131
-
132
- setMenuItems([headingItem, divider, ...filteredMenuItems]);
133
- }, [inputValue]);
134
-
135
- /** add selected key/value pair as a chip in the chip group */
136
- const selectValue = selectedValue => {
137
- setCurrentChips([...currentChips, `${selectedKey}: ${selectedValue}`]);
138
- clearSelectedKey();
139
- };
140
-
141
- /** update the input to show the selected key and the menu to show the values associated with that specific key */
142
- const selectKey = selectedText => {
143
- setInputValue(`${selectedText}: `);
144
- setSelectedKey(selectedText);
145
- setMenuItemsText(data[selectedText]);
146
- };
147
-
148
- const handleEnter = () => {
149
- /** do nothing if the menu contains no real results */
150
- if (menuItems.length === 1) {
151
- return;
152
- }
153
-
154
- /** perform the appropriate action based on key selection state */
155
- if (selectedKey.length) {
156
- selectValue(menuItems[2].props.children);
157
- } else {
158
- selectKey(menuItems[2].props.children);
159
- }
160
- };
161
-
162
- /** allow the user to backspace at the selected key name to drop the currently selected key */
163
- const handleBackspace = () => {
164
- if (selectedKey.length && inputValue === `${selectedKey}: `) {
165
- clearSelectedKey();
166
- }
167
- };
168
-
169
- /** allow the user to select a key by simply typing it and entering a colon, exact (case sensitive) matches only */
170
- const handleColon = () => {
171
- if (!selectedKey.length && keyNames.includes(inputValue)) {
172
- selectKey(inputValue);
173
- event.preventDefault();
174
- }
175
- };
176
-
177
- /** allow the user to focus on the menu and navigate using the arrow keys */
178
- const handleArrowKey = () => {
179
- if (menuRef.current) {
180
- const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
181
- firstElement && firstElement.focus();
182
- }
183
- };
184
-
185
- /** enable keyboard only usage */
186
- const handleTextInputKeyDown = event => {
187
- switch (event.key) {
188
- case 'Enter':
189
- handleEnter();
190
- break;
191
- case 'Escape':
192
- clearSelectedKey();
193
- break;
194
- case 'Backspace':
195
- handleBackspace();
196
- break;
197
- case ':':
198
- handleColon();
199
- break;
200
- case 'ArrowUp':
201
- case 'ArrowDown':
202
- handleArrowKey();
203
- break;
204
- }
205
- };
206
-
207
- /** perform the proper key or value selection when a menu item is selected */
208
- const onSelect = (event, _itemId) => {
209
- const selectedText = event.target.innerText;
210
-
211
- if (selectedKey.length) {
212
- selectValue(selectedText);
213
- } else {
214
- selectKey(selectedText);
215
- }
216
- event.stopPropagation();
217
- textInputGroupRef.current.querySelector('input').focus();
218
- };
219
-
220
- /** close the menu when a click occurs outside of the menu or text input group */
221
- const handleClick = event => {
222
- if (
223
- menuRef.current &&
224
- !menuRef.current.contains(event.target) &&
225
- !textInputGroupRef.current.contains(event.target)
226
- ) {
227
- setMenuIsOpen(false);
228
- }
229
- };
27
+ ```js file="./examples/TextInputGroup/AttributeValueFiltering.js"
28
+ ```
29
+ ### Auto-complete search
230
30
 
231
- /** only show the search icon when no chips are selected */
232
- const showSearchIcon = !currentChips.length;
31
+ This demo showcases a search input with suggestions, which filters possible selections based on the text you've entered. Unlike the attribute-value filtering demo, it allows creation of new chip items when the text entered is not available in the list of suggestions.
233
32
 
234
- /** only show the clear button when there is something that can be cleared */
235
- const showClearButton = inputValue || !!currentChips.length;
33
+ The current text in the input can be converted to a chip at any time by hitting `enter`. Auto-complete suggestions can be chosen by clicking the corresponding entry in the menu, or by navigating to an entry using the up/down arrow keys and selecting it with `enter`.
236
34
 
237
- const inputGroup = (
238
- <div ref={textInputGroupRef}>
239
- <TextInputGroup>
240
- <TextInputGroupMain
241
- icon={showSearchIcon && <SearchIcon />}
242
- value={inputValue}
243
- onChange={handleInputChange}
244
- onFocus={() => setMenuIsOpen(true)}
245
- onKeyDown={handleTextInputKeyDown}
246
- >
247
- <ChipGroup>
248
- {currentChips.map(currentChip => (
249
- <Chip key={currentChip} onClick={() => deleteChip(currentChip)}>
250
- {currentChip}
251
- </Chip>
252
- ))}
253
- </ChipGroup>
254
- </TextInputGroupMain>
255
- <TextInputGroupUtilities>
256
- {showClearButton && (
257
- <Button variant="plain" onClick={clearChipsAndInput} aria-label="Clear button and input">
258
- <TimesIcon />
259
- </Button>
260
- )}
261
- </TextInputGroupUtilities>
262
- </TextInputGroup>
263
- </div>
264
- );
35
+ Hitting `escape` while focused on the input or menu will close the menu, and the menu will reopen when text is entered.
265
36
 
266
- const menu = (
267
- <div ref={menuRef}>
268
- <Menu onSelect={onSelect}>
269
- <MenuContent>
270
- <MenuList>{menuItems}</MenuList>
271
- </MenuContent>
272
- </Menu>
273
- </div>
274
- );
37
+ When only one item remains in the suggestion list, tab can be used to auto-complete the typing of that item.
275
38
 
276
- return <Popper trigger={inputGroup} popper={menu} isVisible={menuIsOpen} onDocumentClick={handleClick} />;
277
- };
39
+ ```js file="./examples/TextInputGroup/AutoCompleteSearch.js"
278
40
  ```
@@ -0,0 +1,250 @@
1
+ import React from 'react';
2
+ import {
3
+ TextInputGroup,
4
+ TextInputGroupMain,
5
+ TextInputGroupUtilities,
6
+ Button,
7
+ Menu,
8
+ MenuContent,
9
+ MenuList,
10
+ MenuItem,
11
+ Popper,
12
+ Chip,
13
+ ChipGroup,
14
+ Divider
15
+ } from '@patternfly/react-core';
16
+ import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon';
17
+ import TimesIcon from '@patternfly/react-icons/dist/esm/icons/times-icon';
18
+
19
+ export const AttributeValueFiltering = () => {
20
+ const [inputValue, setInputValue] = React.useState('');
21
+ const [selectedKey, setSelectedKey] = React.useState('');
22
+ const [menuIsOpen, setMenuIsOpen] = React.useState(false);
23
+ const [currentChips, setCurrentChips] = React.useState([]);
24
+
25
+ /** key and value data to be shown in the menu */
26
+ const data = {
27
+ Cluster: ['acmeqe-managed-1', 'local-cluster'],
28
+ Kind: ['Template', 'ReplicationController', 'ReplicaSet', 'Deployment'],
29
+ Label: ['release', 'environment', 'partition'],
30
+ Name: ['backup-1', 'backup-2', 'production-1', 'production-2', 'testing'],
31
+ Namespace: ['default', 'public'],
32
+ Status: ['running', 'idle', 'stopped']
33
+ };
34
+ const keyNames = ['Cluster', 'Kind', 'Label', 'Name', 'Namespace', 'Status'];
35
+ const [menuItemsText, setMenuItemsText] = React.useState(keyNames);
36
+ const [menuItems, setMenuItems] = React.useState([]);
37
+
38
+ /** refs used to detect when clicks occur inside vs outside of the textInputGroup and menu popper */
39
+ const menuRef = React.useRef();
40
+ const textInputGroupRef = React.useRef();
41
+
42
+ /** callback for updating the inputValue state in this component so that the input can be controlled */
43
+ const handleInputChange = (value, _event) => {
44
+ setInputValue(value);
45
+ };
46
+
47
+ /** callback for removing a chip from the chip selections */
48
+ const deleteChip = chipToDelete => {
49
+ const newChips = currentChips.filter(chip => !Object.is(chip, chipToDelete));
50
+ setCurrentChips(newChips);
51
+ };
52
+
53
+ /** reset state hooks associated with key selection */
54
+ const clearSelectedKey = () => {
55
+ setInputValue('');
56
+ setSelectedKey('');
57
+ setMenuItemsText(keyNames);
58
+ };
59
+
60
+ /** callback for clearing all selected chips, the text input, and any selected keys */
61
+ const clearChipsAndInput = () => {
62
+ setCurrentChips([]);
63
+ clearSelectedKey();
64
+ };
65
+
66
+ React.useEffect(() => {
67
+ /** in the menu only show items that include the text in the input */
68
+ const filteredMenuItems = menuItemsText
69
+ .filter(
70
+ item =>
71
+ !inputValue ||
72
+ item.toLowerCase().includes(
73
+ inputValue
74
+ .toString()
75
+ .slice(selectedKey.length && selectedKey.length + 2)
76
+ .toLowerCase()
77
+ )
78
+ )
79
+ .map((currentValue, index) => (
80
+ <MenuItem key={currentValue} itemId={index}>
81
+ {currentValue}
82
+ </MenuItem>
83
+ ));
84
+
85
+ /** in the menu show a disabled "no result" when all menu items are filtered out */
86
+ if (filteredMenuItems.length === 0) {
87
+ const noResultItem = (
88
+ <MenuItem isDisabled key="no result">
89
+ No results found
90
+ </MenuItem>
91
+ );
92
+ setMenuItems([noResultItem]);
93
+ return;
94
+ }
95
+
96
+ /** determine the menu heading text based on key selection; or lack thereof */
97
+ const headingItem = (
98
+ <MenuItem isDisabled key="heading">
99
+ {selectedKey.length ? `${selectedKey} values` : 'Attributes'}
100
+ </MenuItem>
101
+ );
102
+
103
+ const divider = <Divider key="divider" />;
104
+
105
+ setMenuItems([headingItem, divider, ...filteredMenuItems]);
106
+ }, [inputValue]);
107
+
108
+ /** add selected key/value pair as a chip in the chip group */
109
+ const selectValue = selectedValue => {
110
+ setCurrentChips([...currentChips, `${selectedKey}: ${selectedValue}`]);
111
+ clearSelectedKey();
112
+ };
113
+
114
+ /** update the input to show the selected key and the menu to show the values associated with that specific key */
115
+ const selectKey = selectedText => {
116
+ setInputValue(`${selectedText}: `);
117
+ setSelectedKey(selectedText);
118
+ setMenuItemsText(data[selectedText]);
119
+ };
120
+
121
+ const handleEnter = () => {
122
+ /** do nothing if the menu contains no real results */
123
+ if (menuItems.length === 1) {
124
+ return;
125
+ }
126
+
127
+ /** perform the appropriate action based on key selection state */
128
+ if (selectedKey.length) {
129
+ selectValue(menuItems[2].props.children);
130
+ } else {
131
+ selectKey(menuItems[2].props.children);
132
+ }
133
+ };
134
+
135
+ /** allow the user to backspace at the selected key name to drop the currently selected key */
136
+ const handleBackspace = () => {
137
+ if (selectedKey.length && inputValue === `${selectedKey}: `) {
138
+ clearSelectedKey();
139
+ }
140
+ };
141
+
142
+ /** allow the user to select a key by simply typing it and entering a colon, exact (case sensitive) matches only */
143
+ const handleColon = () => {
144
+ if (!selectedKey.length && keyNames.includes(inputValue)) {
145
+ selectKey(inputValue);
146
+ event.preventDefault();
147
+ }
148
+ };
149
+
150
+ /** allow the user to focus on the menu and navigate using the arrow keys */
151
+ const handleArrowKey = () => {
152
+ if (menuRef.current) {
153
+ const firstElement = menuRef.current.querySelector('li > button:not(:disabled)');
154
+ firstElement && firstElement.focus();
155
+ }
156
+ };
157
+
158
+ /** enable keyboard only usage */
159
+ const handleTextInputKeyDown = event => {
160
+ switch (event.key) {
161
+ case 'Enter':
162
+ handleEnter();
163
+ break;
164
+ case 'Escape':
165
+ clearSelectedKey();
166
+ break;
167
+ case 'Backspace':
168
+ handleBackspace();
169
+ break;
170
+ case ':':
171
+ handleColon();
172
+ break;
173
+ case 'ArrowUp':
174
+ case 'ArrowDown':
175
+ handleArrowKey();
176
+ break;
177
+ }
178
+ };
179
+
180
+ /** perform the proper key or value selection when a menu item is selected */
181
+ const onSelect = (event, _itemId) => {
182
+ const selectedText = event.target.innerText;
183
+
184
+ if (selectedKey.length) {
185
+ selectValue(selectedText);
186
+ } else {
187
+ selectKey(selectedText);
188
+ }
189
+ event.stopPropagation();
190
+ textInputGroupRef.current.querySelector('input').focus();
191
+ };
192
+
193
+ /** close the menu when a click occurs outside of the menu or text input group */
194
+ const handleClick = event => {
195
+ if (
196
+ menuRef.current &&
197
+ !menuRef.current.contains(event.target) &&
198
+ !textInputGroupRef.current.contains(event.target)
199
+ ) {
200
+ setMenuIsOpen(false);
201
+ }
202
+ };
203
+
204
+ /** only show the search icon when no chips are selected */
205
+ const showSearchIcon = !currentChips.length;
206
+
207
+ /** only show the clear button when there is something that can be cleared */
208
+ const showClearButton = inputValue || !!currentChips.length;
209
+
210
+ const inputGroup = (
211
+ <div ref={textInputGroupRef}>
212
+ <TextInputGroup>
213
+ <TextInputGroupMain
214
+ icon={showSearchIcon && <SearchIcon />}
215
+ value={inputValue}
216
+ onChange={handleInputChange}
217
+ onFocus={() => setMenuIsOpen(true)}
218
+ onKeyDown={handleTextInputKeyDown}
219
+ >
220
+ <ChipGroup>
221
+ {currentChips.map(currentChip => (
222
+ <Chip key={currentChip} onClick={() => deleteChip(currentChip)}>
223
+ {currentChip}
224
+ </Chip>
225
+ ))}
226
+ </ChipGroup>
227
+ </TextInputGroupMain>
228
+ <TextInputGroupUtilities>
229
+ {showClearButton && (
230
+ <Button variant="plain" onClick={clearChipsAndInput} aria-label="Clear button and input">
231
+ <TimesIcon />
232
+ </Button>
233
+ )}
234
+ </TextInputGroupUtilities>
235
+ </TextInputGroup>
236
+ </div>
237
+ );
238
+
239
+ const menu = (
240
+ <div ref={menuRef}>
241
+ <Menu onSelect={onSelect}>
242
+ <MenuContent>
243
+ <MenuList>{menuItems}</MenuList>
244
+ </MenuContent>
245
+ </Menu>
246
+ </div>
247
+ );
248
+
249
+ return <Popper trigger={inputGroup} popper={menu} isVisible={menuIsOpen} onDocumentClick={handleClick} />;
250
+ };