|
1 | 1 | import React, { useContext } from 'react';
|
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import clsx from 'clsx'; |
| 4 | + |
2 | 5 | import WizardCommon from '@data-driven-forms/common/src/wizard/wizard';
|
3 | 6 | import { FormSpy, WizardContext } from '@data-driven-forms/react-form-renderer';
|
| 7 | +import { ProgressIndicator, ProgressStep, Button, Column, Grid, Row } from 'carbon-components-react'; |
| 8 | + |
| 9 | +import './wizard.scss'; |
| 10 | + |
| 11 | +const WizardNav = ({ stepsInfo, jumpToStep, ...props }) => ( |
| 12 | + <ProgressIndicator |
| 13 | + onChange={(index) => (props.currentIndex > index ? jumpToStep(index, true) : undefined)} |
| 14 | + {...props} |
| 15 | + className={clsx('ddorg__carbon-wizard-navigation-horizontal', props.className)} |
| 16 | + > |
| 17 | + {stepsInfo.map(({ title, ...step }, index) => ( |
| 18 | + <ProgressStep key={index} label={title} disabled={index > props.currentIndex} {...step} /> |
| 19 | + ))} |
| 20 | + </ProgressIndicator> |
| 21 | +); |
4 | 22 |
|
5 |
| -const WizardInternal = () => { |
6 |
| - const { formOptions, currentStep, handlePrev, onKeyDown, handleNext, activeStepIndex, selectNext } = useContext(WizardContext); |
| 23 | +WizardNav.propTypes = { |
| 24 | + stepsInfo: PropTypes.arrayOf( |
| 25 | + PropTypes.shape({ |
| 26 | + title: PropTypes.node |
| 27 | + }) |
| 28 | + ), |
| 29 | + currentIndex: PropTypes.number, |
| 30 | + jumpToStep: PropTypes.func, |
| 31 | + className: PropTypes.string |
| 32 | +}; |
| 33 | + |
| 34 | +const defaultLabels = { |
| 35 | + submit: 'Submit', |
| 36 | + back: 'Back', |
| 37 | + next: 'Next' |
| 38 | +}; |
| 39 | + |
| 40 | +const Layout = ({ nav, fields }) => ( |
| 41 | + <React.Fragment> |
| 42 | + {nav} |
| 43 | + {fields} |
| 44 | + </React.Fragment> |
| 45 | +); |
| 46 | + |
| 47 | +Layout.propTypes = { |
| 48 | + nav: PropTypes.node, |
| 49 | + fields: PropTypes.node |
| 50 | +}; |
| 51 | + |
| 52 | +const VerticalLayout = ({ nav, fields }) => ( |
| 53 | + <Grid narrow> |
| 54 | + <Row> |
| 55 | + <Column sm={1} md={2} lg={3}> |
| 56 | + {nav} |
| 57 | + </Column> |
| 58 | + <Column>{fields}</Column> |
| 59 | + </Row> |
| 60 | + </Grid> |
| 61 | +); |
| 62 | + |
| 63 | +VerticalLayout.propTypes = { |
| 64 | + nav: PropTypes.node, |
| 65 | + fields: PropTypes.node |
| 66 | +}; |
| 67 | + |
| 68 | +const WizardInternal = ({ |
| 69 | + stepsInfo, |
| 70 | + buttonLabels, |
| 71 | + ButtonSetProps, |
| 72 | + BackButtonProps, |
| 73 | + NextButtonProps, |
| 74 | + SubmitButtonProps, |
| 75 | + ProgressIndicatorProps, |
| 76 | + vertical, |
| 77 | + ...props |
| 78 | +}) => { |
| 79 | + const { formOptions, currentStep, handlePrev, onKeyDown, handleNext, activeStepIndex, selectNext, jumpToStep } = useContext(WizardContext); |
| 80 | + |
| 81 | + const finalButtoLabels = { |
| 82 | + ...defaultLabels, |
| 83 | + ...buttonLabels |
| 84 | + }; |
| 85 | + |
| 86 | + const nav = stepsInfo && ( |
| 87 | + <WizardNav vertical={vertical} stepsInfo={stepsInfo} currentIndex={activeStepIndex} jumpToStep={jumpToStep} {...ProgressIndicatorProps} /> |
| 88 | + ); |
| 89 | + const fields = currentStep.fields.map((item) => formOptions.renderForm([item], formOptions)); |
| 90 | + |
| 91 | + const WizardLayout = vertical && nav ? VerticalLayout : Layout; |
7 | 92 |
|
8 | 93 | return (
|
9 |
| - <div onKeyDown={onKeyDown}> |
10 |
| - {currentStep.fields.map((item) => formOptions.renderForm([item], formOptions))} |
| 94 | + <div onKeyDown={onKeyDown} {...props}> |
| 95 | + <WizardLayout nav={nav ? nav : null} fields={fields} /> |
11 | 96 | <FormSpy>
|
12 |
| - {({ valid }) => ( |
13 |
| - <div> |
14 |
| - <button onClick={() => handleNext(selectNext(currentStep.nextStep, formOptions.getState))} disabled={!valid}> |
15 |
| - Next |
16 |
| - </button> |
17 |
| - <button onClick={handlePrev} disabled={activeStepIndex === 0}> |
18 |
| - Back |
19 |
| - </button> |
| 97 | + {({ invalid, validating, submitting }) => ( |
| 98 | + <div {...ButtonSetProps} className={clsx('ddorg__carbon-wizard-button-set', ButtonSetProps.className)}> |
| 99 | + {currentStep.nextStep ? ( |
| 100 | + <Button |
| 101 | + onClick={() => handleNext(selectNext(currentStep.nextStep, formOptions.getState))} |
| 102 | + disabled={!formOptions.valid || invalid || validating || submitting} |
| 103 | + {...NextButtonProps} |
| 104 | + > |
| 105 | + {finalButtoLabels.next} |
| 106 | + </Button> |
| 107 | + ) : ( |
| 108 | + <Button |
| 109 | + onClick={() => formOptions.handleSubmit()} |
| 110 | + disabled={!formOptions.valid || invalid || validating || submitting} |
| 111 | + {...SubmitButtonProps} |
| 112 | + > |
| 113 | + {finalButtoLabels.submit} |
| 114 | + </Button> |
| 115 | + )} |
| 116 | + <Button kind="secondary" onClick={handlePrev} disabled={activeStepIndex === 0} {...BackButtonProps}> |
| 117 | + {finalButtoLabels.back} |
| 118 | + </Button> |
20 | 119 | </div>
|
21 | 120 | )}
|
22 | 121 | </FormSpy>
|
23 | 122 | </div>
|
24 | 123 | );
|
25 | 124 | };
|
26 | 125 |
|
| 126 | +WizardInternal.propTypes = { |
| 127 | + stepsInfo: PropTypes.arrayOf( |
| 128 | + PropTypes.shape({ |
| 129 | + title: PropTypes.node |
| 130 | + }) |
| 131 | + ), |
| 132 | + buttonLabels: PropTypes.shape({ |
| 133 | + submit: PropTypes.node, |
| 134 | + back: PropTypes.node, |
| 135 | + next: PropTypes.node |
| 136 | + }), |
| 137 | + BackButtonProps: PropTypes.object, |
| 138 | + NextButtonProps: PropTypes.object, |
| 139 | + SubmitButtonProps: PropTypes.object, |
| 140 | + ButtonSetProps: PropTypes.object, |
| 141 | + ProgressIndicatorProps: PropTypes.object, |
| 142 | + vertical: PropTypes.bool |
| 143 | +}; |
| 144 | + |
| 145 | +WizardInternal.defaultProps = { |
| 146 | + BackButtonProps: {}, |
| 147 | + NextButtonProps: {}, |
| 148 | + SubmitButtonProps: {}, |
| 149 | + ButtonSetProps: {}, |
| 150 | + ProgressIndicatorProps: {} |
| 151 | +}; |
| 152 | + |
27 | 153 | const Wizard = (props) => <WizardCommon Wizard={WizardInternal} {...props} />;
|
28 | 154 |
|
29 | 155 | export default Wizard;
|
0 commit comments