| 1 |
import { useEffect, useState } from 'react'; |
| 2 |
import { Form, FormGroup, FormHelperText, TextInput, HelperText, HelperTextItem } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const HelperTextDynamic: React.FunctionComponent = () => { |
| 5 |
const [value, setValue] = useState(''); |
| 6 |
const [inputValidation, setInputValidation] = useState('default'); |
| 7 |
|
| 8 |
const handleInputChange = (_event, inputValue: string) => { |
| 9 |
setValue(inputValue); |
| 10 |
}; |
| 11 |
|
| 12 |
useEffect(() => { |
| 13 |
if (value === '') { |
| 14 |
setInputValidation('default'); |
| 15 |
} else if (value === 'johndoe') { |
| 16 |
setInputValidation('error'); |
| 17 |
} else { |
| 18 |
setInputValidation('success'); |
| 19 |
} |
| 20 |
}, [value]); |
| 21 |
|
| 22 |
return ( |
| 23 |
<Form> |
| 24 |
<FormGroup label="Username" isRequired fieldId="login-input-helper-text2"> |
| 25 |
<TextInput |
| 26 |
isRequired |
| 27 |
type="text" |
| 28 |
id="login-input-helper-text2" |
| 29 |
name="login-input-helper-text2" |
| 30 |
onChange={handleInputChange} |
| 31 |
aria-describedby="helper-text2" |
| 32 |
aria-invalid={inputValidation === 'error'} |
| 33 |
value={value} |
| 34 |
/> |
| 35 |
<FormHelperText> |
| 36 |
<HelperText id="helper-text2" aria-live="polite"> |
| 37 |
{inputValidation !== 'success' && ( |
| 38 |
<HelperTextItem variant={inputValidation as any}> |
| 39 |
{inputValidation === 'default' ? 'Please enter a username' : 'Username already exists'} |
| 40 |
</HelperTextItem> |
| 41 |
)} |
| 42 |
</HelperText> |
| 43 |
</FormHelperText> |
| 44 |
</FormGroup> |
| 45 |
</Form> |
| 46 |
); |
| 47 |
}; |