|
| 1 | +import React, { useContext, useState } from 'react'; |
| 2 | + |
| 3 | +import FormRenderer from '@data-driven-forms/react-form-renderer/form-renderer'; |
| 4 | +import WizardContext from '@data-driven-forms/react-form-renderer/wizard-context'; |
| 5 | +import FormSpy from '@data-driven-forms/react-form-renderer/form-spy'; |
| 6 | + |
| 7 | +import Wizard from '@data-driven-forms/common/wizard'; |
| 8 | +import selectNext from '@data-driven-forms/common/wizard/select-next'; |
| 9 | + |
| 10 | +import FormTemplate from '@data-driven-forms/mui-component-mapper/form-template'; |
| 11 | +import Select from '@data-driven-forms/mui-component-mapper/select'; |
| 12 | +import TextField from '@data-driven-forms/mui-component-mapper/text-field'; |
| 13 | + |
| 14 | +const schema = { |
| 15 | + fields: [ |
| 16 | + { |
| 17 | + component: 'wizard', |
| 18 | + name: 'custom-wizard', |
| 19 | + fields: [ |
| 20 | + { |
| 21 | + name: 'first-step', |
| 22 | + title: 'Select step', |
| 23 | + nextStep: ({ values }) => values['selected-step'], |
| 24 | + fields: [ |
| 25 | + { |
| 26 | + component: 'select', |
| 27 | + name: 'selected-step', |
| 28 | + label: 'Select next step', |
| 29 | + isRequired: true, |
| 30 | + validate: [{ type: 'required' }], |
| 31 | + options: [ |
| 32 | + { label: 'Step a', value: 'step-a' }, |
| 33 | + { label: 'Step b', value: 'step-b' } |
| 34 | + ] |
| 35 | + } |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'step-a', |
| 40 | + title: 'Step A', |
| 41 | + fields: [ |
| 42 | + { |
| 43 | + component: 'text-field', |
| 44 | + name: 'a-value', |
| 45 | + isRequired: true, |
| 46 | + validate: [{ type: 'required' }], |
| 47 | + label: 'A value' |
| 48 | + } |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'step-b', |
| 53 | + title: 'Step B', |
| 54 | + fields: [ |
| 55 | + { |
| 56 | + component: 'text-field', |
| 57 | + name: 'b-value', |
| 58 | + isRequired: true, |
| 59 | + validate: [{ type: 'required' }], |
| 60 | + label: 'B value' |
| 61 | + } |
| 62 | + ] |
| 63 | + } |
| 64 | + ] |
| 65 | + } |
| 66 | + ] |
| 67 | +}; |
| 68 | + |
| 69 | +const WizardInternal = (props) => { |
| 70 | + const { formOptions, currentStep, handlePrev, onKeyDown, handleNext, activeStepIndex } = useContext(WizardContext); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div onKeyDown={onKeyDown} style={{ width: '100%' }}> |
| 74 | + {currentStep.title} |
| 75 | + {formOptions.renderForm(currentStep.fields)} |
| 76 | + <FormSpy> |
| 77 | + {() => ( |
| 78 | + <React.Fragment> |
| 79 | + {currentStep.nextStep && ( |
| 80 | + <button disabled={!formOptions.getState().valid} onClick={() => handleNext(selectNext(currentStep.nextStep, formOptions.getState))}> |
| 81 | + Next |
| 82 | + </button> |
| 83 | + )} |
| 84 | + {!currentStep.nextStep && ( |
| 85 | + <button disabled={!formOptions.getState().valid} onClick={() => formOptions.handleSubmit()}> |
| 86 | + Submit |
| 87 | + </button> |
| 88 | + )} |
| 89 | + <button onClick={handlePrev} disabled={activeStepIndex === 0}> |
| 90 | + Back |
| 91 | + </button> |
| 92 | + </React.Fragment> |
| 93 | + )} |
| 94 | + </FormSpy> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +}; |
| 98 | + |
| 99 | +const WrappedWizard = (props) => <Wizard Wizard={WizardInternal} {...props} />; |
| 100 | + |
| 101 | +const CustomWizard = () => { |
| 102 | + const [values, setValues] = useState(); |
| 103 | + |
| 104 | + return ( |
| 105 | + <React.Fragment> |
| 106 | + <FormRenderer |
| 107 | + schema={schema} |
| 108 | + componentMapper={{ 'text-field': TextField, select: Select, wizard: WrappedWizard }} |
| 109 | + FormTemplate={(props) => <FormTemplate {...props} showFormControls={false} />} |
| 110 | + onSubmit={(values) => setValues(values)} |
| 111 | + /> |
| 112 | + {values && <pre>{JSON.stringify(values, null, 2)}</pre>} |
| 113 | + </React.Fragment> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export default CustomWizard; |
0 commit comments