| 1 |
import { Fragment } from 'react'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
import styles from '@patternfly/react-styles/css/components/Wizard/wizard'; |
| 4 |
import RhMicronsCaretRightIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-right-icon'; |
| 5 |
import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon'; |
| 6 |
import { WizardStep } from './Wizard'; |
| 7 |
import { WizardBody } from './WizardBody'; |
| 8 |
|
| 9 |
export interface WizardToggleProps { |
| 10 |
/** Function that returns the WizardNav component */ |
| 11 |
nav: (isWizardNavOpen: boolean) => React.ReactElement<any>; |
| 12 |
/** The wizard steps */ |
| 13 |
steps: WizardStep[]; |
| 14 |
/** The currently active WizardStep */ |
| 15 |
activeStep: WizardStep; |
| 16 |
/** The WizardFooter */ |
| 17 |
children?: React.ReactNode; |
| 18 |
/** Set to true to remove body padding */ |
| 19 |
hasNoBodyPadding: boolean; |
| 20 |
/** If the nav is open */ |
| 21 |
isNavOpen: boolean; |
| 22 |
/** Callback function for when the nav is toggled */ |
| 23 |
onNavToggle: (isOpen: boolean) => void; |
| 24 |
/** The button's aria-label */ |
| 25 |
'aria-label'?: string; |
| 26 |
/** Adds an accessible name to the wizard body by passing the the id of one or more elements. |
| 27 |
* The aria-labelledby will only be applied when the body content overflows and renders a scrollbar. |
| 28 |
*/ |
| 29 |
mainAriaLabelledBy?: string; |
| 30 |
/** Adds an accessible name to the wizard body when the body content overflows and renders |
| 31 |
* a scrollbar. |
| 32 |
*/ |
| 33 |
mainAriaLabel?: string; |
| 34 |
/** If the wizard is in-page */ |
| 35 |
isInPage?: boolean; |
| 36 |
/** Flag indicating the wizard has a drawer for at least one of the wizard steps */ |
| 37 |
hasDrawer?: boolean; |
| 38 |
/** Flag indicating the wizard drawer is expanded */ |
| 39 |
isDrawerExpanded?: boolean; |
| 40 |
/** Callback function for when the drawer is toggled */ |
| 41 |
onExpandDrawer?: () => void; |
| 42 |
} |
| 43 |
|
| 44 |
export const WizardToggle: React.FunctionComponent<WizardToggleProps> = ({ |
| 45 |
isNavOpen, |
| 46 |
onNavToggle, |
| 47 |
nav, |
| 48 |
steps, |
| 49 |
activeStep, |
| 50 |
children, |
| 51 |
hasNoBodyPadding = false, |
| 52 |
'aria-label': ariaLabel = 'Wizard Toggle', |
| 53 |
mainAriaLabelledBy = null, |
| 54 |
mainAriaLabel = null, |
| 55 |
isInPage = true, |
| 56 |
hasDrawer, |
| 57 |
isDrawerExpanded, |
| 58 |
onExpandDrawer |
| 59 |
}: WizardToggleProps) => { |
| 60 |
let activeStepIndex; |
| 61 |
let activeStepName; |
| 62 |
let activeStepSubName; |
| 63 |
for (let i = 0; i < steps.length; i++) { |
| 64 |
if ((activeStep.id && steps[i].id === activeStep.id) || steps[i].name === activeStep.name) { |
| 65 |
activeStepIndex = i + 1; |
| 66 |
activeStepName = steps[i].name; |
| 67 |
break; |
| 68 |
} else if (steps[i].steps) { |
| 69 |
for (const step of steps[i].steps) { |
| 70 |
if ((activeStep.id && step.id === activeStep.id) || step.name === activeStep.name) { |
| 71 |
activeStepIndex = i + 1; |
| 72 |
activeStepName = steps[i].name; |
| 73 |
activeStepSubName = step.name; |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return ( |
| 81 |
<Fragment> |
| 82 |
<button |
| 83 |
onClick={() => onNavToggle(!isNavOpen)} |
| 84 |
className={css(styles.wizardToggle, isNavOpen && 'pf-m-expanded')} |
| 85 |
aria-label={ariaLabel} |
| 86 |
aria-expanded={isNavOpen} |
| 87 |
> |
| 88 |
<span className={css(styles.wizardToggleList)}> |
| 89 |
<span className={css(styles.wizardToggleListItem)}> |
| 90 |
<span className={css(styles.wizardToggleNum)}>{activeStepIndex}</span> {activeStepName} |
| 91 |
{activeStepSubName && ( |
| 92 |
<span className={css(styles.wizardToggleSeparator)}> |
| 93 |
<RhMicronsCaretRightIcon /> |
| 94 |
</span> |
| 95 |
)} |
| 96 |
</span> |
| 97 |
{activeStepSubName && <span className={css(styles.wizardToggleListItem)}>{activeStepSubName}</span>} |
| 98 |
</span> |
| 99 |
<span className={css(styles.wizardToggleIcon)}> |
| 100 |
<RhMicronsCaretDownIcon /> |
| 101 |
</span> |
| 102 |
</button> |
| 103 |
<div className={css(styles.wizardOuterWrap)}> |
| 104 |
<div className={css(styles.wizardInnerWrap)}> |
| 105 |
{nav(isNavOpen)} |
| 106 |
<WizardBody |
| 107 |
mainComponent={isInPage ? 'div' : 'main'} |
| 108 |
aria-label={mainAriaLabel} |
| 109 |
aria-labelledby={mainAriaLabelledBy} |
| 110 |
hasNoBodyPadding={hasNoBodyPadding} |
| 111 |
activeStep={activeStep} |
| 112 |
isDrawerExpanded={isDrawerExpanded} |
| 113 |
onExpandDrawer={onExpandDrawer} |
| 114 |
hasDrawer={hasDrawer} |
| 115 |
> |
| 116 |
{hasDrawer && !isDrawerExpanded && activeStep.drawerToggleButton} |
| 117 |
{activeStep.component} |
| 118 |
</WizardBody> |
| 119 |
</div> |
| 120 |
{children} |
| 121 |
</div> |
| 122 |
</Fragment> |
| 123 |
); |
| 124 |
}; |
| 125 |
WizardToggle.displayName = 'WizardToggle'; |