|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { css } from '@patternfly/react-styles'; |
| 4 | +import styles from '@patternfly/react-styles/css/components/Wizard/wizard'; |
| 5 | + |
| 6 | +import { |
| 7 | + DefaultWizardFooterProps, |
| 8 | + DefaultWizardNavProps, |
| 9 | + isCustomWizardFooter, |
| 10 | + isWizardParentStep, |
| 11 | + WizardNavStepFunction, |
| 12 | + CustomWizardNavFunction |
| 13 | +} from './types'; |
| 14 | +import { buildSteps, normalizeNavStep } from './utils'; |
| 15 | +import { useWizardContext, WizardComposableContextProvider } from './WizardComposabeContext'; |
| 16 | +import { WizardComposableStepProps } from './WizardComposableStep'; |
| 17 | +import { WizardComposableFooter } from './WizardComposableFooter'; |
| 18 | +import { WizardComposableToggle } from './WizardComposableToggle'; |
| 19 | + |
| 20 | +export interface WizardComposableProps extends React.HTMLProps<HTMLDivElement> { |
| 21 | + /** Step, footer, or header child components */ |
| 22 | + children: React.ReactElement<WizardComposableStepProps> | React.ReactElement<WizardComposableStepProps>[]; |
| 23 | + /** Wizard header */ |
| 24 | + header?: React.ReactNode; |
| 25 | + /** Wizard footer */ |
| 26 | + footer?: DefaultWizardFooterProps | React.ReactElement; |
| 27 | + /** Default wizard nav props or a custom WizardNav (with callback) */ |
| 28 | + nav?: DefaultWizardNavProps | CustomWizardNavFunction; |
| 29 | + /** The initial index the wizard is to start on (1 or higher). Defaults to 1. */ |
| 30 | + startIndex?: number; |
| 31 | + /** Additional classes spread to the Wizard */ |
| 32 | + className?: string; |
| 33 | + /** Custom width of the wizard */ |
| 34 | + width?: number | string; |
| 35 | + /** Custom height of the wizard */ |
| 36 | + height?: number | string; |
| 37 | + /** Callback function when a step in the nav is clicked */ |
| 38 | + onNavByIndex?: WizardNavStepFunction; |
| 39 | + /** Callback function after Next button is clicked */ |
| 40 | + onNext?: WizardNavStepFunction; |
| 41 | + /** Callback function after Back button is clicked */ |
| 42 | + onBack?: WizardNavStepFunction; |
| 43 | + /** Callback function to save at the end of the wizard, if not specified uses onClose */ |
| 44 | + onSave?(): void; |
| 45 | + /** Callback function to close the wizard */ |
| 46 | + onClose?(): void; |
| 47 | +} |
| 48 | + |
| 49 | +export const WizardComposable = (props: WizardComposableProps) => { |
| 50 | + const { startIndex = 1, children, footer, onNavByIndex, onNext, onBack, onSave, onClose, ...internalProps } = props; |
| 51 | + const [currentStepIndex, setCurrentStepIndex] = React.useState(startIndex); |
| 52 | + const steps = buildSteps(children); |
| 53 | + |
| 54 | + const goToStepByIndex = (index: number) => { |
| 55 | + const lastStepIndex = steps.length; |
| 56 | + |
| 57 | + if (index < 1) { |
| 58 | + index = 1; |
| 59 | + } else if (index > lastStepIndex) { |
| 60 | + index = lastStepIndex; |
| 61 | + } |
| 62 | + |
| 63 | + const currStep = steps[index - 1]; |
| 64 | + const prevStep = steps[currentStepIndex - 1]; |
| 65 | + setCurrentStepIndex(index); |
| 66 | + |
| 67 | + return onNavByIndex?.(normalizeNavStep(currStep), normalizeNavStep(prevStep)); |
| 68 | + }; |
| 69 | + |
| 70 | + const goToNextStep = () => { |
| 71 | + // Save when on the last step, otherwise close |
| 72 | + if (currentStepIndex >= steps.length) { |
| 73 | + if (onSave) { |
| 74 | + return onSave(); |
| 75 | + } |
| 76 | + |
| 77 | + return onClose?.(); |
| 78 | + } else { |
| 79 | + let currStep = steps[currentStepIndex]; |
| 80 | + let newStepIndex = currentStepIndex + 1; |
| 81 | + const prevStep = steps[currentStepIndex - 1]; |
| 82 | + |
| 83 | + // Skip parent step and focus on the first sub-step if they exist |
| 84 | + if (isWizardParentStep(currStep)) { |
| 85 | + newStepIndex += 1; |
| 86 | + currStep = steps[currentStepIndex + 1]; |
| 87 | + } |
| 88 | + |
| 89 | + setCurrentStepIndex(newStepIndex); |
| 90 | + return onNext?.(normalizeNavStep(currStep), normalizeNavStep(prevStep)); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + const goToPrevStep = () => { |
| 95 | + if (steps.length < currentStepIndex) { |
| 96 | + // Previous step was removed, just update the currentStep state |
| 97 | + setCurrentStepIndex(steps.length); |
| 98 | + } else { |
| 99 | + let currStep = steps[currentStepIndex - 2]; |
| 100 | + let newStepIndex = currentStepIndex - 1; |
| 101 | + const prevStep = steps[currentStepIndex - 1]; |
| 102 | + |
| 103 | + // // Skip parent step and focus on the step prior |
| 104 | + if (isWizardParentStep(currStep)) { |
| 105 | + newStepIndex -= 1; |
| 106 | + currStep = steps[currentStepIndex - 3]; |
| 107 | + } |
| 108 | + |
| 109 | + setCurrentStepIndex(newStepIndex); |
| 110 | + return onBack?.(normalizeNavStep(currStep), normalizeNavStep(prevStep)); |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + const goToStepById = (id: number | string) => { |
| 115 | + const stepIndex = steps.findIndex(step => step.id === id) + 1; |
| 116 | + stepIndex > 0 && setCurrentStepIndex(stepIndex); |
| 117 | + }; |
| 118 | + |
| 119 | + const goToStepByName = (name: string) => { |
| 120 | + const stepIndex = steps.findIndex(step => step.name === name) + 1; |
| 121 | + stepIndex > 0 && setCurrentStepIndex(stepIndex); |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <WizardComposableContextProvider |
| 126 | + steps={steps} |
| 127 | + currentStepIndex={currentStepIndex} |
| 128 | + footer={isCustomWizardFooter(footer) && footer} |
| 129 | + onNext={goToNextStep} |
| 130 | + onBack={goToPrevStep} |
| 131 | + onClose={onClose} |
| 132 | + goToStepById={goToStepById} |
| 133 | + goToStepByName={goToStepByName} |
| 134 | + goToStepByIndex={goToStepByIndex} |
| 135 | + > |
| 136 | + <WizardComposableInternal {...internalProps}>{children}</WizardComposableInternal> |
| 137 | + </WizardComposableContextProvider> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +// eslint-disable-next-line patternfly-react/no-anonymous-functions |
| 142 | +const WizardComposableInternal = ({ height, width, className, header, nav, ...restProps }: WizardComposableProps) => { |
| 143 | + const { activeStep, steps, footer, onNext, onBack, onClose, goToStepByIndex } = useWizardContext(); |
| 144 | + |
| 145 | + const wizardFooter = isCustomWizardFooter(footer) ? ( |
| 146 | + footer |
| 147 | + ) : ( |
| 148 | + <WizardComposableFooter |
| 149 | + activeStep={activeStep} |
| 150 | + onNext={onNext} |
| 151 | + onBack={onBack} |
| 152 | + onClose={onClose} |
| 153 | + disableBackButton={activeStep.id === steps[0].id} |
| 154 | + {...footer} |
| 155 | + /> |
| 156 | + ); |
| 157 | + |
| 158 | + return ( |
| 159 | + <div |
| 160 | + className={css(styles.wizard, className)} |
| 161 | + style={{ |
| 162 | + ...(height ? { height } : {}), |
| 163 | + ...(width ? { width } : {}) |
| 164 | + }} |
| 165 | + {...restProps} |
| 166 | + > |
| 167 | + {header} |
| 168 | + <WizardComposableToggle |
| 169 | + steps={steps} |
| 170 | + activeStep={activeStep} |
| 171 | + footer={wizardFooter} |
| 172 | + nav={nav} |
| 173 | + goToStepByIndex={goToStepByIndex} |
| 174 | + /> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +}; |
| 178 | + |
| 179 | +WizardComposable.displayName = 'WizardComposable'; |
0 commit comments