| 1 |
import { useState } from 'react'; |
| 2 |
import { TextInputGroup, TextInputGroupMain, TextInputGroupUtilities, Button } from '@patternfly/react-core'; |
| 3 |
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon'; |
| 4 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 5 |
|
| 6 |
export const TextInputGroupUtilitiesAndIcon: React.FunctionComponent = () => { |
| 7 |
const [inputValue, setInputValue] = useState(''); |
| 8 |
|
| 9 |
/** callback for updating the inputValue state in this component so that the input can be controlled */ |
| 10 |
const handleInputChange = (_event: React.FormEvent<HTMLInputElement>, value: string) => { |
| 11 |
setInputValue(value); |
| 12 |
}; |
| 13 |
|
| 14 |
/** show the input clearing button only when the input is not empty */ |
| 15 |
const showClearButton = !!inputValue; |
| 16 |
|
| 17 |
/** render the utilities component only when a component it contains is being rendered */ |
| 18 |
const showUtilities = showClearButton; |
| 19 |
|
| 20 |
/** callback for clearing the text input */ |
| 21 |
const clearInput = () => { |
| 22 |
setInputValue(''); |
| 23 |
}; |
| 24 |
|
| 25 |
return ( |
| 26 |
<TextInputGroup> |
| 27 |
<TextInputGroupMain icon={<SearchIcon />} value={inputValue} onChange={handleInputChange} /> |
| 28 |
{showUtilities && ( |
| 29 |
<TextInputGroupUtilities> |
| 30 |
{showClearButton && ( |
| 31 |
<Button |
| 32 |
variant="plain" |
| 33 |
onClick={clearInput} |
| 34 |
aria-label="Clear button and input" |
| 35 |
icon={<RhMicronsCloseIcon />} |
| 36 |
/> |
| 37 |
)} |
| 38 |
</TextInputGroupUtilities> |
| 39 |
)} |
| 40 |
</TextInputGroup> |
| 41 |
); |
| 42 |
}; |