| 1 |
import styles from '@patternfly/react-styles/css/components/Form/form'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import { FormFieldGroupToggle } from './FormFieldGroupToggle'; |
| 4 |
import { useSSRSafeId } from '../../helpers'; |
| 5 |
import { useHasAnimations } from '../../helpers'; |
| 6 |
|
| 7 |
export interface InternalFormFieldGroupProps extends Omit<React.HTMLProps<HTMLDivElement>, 'label' | 'onToggle'> { |
| 8 |
/** Anything that can be rendered as form field group content. */ |
| 9 |
children?: React.ReactNode; |
| 10 |
/** Additional classes added to the form field group. */ |
| 11 |
className?: string; |
| 12 |
/** Form field group header */ |
| 13 |
header?: any; |
| 14 |
/** Flag indicating if the field group is expandable */ |
| 15 |
isExpandable?: boolean; |
| 16 |
/** Flag indicate if the form field group is expanded. Modifies the card to be expandable. */ |
| 17 |
isExpanded?: boolean; |
| 18 |
/** Function callback called when user clicks toggle button */ |
| 19 |
onToggle?: () => void; |
| 20 |
/** Aria-label to use on the form field group toggle button */ |
| 21 |
toggleAriaLabel?: string; |
| 22 |
/** Flag indicating whether an expandable form field group has animations. This will always render |
| 23 |
* nested field group content rather than dynamically rendering them. This prop will be removed in |
| 24 |
* the next breaking change release in favor of defaulting to always-rendered items. |
| 25 |
*/ |
| 26 |
hasAnimations?: boolean; |
| 27 |
} |
| 28 |
|
| 29 |
export const InternalFormFieldGroup: React.FunctionComponent<InternalFormFieldGroupProps> = ({ |
| 30 |
children, |
| 31 |
className, |
| 32 |
header, |
| 33 |
isExpandable, |
| 34 |
isExpanded, |
| 35 |
onToggle, |
| 36 |
toggleAriaLabel, |
| 37 |
hasAnimations: hasAnimationsProp, |
| 38 |
...props |
| 39 |
}: InternalFormFieldGroupProps) => { |
| 40 |
const toggleId = useSSRSafeId('form-field-group-toggle'); |
| 41 |
const hasAnimations = useHasAnimations(hasAnimationsProp); |
| 42 |
const headerTitleText = header ? header.props.titleText : null; |
| 43 |
if (isExpandable && !toggleAriaLabel && !headerTitleText) { |
| 44 |
// eslint-disable-next-line no-console |
| 45 |
console.error( |
| 46 |
'FormFieldGroupExpandable:', |
| 47 |
'toggleAriaLabel or the titleText prop of FormFieldGroupHeader is required to make the toggle button accessible' |
| 48 |
); |
| 49 |
} |
| 50 |
return ( |
| 51 |
<div |
| 52 |
className={css( |
| 53 |
styles.formFieldGroup, |
| 54 |
isExpanded && isExpandable && styles.modifiers.expanded, |
| 55 |
hasAnimations && isExpandable && styles.modifiers.expandable, |
| 56 |
className |
| 57 |
)} |
| 58 |
role="group" |
| 59 |
{...(headerTitleText && { 'aria-labelledby': `${header.props.titleText.id}` })} |
| 60 |
{...props} |
| 61 |
> |
| 62 |
{isExpandable && ( |
| 63 |
<FormFieldGroupToggle |
| 64 |
onToggle={onToggle} |
| 65 |
isExpanded={isExpanded} |
| 66 |
aria-label={toggleAriaLabel} |
| 67 |
toggleId={toggleId} |
| 68 |
{...(headerTitleText && { 'aria-labelledby': `${header.props.titleText.id} ${toggleId}` })} |
| 69 |
/> |
| 70 |
)} |
| 71 |
{header && header} |
| 72 |
{(!isExpandable || (isExpandable && isExpanded) || (hasAnimations && isExpandable)) && ( |
| 73 |
<div |
| 74 |
className={css(styles.formFieldGroupBody)} |
| 75 |
{...(hasAnimations && isExpandable && !isExpanded && { inert: '' })} |
| 76 |
> |
| 77 |
{children} |
| 78 |
</div> |
| 79 |
)} |
| 80 |
</div> |
| 81 |
); |
| 82 |
}; |
| 83 |
InternalFormFieldGroup.displayName = 'InternalFormFieldGroup'; |