|
1 | 1 | import React from 'react'; |
2 | | -import { FormGroup as Pf4FormGroup, TextContent, Text } from '@patternfly/react-core'; |
| 2 | +import { FormGroup as Pf4FormGroup, TextContent, Text, FormHelperText, HelperText, HelperTextItem } from '@patternfly/react-core'; |
3 | 3 | import PropTypes from 'prop-types'; |
4 | 4 |
|
5 | 5 | import showError from '../show-error/show-error'; |
6 | 6 |
|
7 | | -const FormGroup = ({ label, isRequired, helperText, meta, validateOnMount, description, hideLabel, children, id, FormGroupProps }) => ( |
8 | | - <Pf4FormGroup |
9 | | - isRequired={isRequired} |
10 | | - label={!hideLabel && label} |
11 | | - fieldId={id} |
12 | | - helperText={((meta.touched || validateOnMount) && meta.warning) || helperText} |
13 | | - helperTextInvalid={meta.error || meta.submitError} |
14 | | - {...showError(meta, validateOnMount)} |
15 | | - {...FormGroupProps} |
16 | | - > |
17 | | - {description && ( |
18 | | - <TextContent> |
19 | | - <Text component="small">{description}</Text> |
20 | | - </TextContent> |
21 | | - )} |
22 | | - {children} |
23 | | - </Pf4FormGroup> |
24 | | -); |
| 7 | +const FormGroup = ({ label, isRequired, helperText, meta, validateOnMount, description, hideLabel, children, id, FormGroupProps }) => { |
| 8 | + const { validated } = showError(meta, validateOnMount); |
| 9 | + const validationInternal = (meta.touched || validateOnMount) && (meta.error || meta.submitError || meta.warning); |
| 10 | + return ( |
| 11 | + <Pf4FormGroup isRequired={isRequired} label={!hideLabel && label} fieldId={id} aria-label={meta.error || meta.submitError} {...FormGroupProps}> |
| 12 | + {description && ( |
| 13 | + <TextContent> |
| 14 | + <Text component="small">{description}</Text> |
| 15 | + </TextContent> |
| 16 | + )} |
| 17 | + {children} |
| 18 | + {(helperText || ['error', 'warning'].includes(validated)) && ( |
| 19 | + <FormHelperText> |
| 20 | + <HelperText> |
| 21 | + <HelperTextItem variant={validated}>{validationInternal || helperText}</HelperTextItem> |
| 22 | + </HelperText> |
| 23 | + </FormHelperText> |
| 24 | + )} |
| 25 | + </Pf4FormGroup> |
| 26 | + ); |
| 27 | +}; |
25 | 28 |
|
26 | 29 | FormGroup.propTypes = { |
27 | 30 | label: PropTypes.node, |
|
0 commit comments