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