-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiStepFormHook.js
73 lines (64 loc) · 2.13 KB
/
multiStepFormHook.js
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
import { useEffect, useState } from 'react';
/**
* A Custom hook for creating React Multi Step Forms.
*
* @param {Array.<React.Component> | Array.<JSX.Element>} allFormSections - An Array containing all Sections of the Multi Step Form
* @return {{changeActiveSectionNumber: function, activeSectionNumber: number, maxSectionReached: number, resetMultiStepProgress: function, activeSection: React.Component}}
*/
const useMultiStepForm = allFormSections => {
const totalSections = allFormSections.length;
/**
* activeSectionNumber keeps track of the Currently
* Active Form Section Number.
*/
const [activeSectionNumber, setActiveSectionNumber] = useState(1);
/**
* maxSectionReached keeps track of the Max Form
* Section Number user has reached.
*/
const [maxSectionReached, setMaxSectionReached] = useState(1);
/**
* activeSection represents the currently active
* Form Section.
* @type React.Component
*/
const activeSection = allFormSections[activeSectionNumber - 1];
useEffect(() => {
if (activeSectionNumber > maxSectionReached) {
setMaxSectionReached(activeSectionNumber);
}
}, [activeSectionNumber]);
/**
* changeActiveSectionNumber is responsible changing the
* current active section number.
* It can be used to traverse all the form sections.
*
* @param {Number} newActiveSectionNumber - New Active Section Number
*/
const changeActiveSectionNumber = newActiveSectionNumber => {
setActiveSectionNumber(currentActiveSection => {
if (newActiveSectionNumber > totalSections) {
return currentActiveSection;
} else if (newActiveSectionNumber < 1) {
return currentActiveSection;
}
return newActiveSectionNumber;
});
};
/***
* resetMultiStepProgress is responsible for resetting
* the progress of the user along the multi step Form.
*/
const resetMultiStepProgress = () => {
setActiveSectionNumber(1);
setMaxSectionReached(1);
};
return {
activeSection,
activeSectionNumber,
maxSectionReached,
changeActiveSectionNumber,
resetMultiStepProgress,
};
};
export default useMultiStepForm;