-
Notifications
You must be signed in to change notification settings - Fork 31
/
SetupItem.jsx
153 lines (142 loc) · 4.14 KB
/
SetupItem.jsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @component
*/
import classNames from 'clsx';
import PropTypes from 'prop-types';
import React, { useEffect } from 'react';
import { isDisabled } from '../utils/setupWizardHelper';
import SetupItemRight from './SetupItemRight';
import withSetupWizardContext from './withSetupWizardContext';
/**
* An item that represents one step in a `SetupWizard`.
*/
const SetupWizardItem = ({
step,
showStep,
title,
open: openProp = false,
ready: readyProp = false,
disabled: disabledProp = false,
onClick: onClickProp,
required = false,
contentStyle = {},
children,
enabledSteps = [],
completedSteps = [],
currentStep = -1,
toStep,
stepRequired,
right,
stepEnabled,
stepComplete,
}) => {
useEffect(() => {
stepRequired(required, step);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
const disabled = isDisabled(enabledSteps, step);
if (disabled && !disabledProp && readyProp) {
stepEnabled(true, step);
stepComplete(true, step);
}
}, [
enabledSteps,
disabledProp,
readyProp,
step,
stepComplete,
stepEnabled,
]);
const disabled = isDisabled(enabledSteps, step) || disabledProp;
const open = step === currentStep || openProp;
const ready = completedSteps.indexOf(step) >= 0 || readyProp;
const onClick = (event) => {
if (!disabled) {
if (step === currentStep) {
toStep(-1);
} else {
toStep(step);
}
if (onClickProp) {
onClickProp(event);
}
}
};
return (
<div
className={classNames('accordion', {
'accordion--open': open,
'accordion--disabled': disabled,
})}
>
<div
className={classNames(
'accordion__head',
'no-arrow',
'ellipsis',
'wizardHead',
{ pointer: !disabled }
)}
onClick={onClick}
>
<div className="accordion__head__icon">
<i className="react-chayns-icon ts-angle-right" />
</div>
<div className="accordion__head__title">
{showStep !== null &&
`${
(typeof showStep === 'number' ? showStep : step) + 1
}. `}
{title}
</div>
<SetupItemRight ready={ready} right={right} />
</div>
<div className="accordion__body" style={contentStyle}>
{children}
</div>
</div>
);
};
SetupWizardItem.propTypes = {
/**
* The index of the step (`0`-based).
*/
step: PropTypes.number.isRequired,
showStep: PropTypes.number,
/**
* The title of the step.
*/
title: PropTypes.string.isRequired,
toStep: PropTypes.func.isRequired,
stepRequired: PropTypes.func.isRequired,
open: PropTypes.bool,
ready: PropTypes.bool,
disabled: PropTypes.bool,
onClick: PropTypes.func,
contentStyle: PropTypes.objectOf(
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
),
children: PropTypes.element,
/**
* Wether the step is required to continue the wizard.
*/
required: PropTypes.bool,
enabledSteps: PropTypes.arrayOf(PropTypes.number),
completedSteps: PropTypes.arrayOf(PropTypes.number),
currentStep: PropTypes.number,
stepEnabled: PropTypes.func.isRequired,
stepComplete: PropTypes.func.isRequired,
/**
* A component that is shown on the right hand of the accordion head.
*/
right: PropTypes.oneOfType([
PropTypes.node.isRequired,
PropTypes.shape({
complete: PropTypes.node.isRequired,
notComplete: PropTypes.node.isRequired,
}).isRequired,
]),
};
SetupWizardItem.displayName = 'SetupWizardItem';
export default withSetupWizardContext(SetupWizardItem);