|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { noop, Wizard, Icon, Button } from '../../../index'; |
| 4 | +import WizardPatternBody from './WizardPatternBody'; |
| 5 | +import { wizardStepShape } from './WizardPatternConstants'; |
| 6 | + |
| 7 | +/** |
| 8 | + * WizardPattern - the Wizard Pattern Body component. |
| 9 | + */ |
| 10 | +const WizardPattern = ({ |
| 11 | + steps, |
| 12 | + activeStepIndex, |
| 13 | + onStepChanged, |
| 14 | + onNext, |
| 15 | + onBack, |
| 16 | + nextStepDisabled, |
| 17 | + title, |
| 18 | + loadingTitle, |
| 19 | + loadingMessage, |
| 20 | + show, |
| 21 | + onHide, |
| 22 | + onExited, |
| 23 | + stepButtonsDisabled, |
| 24 | + cancelText, |
| 25 | + backText, |
| 26 | + nextText, |
| 27 | + closeText, |
| 28 | + loading, |
| 29 | + nextButtonRef, |
| 30 | + bodyHeader, |
| 31 | + children |
| 32 | +}) => { |
| 33 | + const onFirstStep = activeStepIndex === 0; |
| 34 | + const onFinalStep = activeStepIndex === steps.length - 1; |
| 35 | + |
| 36 | + const onHideClick = () => { |
| 37 | + onHide(onFinalStep); |
| 38 | + }; |
| 39 | + |
| 40 | + const onBackClick = () => { |
| 41 | + goToStep(Math.max(activeStepIndex - 1, 0)); |
| 42 | + }; |
| 43 | + |
| 44 | + const onNextClick = () => { |
| 45 | + goToStep(Math.min(activeStepIndex + 1, steps.length - 1)); |
| 46 | + }; |
| 47 | + |
| 48 | + const getStep = (index = activeStepIndex) => steps[index]; |
| 49 | + |
| 50 | + const getPrevStep = (relativeToIndex = activeStepIndex) => |
| 51 | + relativeToIndex > 0 && steps[relativeToIndex - 1]; |
| 52 | + |
| 53 | + const getNextStep = (relativeToIndex = activeStepIndex) => |
| 54 | + relativeToIndex < steps.length - 1 && steps[relativeToIndex + 1]; |
| 55 | + |
| 56 | + const activeStep = getStep(); |
| 57 | + |
| 58 | + const goToStep = newStepIndex => { |
| 59 | + if (shouldPreventGoToStep(newStepIndex)) return; |
| 60 | + if (newStepIndex === activeStepIndex + 1) { |
| 61 | + const stepOnNextResult = activeStep.onNext && activeStep.onNext(); |
| 62 | + const propOnNextResult = onNext(newStepIndex); |
| 63 | + const stepFailed = |
| 64 | + stepOnNextResult === false || propOnNextResult === false; |
| 65 | + if (stepFailed) return; |
| 66 | + } |
| 67 | + if (newStepIndex === activeStepIndex - 1) { |
| 68 | + const stepFailed = onBack(newStepIndex) === false; |
| 69 | + if (stepFailed) return; |
| 70 | + } |
| 71 | + if (onStepChanged) onStepChanged(newStepIndex); |
| 72 | + }; |
| 73 | + |
| 74 | + const shouldPreventGoToStep = newStepIndex => { |
| 75 | + const targetStep = getStep(newStepIndex); |
| 76 | + const stepBeforeTarget = getPrevStep(newStepIndex); |
| 77 | + |
| 78 | + const preventExitActive = activeStep.preventExit; |
| 79 | + const preventEnterTarget = |
| 80 | + targetStep.preventEnter || |
| 81 | + (stepBeforeTarget && stepBeforeTarget.isInvalid); |
| 82 | + const nextStepClicked = newStepIndex === activeStepIndex + 1; |
| 83 | + |
| 84 | + return ( |
| 85 | + preventExitActive || |
| 86 | + preventEnterTarget || |
| 87 | + (nextStepClicked && nextStepDisabled) |
| 88 | + ); |
| 89 | + }; |
| 90 | + |
| 91 | + const activeStepStr = (activeStepIndex + 1).toString(); |
| 92 | + |
| 93 | + const prevStepUnreachable = |
| 94 | + onFirstStep || activeStep.preventExit || getPrevStep().preventEnter; |
| 95 | + // nextStepUnreachable is still true onFinalStep, because the Next button turns into a Close button |
| 96 | + const nextStepUnreachable = |
| 97 | + nextStepDisabled || |
| 98 | + activeStep.isInvalid || |
| 99 | + activeStep.preventExit || |
| 100 | + getNextStep().preventEnter; |
| 101 | + |
| 102 | + return ( |
| 103 | + <Wizard show={show} onHide={onHideClick} onExited={onExited}> |
| 104 | + <Wizard.Header onClose={onHideClick} title={title} /> |
| 105 | + <Wizard.Body> |
| 106 | + {bodyHeader} |
| 107 | + <WizardPatternBody |
| 108 | + loadingTitle={loadingTitle} |
| 109 | + loadingMessage={loadingMessage} |
| 110 | + loading={loading} |
| 111 | + steps={steps} |
| 112 | + activeStepIndex={activeStepIndex} |
| 113 | + activeStepStr={activeStepStr} |
| 114 | + goToStep={goToStep} |
| 115 | + nextStepDisabled={nextStepDisabled} |
| 116 | + stepButtonsDisabled={stepButtonsDisabled} |
| 117 | + /> |
| 118 | + </Wizard.Body> |
| 119 | + <Wizard.Footer> |
| 120 | + <Button bsStyle="default" className="btn-cancel" onClick={onHideClick}> |
| 121 | + {cancelText} |
| 122 | + </Button> |
| 123 | + <Button |
| 124 | + bsStyle="default" |
| 125 | + onClick={onBackClick} |
| 126 | + disabled={prevStepUnreachable} |
| 127 | + > |
| 128 | + <Icon type="fa" name="angle-left" /> |
| 129 | + {backText} |
| 130 | + </Button> |
| 131 | + <Button |
| 132 | + bsStyle="primary" |
| 133 | + onClick={onFinalStep ? onHideClick : onNextClick} |
| 134 | + disabled={nextStepUnreachable} |
| 135 | + ref={nextButtonRef} |
| 136 | + > |
| 137 | + {onFinalStep ? ( |
| 138 | + closeText |
| 139 | + ) : ( |
| 140 | + <React.Fragment> |
| 141 | + {nextText} |
| 142 | + <Icon type="fa" name="angle-right" /> |
| 143 | + </React.Fragment> |
| 144 | + )} |
| 145 | + </Button> |
| 146 | + </Wizard.Footer> |
| 147 | + {children} |
| 148 | + </Wizard> |
| 149 | + ); |
| 150 | +}; |
| 151 | + |
| 152 | +WizardPattern.propTypes = { |
| 153 | + activeStepIndex: PropTypes.number.isRequired, |
| 154 | + show: PropTypes.bool, |
| 155 | + title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 156 | + onHide: PropTypes.func, |
| 157 | + onExited: PropTypes.func, |
| 158 | + onBack: PropTypes.func, |
| 159 | + onNext: PropTypes.func, |
| 160 | + onStepChanged: PropTypes.func, |
| 161 | + loadingTitle: PropTypes.string, |
| 162 | + loadingMessage: PropTypes.string, |
| 163 | + loading: PropTypes.bool, |
| 164 | + cancelText: PropTypes.string, |
| 165 | + backText: PropTypes.string, |
| 166 | + nextText: PropTypes.string, |
| 167 | + closeText: PropTypes.string, |
| 168 | + steps: PropTypes.arrayOf(PropTypes.shape(wizardStepShape)), |
| 169 | + nextStepDisabled: PropTypes.bool, |
| 170 | + stepButtonsDisabled: PropTypes.bool, |
| 171 | + nextButtonRef: PropTypes.func, |
| 172 | + bodyHeader: PropTypes.node, |
| 173 | + children: PropTypes.node |
| 174 | +}; |
| 175 | + |
| 176 | +WizardPattern.defaultProps = { |
| 177 | + show: false, |
| 178 | + title: '', |
| 179 | + onHide: noop, |
| 180 | + onExited: noop, |
| 181 | + onBack: noop, |
| 182 | + onNext: noop, |
| 183 | + onStepChanged: noop, |
| 184 | + loadingTitle: 'Loading Wizard...', |
| 185 | + loadingMessage: 'Loading...', |
| 186 | + loading: false, |
| 187 | + cancelText: 'Cancel', |
| 188 | + backText: 'Back', |
| 189 | + nextText: 'Next', |
| 190 | + closeText: 'Close', |
| 191 | + steps: [], |
| 192 | + nextStepDisabled: false, |
| 193 | + stepButtonsDisabled: false, |
| 194 | + nextButtonRef: noop, |
| 195 | + bodyHeader: null, |
| 196 | + children: null |
| 197 | +}; |
| 198 | + |
| 199 | +WizardPattern.displayName = 'WizardPattern'; |
| 200 | + |
| 201 | +export default WizardPattern; |
0 commit comments