Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 500x 500x 500x 500x 500x 500x 500x 9x 7x 7x 1x 1x 1x 1x 7x 3x 7x 7x 7x 1x 1x 1x 7x 7x 7x 7x 3x 3x 3x 7x 7x 7x 7x 500x | /* * Copyright 2021 Harness Inc. All rights reserved. * Use of this source code is governed by the PolyForm Shield 1.0.0 license * that can be found in the licenses directory at the root of this repository, also available at * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt. */ import React from 'react' import cx from 'classnames' import { Text, Icon, IconName } from '@wings-software/uicore' import { Status } from './WizardHelper' import WizardHeader from './WizardHeader' import css from './StepWizard.module.scss' export interface PanelInterface { id: string tabTitle?: string tabTitleComponent?: JSX.Element status?: string } interface StepChangeData<SharedObject> { prevStep: number nextStep: number prevStepData: SharedObject } export interface StepWizardProps<SharedObject> { icon?: IconName // iconProps?: Omit<IconProps, 'name'> title?: string | JSX.Element children: Array<React.ReactElement<StepProps<SharedObject>> | null> isNavMode?: boolean className?: string stepClassName?: string navClassName?: string onStepChange?: (data: StepChangeData<SharedObject | undefined>) => void onCompleteWizard?: (data: SharedObject | undefined) => void initialStep?: number panels: PanelInterface[] props?: string } export interface StepProps<SharedObject> { name?: string // These props will be passed by wizard prevStepData?: SharedObject currentStep?: () => number totalSteps?: () => number nextStep?: (prevStepData?: SharedObject) => void previousStep?: (prevStepData?: SharedObject) => void gotoStep?: (stepNumber: number, prevStepData?: SharedObject) => void firstStep?: (prevStepData?: SharedObject) => void lastStep?: (prevStepData?: SharedObject) => void } interface StepState<SharedObject> { activeStep: number prevStep: number prevStepData?: SharedObject stepNames?: string[] totalSteps: number panels: PanelInterface[] panelActiveId: number } export const WizardWithProgress = <SharedObject extends unknown>(props: StepWizardProps<SharedObject>) => { const { className = '', isNavMode = true, initialStep = 1, children, stepClassName = '', navClassName = '', icon = '', title = '', panels } = props const [state, setState] = React.useState<StepState<SharedObject>>({ activeStep: initialStep < 1 || initialStep > children.length ? 1 : initialStep, totalSteps: 0, prevStep: -1, panels, panelActiveId: 0 }) const gotoStep = React.useCallback( (stepNumber: number, prevStepData?: SharedObject) => { Iif (state.activeStep === stepNumber) { return } Iif (props.onCompleteWizard && state.totalSteps > 0 && stepNumber > 1 && stepNumber === state.totalSteps + 1) { props.onCompleteWizard(prevStepData) return } else Iif (stepNumber > state.totalSteps || stepNumber < 1) { // Not a valid step stepNumber return } setState(prevState => ({ ...prevState, prevStep: prevState.activeStep, activeStep: stepNumber, prevStepData })) }, [state.activeStep, state.totalSteps] ) React.useEffect(() => { Iif (props.onStepChange && state.prevStep !== -1) { props.onStepChange({ prevStep: state.prevStep, nextStep: state.activeStep, prevStepData: state.prevStepData }) } }, [state.prevStep, state.activeStep, state.prevStepData]) const currentStep = React.useCallback(() => { return state.activeStep }, [state.activeStep]) const totalSteps = React.useCallback(() => { return state.totalSteps }, [state.activeStep]) const nextStep = React.useCallback( (prevStepData?: SharedObject) => { panels[state.activeStep - 1].status = Status.COMPLETED panels[state.activeStep].status = Status.INPROGRESS gotoStep(state.activeStep + 1, prevStepData) // const panelActiveId = state.activeStep ? state.panelActiveId + 1 : state.panelActiveId setState(prevState => ({ ...prevState, panels })) }, [gotoStep] ) const previousStep = React.useCallback( (prevStepData?: SharedObject) => { gotoStep(state.activeStep - 1, prevStepData) }, [gotoStep] ) const firstStep = React.useCallback( (prevStepData?: SharedObject) => { gotoStep(1, prevStepData) }, [gotoStep] ) const lastStep = React.useCallback( (prevStepData?: SharedObject) => { gotoStep(state.totalSteps, prevStepData) }, [gotoStep] ) React.useLayoutEffect(() => { const steps = React.Children.toArray(props.children) const stepNames: string[] = [] setState(prevState => ({ ...prevState, stepNames, totalSteps: steps.length })) }, [children]) const renderStep = () => ( <React.Fragment> {state.stepNames && state.stepNames.map((stepName, index) => { const activeStep = index + 1 === state.activeStep const completedSteps = state.activeStep > index + 1 return ( <div key={index} onClick={() => completedSteps && gotoStep(index + 1, state.prevStepData)} className={cx( css.navStep, navClassName, { [css.activeStep]: activeStep }, { [css.completedStep]: completedSteps } )} > {completedSteps ? ( <Icon name="small-tick" size={22} color="green500" style={{ marginRight: 'var(--spacing-xsmall)' }} /> ) : ( <span className={css.number}>{index + 1}</span> )} <Text className={css.stepName} lineClamp={2} width={240}> {stepName} </Text> </div> ) })} </React.Fragment> ) const activeChild = (React.Children.toArray(props.children)[state.activeStep - 1] as React.ReactElement) || <div /> const childProps: StepProps<SharedObject> = { currentStep, totalSteps, prevStepData: state.prevStepData, nextStep: nextStep, previousStep, gotoStep, firstStep, lastStep } return ( <div className={cx(css.main, className, { [css.navBar]: isNavMode })}> {isNavMode && ( <div className={css.navBarList}> {icon ? ( <span className={css.header}> <Icon name={icon} /> </span> ) : null} {title ? ( typeof title === 'string' ? ( <span className={cx(css.title, css.header)}>{title}</span> ) : ( title ) ) : null} {renderStep()} </div> )} {!isNavMode && ( <WizardHeader panels={panels} onClick={() => { // console.log(state.activeStep - 1, state.activeStep) }} /> )} {state.activeStep && ( <div className={cx(css.stepDetails, stepClassName)}> {React.cloneElement(activeChild, childProps)}</div> )} </div> ) } export default WizardWithProgress |