forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccordion-form.tsx
140 lines (126 loc) · 3.7 KB
/
accordion-form.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { useState } from 'react';
import AccordionFormSection from './accordion-form-section';
import type {
AccordionSectionProps,
SectionGeneratorReturnType,
ValidationErrors,
ValidatorFunction,
} from './types';
interface AccordionFormProps< T > {
generatedSections?: AccordionSectionProps< T >[];
sectionGenerator?: () => (
props: SectionGeneratorReturnType< T >
) => AccordionSectionProps< T >[];
currentIndex: number;
updateCurrentIndex: ( index: number ) => void;
onSubmit: ( formValues: T ) => void;
formValues: T;
updateFormValues?: ( formValues: T ) => void;
saveFormValues: () => Promise< void >;
onErrorUpdates?: ( errors: ValidationErrors ) => void;
blockNavigation?: boolean;
isSaving: boolean;
hasUnsavedChanges: boolean;
}
export default function AccordionForm< T >( {
currentIndex,
updateCurrentIndex,
updateFormValues,
formValues,
saveFormValues,
onSubmit,
generatedSections,
onErrorUpdates,
blockNavigation,
isSaving,
hasUnsavedChanges,
}: AccordionFormProps< T > ) {
const [ formErrors, setFormErrors ] = useState< ValidationErrors >( {} );
const isSectionAtIndexTouchedInitialState: Record< string, boolean > = {};
for ( let i = 0; i <= currentIndex; i++ ) {
isSectionAtIndexTouchedInitialState[ `${ i }` ] = true;
}
const [ isSectionAtIndexTouched, setIsSectionAtIndexTouched ] = useState<
Record< string, boolean >
>( isSectionAtIndexTouchedInitialState );
const sections = generatedSections ?? [];
const runValidatorAndSetFormErrors = ( validator: ValidatorFunction< T > ) => {
const validationResult = validator( formValues );
setFormErrors( {
...formErrors,
...validationResult.errors,
} );
onErrorUpdates &&
onErrorUpdates( {
...formErrors,
...validationResult.errors,
} );
return validationResult;
};
const submitStep = () => {
// Re-run validation on all sections before submitting
for ( let index = 0; index < sections.length; index++ ) {
const section = sections[ index ];
if ( section.validate ) {
const validationResult = runValidatorAndSetFormErrors( section.validate );
if ( ! validationResult.result ) {
updateCurrentIndex( index );
return;
}
}
}
onSubmit( formValues );
};
const onOpen = ( currentIndex: number ) => {
updateCurrentIndex( currentIndex );
updateFormValues && updateFormValues( formValues );
setIsSectionAtIndexTouched( { ...isSectionAtIndexTouched, [ `${ currentIndex }` ]: true } );
// save values, but do not block UI.
if ( hasUnsavedChanges ) {
saveFormValues();
}
};
const onNext = async ( validator?: ValidatorFunction< T > ) => {
updateFormValues && updateFormValues( formValues );
if ( validator ) {
const validationResult = runValidatorAndSetFormErrors( validator );
if ( ! validationResult.result ) {
return;
}
}
if ( hasUnsavedChanges ) {
await saveFormValues();
}
if ( currentIndex < sections.length - 1 ) {
updateCurrentIndex( currentIndex + 1 );
setIsSectionAtIndexTouched( {
...isSectionAtIndexTouched,
[ `${ currentIndex + 1 }` ]: true,
} );
} else {
submitStep();
}
};
return (
<>
{ sections.map( ( section, index ) => (
<AccordionFormSection
key={ `${ index }` }
title={ section.title }
summary={ section.summary }
isExpanded={ index === currentIndex }
showSkip={ section.showSkip }
component={ section.component }
isTouched={ isSectionAtIndexTouched[ `${ index }` ] }
onNext={ () => onNext( section.validate ) }
onOpen={ () => onOpen( index ) }
onSave={ saveFormValues }
blockNavigation={ blockNavigation }
showSubmit={ index === sections.length - 1 }
isSaving={ isSaving }
hasUnsavedChanges={ hasUnsavedChanges }
/>
) ) }
</>
);
}