| 1 |
import { useEffect, useState } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Wizard/wizard'; |
| 4 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 5 |
import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon'; |
| 6 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 7 |
import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon'; |
| 8 |
|
| 9 |
import { OUIAProps, useOUIAProps } from '../../helpers'; |
| 10 |
import { WizardNavItemStatus } from './types'; |
| 11 |
|
| 12 |
export interface WizardNavItemProps |
| 13 |
extends Omit<React.HTMLProps<HTMLLIElement>, 'onClick' | 'id' | 'content' | 'type'>, OUIAProps { |
| 14 |
/** Additional classes spread to the wizard nav item */ |
| 15 |
className?: string; |
| 16 |
/** Can nest a WizardNav component for substeps */ |
| 17 |
children?: React.ReactNode; |
| 18 |
/** The content to display in the navigation item */ |
| 19 |
content?: React.ReactNode; |
| 20 |
/** Whether the navigation item is the currently active item */ |
| 21 |
isCurrent?: boolean; |
| 22 |
/** Whether the navigation item is disabled */ |
| 23 |
isDisabled?: boolean; |
| 24 |
/** Whether the navigation item has been visited */ |
| 25 |
isVisited?: boolean; |
| 26 |
/** The step index passed into the onNavItemClick callback */ |
| 27 |
stepIndex: number; |
| 28 |
/** Callback for when the navigation item is clicked */ |
| 29 |
onClick?: (event: React.MouseEvent<HTMLButtonElement> | React.MouseEvent<HTMLAnchorElement>, index: number) => any; |
| 30 |
/** Component used to render WizardNavItem */ |
| 31 |
component?: 'button' | 'a'; |
| 32 |
/** An optional url to use for when using an anchor component */ |
| 33 |
href?: string; |
| 34 |
/** Where to display the linked URL when using an anchor component */ |
| 35 |
target?: React.HTMLAttributeAnchorTarget; |
| 36 |
/** Flag indicating that this NavItem has child steps and is expandable */ |
| 37 |
isExpandable?: boolean; |
| 38 |
/** The id for the navigation item */ |
| 39 |
id?: string | number; |
| 40 |
/** Used to determine the icon displayed next to content. Default has no icon. */ |
| 41 |
status?: 'default' | 'error' | 'success' | 'warning'; |
| 42 |
} |
| 43 |
|
| 44 |
export const WizardNavItem = ({ |
| 45 |
children = null, |
| 46 |
content = '', |
| 47 |
isCurrent = false, |
| 48 |
isDisabled = false, |
| 49 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 50 |
isVisited = false, |
| 51 |
stepIndex, |
| 52 |
onClick, |
| 53 |
component: NavItemComponent = 'button', |
| 54 |
href, |
| 55 |
isExpandable = false, |
| 56 |
id, |
| 57 |
status = 'default', |
| 58 |
target, |
| 59 |
ouiaId, |
| 60 |
ouiaSafe = true, |
| 61 |
className, |
| 62 |
...props |
| 63 |
}: WizardNavItemProps) => { |
| 64 |
const [isExpanded, setIsExpanded] = useState(false); |
| 65 |
const ouiaProps = useOUIAProps(WizardNavItem.displayName, ouiaId, ouiaSafe); |
| 66 |
|
| 67 |
useEffect(() => { |
| 68 |
setIsExpanded(isCurrent); |
| 69 |
}, [isCurrent]); |
| 70 |
|
| 71 |
if (NavItemComponent === 'a' && !href && process.env.NODE_ENV !== 'production') { |
| 72 |
// eslint-disable-next-line no-console |
| 73 |
console.error('WizardNavItem: When using an anchor, please provide an href'); |
| 74 |
} |
| 75 |
|
| 76 |
return ( |
| 77 |
<li |
| 78 |
className={css( |
| 79 |
styles.wizardNavItem, |
| 80 |
isExpandable && styles.modifiers.expandable, |
| 81 |
isExpandable && isExpanded && styles.modifiers.expanded, |
| 82 |
className |
| 83 |
)} |
| 84 |
{...props} |
| 85 |
> |
| 86 |
<NavItemComponent |
| 87 |
{...(NavItemComponent === 'a' |
| 88 |
? { tabIndex: isDisabled ? -1 : undefined, href, target } |
| 89 |
: { disabled: isDisabled })} |
| 90 |
{...(id && { id: id.toString() })} |
| 91 |
onClick={(e) => { |
| 92 |
e.stopPropagation(); |
| 93 |
isExpandable ? setIsExpanded(!isExpanded || isCurrent) : onClick?.(e, stepIndex); |
| 94 |
}} |
| 95 |
className={css( |
| 96 |
styles.wizardNavLink, |
| 97 |
isCurrent && styles.modifiers.current, |
| 98 |
isDisabled && styles.modifiers.disabled, |
| 99 |
status === WizardNavItemStatus.Error && styles.modifiers.danger, |
| 100 |
status === WizardNavItemStatus.Success && styles.modifiers.success, |
| 101 |
status === WizardNavItemStatus.Warning && styles.modifiers.warning |
| 102 |
)} |
| 103 |
aria-disabled={isDisabled ? true : null} |
| 104 |
aria-current={isCurrent && !children ? 'step' : false} |
| 105 |
{...(isExpandable && { 'aria-expanded': isExpanded })} |
| 106 |
{...ouiaProps} |
| 107 |
> |
| 108 |
{status !== WizardNavItemStatus.Default && ( |
| 109 |
<> |
| 110 |
<span className="pf-v6-screen-reader">, {status}</span> |
| 111 |
<span className={css(styles.wizardNavLinkStatusIcon)}> |
| 112 |
{status === WizardNavItemStatus.Error && <RhUiErrorFillIcon />} |
| 113 |
{status === WizardNavItemStatus.Success && <RhUiCheckCircleFillIcon />} |
| 114 |
{status === WizardNavItemStatus.Warning && <RhUiWarningFillIcon />} |
| 115 |
</span> |
| 116 |
</> |
| 117 |
)} |
| 118 |
<span className={css(styles.wizardNavLinkMain)}> |
| 119 |
{isExpandable ? ( |
| 120 |
<> |
| 121 |
<span className="pf-v6-c-wizard__nav-link-text">{content}</span> |
| 122 |
<span className={css(styles.wizardNavLinkToggle)}> |
| 123 |
<span className={css(styles.wizardNavLinkToggleIcon)}> |
| 124 |
<RhMicronsCaretDownIcon aria-label={`${isCurrent ? 'Collapse' : 'Expand'} step icon`} /> |
| 125 |
</span> |
| 126 |
</span> |
| 127 |
</> |
| 128 |
) : ( |
| 129 |
<>{content}</> |
| 130 |
)} |
| 131 |
</span> |
| 132 |
</NavItemComponent> |
| 133 |
{children} |
| 134 |
</li> |
| 135 |
); |
| 136 |
}; |
| 137 |
|
| 138 |
WizardNavItem.displayName = 'WizardNavItem'; |