| 1 |
import { Fragment, isValidElement } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/Form/form'; |
| 3 |
import { ASTERISK } from '../../helpers/htmlConstants'; |
| 4 |
import { css } from '@patternfly/react-styles'; |
| 5 |
import { useSSRSafeId } from '../../helpers'; |
| 6 |
|
| 7 |
export interface FormGroupProps extends Omit<React.HTMLProps<HTMLDivElement>, 'label'> { |
| 8 |
/** Anything that can be rendered as FormGroup content. */ |
| 9 |
children?: React.ReactNode; |
| 10 |
/** Additional classes added to the FormGroup. */ |
| 11 |
className?: string; |
| 12 |
/** Label text before the field. */ |
| 13 |
label?: React.ReactNode; |
| 14 |
/** Additional label information displayed after the label. */ |
| 15 |
labelInfo?: React.ReactNode; |
| 16 |
/** A help button for the label. We recommend using FormGroupLabelHelp element as a help icon button. The help button should be wrapped or linked to our popover component. */ |
| 17 |
labelHelp?: React.ReactElement<any>; |
| 18 |
/** Sets the FormGroup required. */ |
| 19 |
isRequired?: boolean; |
| 20 |
/** Sets the FormGroup isInline. */ |
| 21 |
isInline?: boolean; |
| 22 |
/** Sets the FormGroupControl to be stacked */ |
| 23 |
isStack?: boolean; |
| 24 |
/** Removes top spacer from label. */ |
| 25 |
hasNoPaddingTop?: boolean; |
| 26 |
/** ID of an individual field or a group of multiple fields. Required when a role of "group" or "radiogroup" is passed in. |
| 27 |
* If only one field is included, its ID attribute and this prop must be the same. |
| 28 |
*/ |
| 29 |
fieldId?: string; |
| 30 |
/** Sets the role of the form group. Pass in "radiogroup" when the form group contains multiple |
| 31 |
* radio inputs, or pass in "group" when the form group contains multiple of any other input type. |
| 32 |
*/ |
| 33 |
role?: string; |
| 34 |
} |
| 35 |
|
| 36 |
export const FormGroup: React.FunctionComponent<FormGroupProps> = ({ |
| 37 |
children = null, |
| 38 |
className = '', |
| 39 |
label, |
| 40 |
labelInfo, |
| 41 |
labelHelp, |
| 42 |
isRequired = false, |
| 43 |
isInline = false, |
| 44 |
hasNoPaddingTop = false, |
| 45 |
isStack = false, |
| 46 |
fieldId, |
| 47 |
role, |
| 48 |
...props |
| 49 |
}: FormGroupProps) => { |
| 50 |
const randomId = useSSRSafeId(); |
| 51 |
const isGroupOrRadioGroup = role === 'group' || role === 'radiogroup'; |
| 52 |
const LabelComponent = isGroupOrRadioGroup ? 'span' : 'label'; |
| 53 |
|
| 54 |
const labelContent = ( |
| 55 |
<> |
| 56 |
<LabelComponent className={css(styles.formLabel)} {...(!isGroupOrRadioGroup && { htmlFor: fieldId })}> |
| 57 |
<span className={css(styles.formLabelText)}>{label}</span> |
| 58 |
{isRequired && ( |
| 59 |
<span className={css(styles.formLabelRequired)} aria-hidden="true"> |
| 60 |
{ASTERISK} |
| 61 |
</span> |
| 62 |
)} |
| 63 |
</LabelComponent> |
| 64 |
<> </> |
| 65 |
{isValidElement(labelHelp) && <span className={styles.formGroupLabelHelp}>{labelHelp}</span>} |
| 66 |
</> |
| 67 |
); |
| 68 |
|
| 69 |
return ( |
| 70 |
<div |
| 71 |
className={css(styles.formGroup, className)} |
| 72 |
{...(role && { role })} |
| 73 |
{...(isGroupOrRadioGroup && { 'aria-labelledby': `${fieldId || randomId}-legend` })} |
| 74 |
{...props} |
| 75 |
> |
| 76 |
{label && ( |
| 77 |
<div |
| 78 |
className={css( |
| 79 |
styles.formGroupLabel, |
| 80 |
labelInfo && styles.modifiers.info, |
| 81 |
hasNoPaddingTop && styles.modifiers.noPaddingTop |
| 82 |
)} |
| 83 |
{...(isGroupOrRadioGroup && { id: `${fieldId || randomId}-legend` })} |
| 84 |
> |
| 85 |
{labelInfo && ( |
| 86 |
<Fragment> |
| 87 |
<div className={css(styles.formGroupLabelMain)}>{labelContent}</div> |
| 88 |
<div className={css(styles.formGroupLabelInfo)}>{labelInfo}</div> |
| 89 |
</Fragment> |
| 90 |
)} |
| 91 |
{!labelInfo && labelContent} |
| 92 |
</div> |
| 93 |
)} |
| 94 |
<div |
| 95 |
className={css(styles.formGroupControl, isInline && styles.modifiers.inline, isStack && styles.modifiers.stack)} |
| 96 |
> |
| 97 |
{children} |
| 98 |
</div> |
| 99 |
</div> |
| 100 |
); |
| 101 |
}; |
| 102 |
FormGroup.displayName = 'FormGroup'; |