components
1 year ago
hooks
2 years ago
store
2 years ago
types
2 years ago
utilities
2 years ago
index.tsx
2 years ago
index.tsx
84 lines
| 1 | import type {Form} from '@givewp/forms/types'; |
| 2 | import {Section} from '@givewp/forms/types'; |
| 3 | import {DonationFormMultiStepStateProvider} from './store'; |
| 4 | import {StepObject} from '@givewp/forms/app/form/MultiStepForm/types'; |
| 5 | import StepForm from '@givewp/forms/app/form/MultiStepForm/components/StepForm'; |
| 6 | import getSectionFieldNames from '@givewp/forms/app/form/MultiStepForm/utilities/getSectionFieldNames'; |
| 7 | import DonationFormErrorBoundary from '@givewp/forms/app/errors/boundaries/DonationFormErrorBoundary'; |
| 8 | import {withTemplateWrapper} from '@givewp/forms/app/templates'; |
| 9 | import SectionNode from '@givewp/forms/app/fields/SectionNode'; |
| 10 | import Steps from '@givewp/forms/app/form/MultiStepForm/components/Steps'; |
| 11 | import HeaderStep from '@givewp/forms/app/form/MultiStepForm/components/HeaderStep'; |
| 12 | import {DonationSummaryProvider} from '@givewp/forms/app/store/donation-summary'; |
| 13 | |
| 14 | const FormSectionTemplate = withTemplateWrapper(window.givewp.form.templates.layouts.section, 'section'); |
| 15 | |
| 16 | /** |
| 17 | * @since 3.0.0 |
| 18 | */ |
| 19 | const convertSectionsToSteps = (sections: Section[], hasFirstStep: boolean) => { |
| 20 | const totalSteps = hasFirstStep ? sections.length + 1 : sections.length; |
| 21 | |
| 22 | return sections.map((section, index) => { |
| 23 | const currentStep = hasFirstStep ? index + 1 : index; |
| 24 | const isFirstStep = currentStep === 0; |
| 25 | const isLastStep = currentStep === totalSteps - 1; |
| 26 | const fields = getSectionFieldNames(section); |
| 27 | const title = section?.label; |
| 28 | const description = section?.description; |
| 29 | |
| 30 | const element = ( |
| 31 | <StepForm key={currentStep} currentStep={currentStep} isFirstStep={isFirstStep} isLastStep={isLastStep}> |
| 32 | <DonationFormErrorBoundary key={section.name}> |
| 33 | <FormSectionTemplate key={section.name} section={section} hideLabel> |
| 34 | {section.nodes.map((node) => ( |
| 35 | <DonationFormErrorBoundary key={node.name}> |
| 36 | <SectionNode key={node.name} node={node} /> |
| 37 | </DonationFormErrorBoundary> |
| 38 | ))} |
| 39 | </FormSectionTemplate> |
| 40 | </DonationFormErrorBoundary> |
| 41 | </StepForm> |
| 42 | ); |
| 43 | |
| 44 | return { |
| 45 | id: currentStep, |
| 46 | title, |
| 47 | description, |
| 48 | element, |
| 49 | fields, |
| 50 | visibilityConditions: section.visibilityConditions, |
| 51 | isVisible: !section.visibilityConditions.length, |
| 52 | } as StepObject; |
| 53 | }); |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * @since 3.6.0 updated to use includeHeaderInMultiStep |
| 58 | * @since 3.0.0 |
| 59 | */ |
| 60 | export default function MultiStepForm({form}: {form: Form}) { |
| 61 | const shouldIncludeHeaderInSteps = form.design?.includeHeaderInMultiStep && form.settings?.showHeader; |
| 62 | const steps = convertSectionsToSteps(form.nodes, shouldIncludeHeaderInSteps); |
| 63 | |
| 64 | if (shouldIncludeHeaderInSteps) { |
| 65 | steps.unshift({ |
| 66 | id: 0, |
| 67 | title: null, |
| 68 | description: null, |
| 69 | element: <HeaderStep form={form} />, |
| 70 | fields: [], |
| 71 | visibilityConditions: [], |
| 72 | isVisible: true, |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | return ( |
| 77 | <DonationFormMultiStepStateProvider initialState={{steps, currentStep: 0}}> |
| 78 | <DonationSummaryProvider> |
| 79 | <Steps steps={steps} /> |
| 80 | </DonationSummaryProvider> |
| 81 | </DonationFormMultiStepStateProvider> |
| 82 | ); |
| 83 | } |
| 84 |