| 1 |
import { useState } from 'react'; |
| 2 |
import { |
| 3 |
TextInputGroup, |
| 4 |
TextInputGroupMain, |
| 5 |
TextInputGroupUtilities, |
| 6 |
Button, |
| 7 |
ValidatedOptions, |
| 8 |
Flex, |
| 9 |
FlexItem |
| 10 |
} from '@patternfly/react-core'; |
| 11 |
import SearchIcon from '@patternfly/react-icons/dist/esm/icons/search-icon'; |
| 12 |
import RhMicronsCloseIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-close-icon'; |
| 13 |
|
| 14 |
export const TextInputGroupWithStatus: React.FunctionComponent = () => { |
| 15 |
const [successValue, setSuccessValue] = useState('Success validation'); |
| 16 |
const [warningValue, setWarningValue] = useState('Warning validation with custom non-status icon at start'); |
| 17 |
const [errorValue, setErrorValue] = useState('Error validation with custom non-status icon at start and utilities'); |
| 18 |
|
| 19 |
/** show the input clearing button only when the input is not empty */ |
| 20 |
const showClearButton = !!errorValue; |
| 21 |
|
| 22 |
/** render the utilities component only when a component it contains is being rendered */ |
| 23 |
const showUtilities = showClearButton; |
| 24 |
|
| 25 |
/** callback for clearing the text input */ |
| 26 |
const clearInput = () => { |
| 27 |
setErrorValue(''); |
| 28 |
}; |
| 29 |
|
| 30 |
return ( |
| 31 |
<Flex direction={{ default: 'column' }} rowGap={{ default: 'rowGapSm' }}> |
| 32 |
<FlexItem> |
| 33 |
<TextInputGroup validated={ValidatedOptions.success}> |
| 34 |
<TextInputGroupMain value={successValue} onChange={(_event, value) => setSuccessValue(value)} /> |
| 35 |
</TextInputGroup> |
| 36 |
</FlexItem> |
| 37 |
<FlexItem> |
| 38 |
<TextInputGroup validated={ValidatedOptions.warning}> |
| 39 |
<TextInputGroupMain |
| 40 |
icon={<SearchIcon />} |
| 41 |
value={warningValue} |
| 42 |
onChange={(_event, value) => setWarningValue(value)} |
| 43 |
/> |
| 44 |
</TextInputGroup> |
| 45 |
</FlexItem> |
| 46 |
<FlexItem> |
| 47 |
<TextInputGroup validated={ValidatedOptions.error}> |
| 48 |
<TextInputGroupMain |
| 49 |
icon={<SearchIcon />} |
| 50 |
value={errorValue} |
| 51 |
onChange={(_event, value) => setErrorValue(value)} |
| 52 |
/> |
| 53 |
{showUtilities && ( |
| 54 |
<TextInputGroupUtilities> |
| 55 |
{showClearButton && ( |
| 56 |
<Button |
| 57 |
variant="plain" |
| 58 |
onClick={clearInput} |
| 59 |
aria-label="Clear button and input" |
| 60 |
icon={<RhMicronsCloseIcon />} |
| 61 |
/> |
| 62 |
)} |
| 63 |
</TextInputGroupUtilities> |
| 64 |
)} |
| 65 |
</TextInputGroup> |
| 66 |
</FlexItem> |
| 67 |
</Flex> |
| 68 |
); |
| 69 |
}; |