|
| 1 | +import React, { useReducer, useEffect } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { useFormApi } from '@data-driven-forms/react-form-renderer'; |
| 4 | + |
| 5 | +import get from 'lodash/get'; |
| 6 | +import set from 'lodash/set'; |
| 7 | +import flattenDeep from 'lodash/flattenDeep'; |
| 8 | +import handleEnter from './enter-handler'; |
| 9 | +import reducer, { DYNAMIC_WIZARD_TYPES, findCurrentStep } from './reducer'; |
| 10 | + |
| 11 | +const Wizard = ({ fields, isDynamic, crossroads, Wizard, ...props }) => { |
| 12 | + const formOptions = useFormApi(); |
| 13 | + |
| 14 | + const [state, dispatch] = useReducer(reducer, { |
| 15 | + activeStep: fields[0].name, |
| 16 | + prevSteps: [], |
| 17 | + activeStepIndex: 0, |
| 18 | + maxStepIndex: 0, |
| 19 | + isDynamic: isDynamic || fields.some(({ nextStep }) => DYNAMIC_WIZARD_TYPES.includes(typeof nextStep)), |
| 20 | + loading: true |
| 21 | + }); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + dispatch({ type: 'finishLoading', payload: { formOptions, fields } }); |
| 25 | + }, [fields]); |
| 26 | + |
| 27 | + if (state.loading) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + const prepareValues = (values, visitedSteps, getRegisteredFields) => { |
| 32 | + // Add the final step fields to history |
| 33 | + const finalRegisteredFieldsHistory = { |
| 34 | + ...state.registeredFieldsHistory, |
| 35 | + [state.activeStep]: getRegisteredFields() |
| 36 | + }; |
| 37 | + |
| 38 | + const finalObject = {}; |
| 39 | + |
| 40 | + // Find only visited fields |
| 41 | + flattenDeep( |
| 42 | + Object.values([...visitedSteps, state.activeStep].reduce((obj, key) => ({ ...obj, [key]: finalRegisteredFieldsHistory[key] }), {})) |
| 43 | + ).forEach((key) => set(finalObject, key, get(values, key))); |
| 44 | + |
| 45 | + return finalObject; |
| 46 | + }; |
| 47 | + |
| 48 | + const handleSubmit = () => |
| 49 | + formOptions.onSubmit( |
| 50 | + prepareValues(formOptions.getState().values, [...state.prevSteps, state.activeStep], formOptions.getRegisteredFields), |
| 51 | + formOptions |
| 52 | + ); |
| 53 | + |
| 54 | + const jumpToStep = (index, valid) => dispatch({ type: 'jumpToStep', payload: { index, valid, fields, crossroads, formOptions } }); |
| 55 | + |
| 56 | + const handlePrev = () => jumpToStep(state.activeStepIndex - 1); |
| 57 | + |
| 58 | + const handleNext = (nextStep) => dispatch({ type: 'handleNext', payload: { nextStep, formOptions, fields } }); |
| 59 | + |
| 60 | + const setPrevSteps = () => dispatch({ type: 'setPrevSteps', payload: { formOptions, fields } }); |
| 61 | + |
| 62 | + const findCurrentStepWrapped = (step) => findCurrentStep(step, fields); |
| 63 | + |
| 64 | + const onKeyDown = (e) => handleEnter(e, formOptions, state.activeStep, findCurrentStepWrapped, handleNext, handleSubmit); |
| 65 | + |
| 66 | + return ( |
| 67 | + <Wizard |
| 68 | + {...props} |
| 69 | + handleNext={handleNext} |
| 70 | + onKeyDown={onKeyDown} |
| 71 | + setPrevSteps={setPrevSteps} |
| 72 | + currentStep={findCurrentStep(state.activeStep, fields)} |
| 73 | + jumpToStep={jumpToStep} |
| 74 | + handlePrev={handlePrev} |
| 75 | + formOptions={{ |
| 76 | + ...formOptions, |
| 77 | + handleSubmit |
| 78 | + }} |
| 79 | + navSchema={state.navSchema} |
| 80 | + activeStepIndex={state.activeStepIndex} |
| 81 | + maxStepIndex={state.maxStepIndex} |
| 82 | + isDynamic={state.isDynamic} |
| 83 | + crossroads={crossroads} |
| 84 | + prevSteps={state.prevSteps} |
| 85 | + /> |
| 86 | + ); |
| 87 | +}; |
| 88 | + |
| 89 | +Wizard.propTypes = { |
| 90 | + fields: PropTypes.arrayOf( |
| 91 | + PropTypes.shape({ |
| 92 | + name: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired |
| 93 | + }) |
| 94 | + ).isRequired, |
| 95 | + isDynamic: PropTypes.bool, |
| 96 | + crossroads: PropTypes.arrayOf(PropTypes.string), |
| 97 | + Wizard: PropTypes.oneOfType([PropTypes.node, PropTypes.func]) |
| 98 | +}; |
| 99 | + |
| 100 | +export default Wizard; |
| 101 | + |
| 102 | +export const wizardProps = { |
| 103 | + currentStep: PropTypes.object, |
| 104 | + handlePrev: PropTypes.func, |
| 105 | + onKeyDown: PropTypes.func, |
| 106 | + jumpToStep: PropTypes.func, |
| 107 | + setPrevSteps: PropTypes.func, |
| 108 | + handleNext: PropTypes.func, |
| 109 | + navSchema: PropTypes.array, |
| 110 | + activeStepIndex: PropTypes.number, |
| 111 | + maxStepIndex: PropTypes.number, |
| 112 | + formOptions: PropTypes.shape({ |
| 113 | + onCancel: PropTypes.func |
| 114 | + }), |
| 115 | + prevSteps: PropTypes.array |
| 116 | +}; |
0 commit comments