| 1 |
import styles from '@patternfly/react-styles/css/components/HelperText/helper-text'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import MinusIcon from '@patternfly/react-icons/dist/esm/icons/minus-icon'; |
| 4 |
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon'; |
| 5 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 6 |
import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon'; |
| 7 |
|
| 8 |
export enum HelperTextItemVariant { |
| 9 |
default = 'default', |
| 10 |
warning = 'warning', |
| 11 |
error = 'error', |
| 12 |
success = 'success' |
| 13 |
} |
| 14 |
|
| 15 |
export interface HelperTextItemProps extends React.HTMLProps<HTMLDivElement | HTMLLIElement> { |
| 16 |
/** Content rendered inside the helper text item. */ |
| 17 |
children?: React.ReactNode; |
| 18 |
/** Additional classes applied to the helper text item. */ |
| 19 |
className?: string; |
| 20 |
/** Sets the component type of the helper text item. */ |
| 21 |
component?: 'div' | 'li'; |
| 22 |
/** Variant styling of the helper text item. Will also render a default icon, which can be overridden |
| 23 |
* with the icon prop. |
| 24 |
*/ |
| 25 |
variant?: 'default' | 'indeterminate' | 'warning' | 'success' | 'error'; |
| 26 |
/** Custom icon prefixing the helper text. This property will override the default icon when the variant property is passed in. */ |
| 27 |
icon?: React.ReactNode; |
| 28 |
/** ID for the helper text item. The value of this prop can be passed into a form component's |
| 29 |
* aria-describedby prop when you intend for only specific helper text items to be announced to |
| 30 |
* assistive technologies. |
| 31 |
*/ |
| 32 |
id?: string; |
| 33 |
/** Text that is only accessible to screen readers in order to announce the variant of a helper text item. |
| 34 |
* This prop can only be used when the variant prop has a value other than "default". |
| 35 |
*/ |
| 36 |
screenReaderText?: string; |
| 37 |
} |
| 38 |
|
| 39 |
const defaultVariantIcons = { |
| 40 |
indeterminate: <MinusIcon />, |
| 41 |
warning: <RhUiWarningFillIcon />, |
| 42 |
success: <RhUiCheckCircleFillIcon />, |
| 43 |
error: <RhUiErrorFillIcon /> |
| 44 |
}; |
| 45 |
|
| 46 |
export const HelperTextItem: React.FunctionComponent<HelperTextItemProps> = ({ |
| 47 |
children, |
| 48 |
className, |
| 49 |
component = 'div', |
| 50 |
variant = 'default', |
| 51 |
icon, |
| 52 |
id, |
| 53 |
screenReaderText = `${variant} status`, |
| 54 |
...props |
| 55 |
}: HelperTextItemProps) => { |
| 56 |
const Component = component as any; |
| 57 |
const isNotDefaultVariant = variant !== 'default'; |
| 58 |
const defaultIcon = isNotDefaultVariant && defaultVariantIcons[variant]; |
| 59 |
const shouldRenderSRText = isNotDefaultVariant && screenReaderText && screenReaderText !== ''; |
| 60 |
return ( |
| 61 |
<Component |
| 62 |
className={css(styles.helperTextItem, isNotDefaultVariant && styles.modifiers[variant], className)} |
| 63 |
id={id} |
| 64 |
{...props} |
| 65 |
> |
| 66 |
{(defaultIcon || icon) && <span className={css(styles.helperTextItemIcon)}>{icon || defaultIcon}</span>} |
| 67 |
|
| 68 |
<span className={css(styles.helperTextItemText)}> |
| 69 |
{children} |
| 70 |
{shouldRenderSRText && <span className="pf-v6-screen-reader">: {screenReaderText};</span>} |
| 71 |
</span> |
| 72 |
</Component> |
| 73 |
); |
| 74 |
}; |
| 75 |
HelperTextItem.displayName = 'HelperTextItem'; |