-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWizardStep.tsx
84 lines (74 loc) · 2.05 KB
/
WizardStep.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
import * as React from "react";
import {
Step,
StepLabel,
StepLabelProps,
SvgIcon,
Typography,
useStepContext
} from "@mui/material";
import { Error as ErrorIcon } from "@mui/icons-material";
import { WizardStepProps } from "./WizardStep.types";
/**
* The WizardStep component allows you to create a multi-step form stepper.
* See also the WizardSteps component.
*/
export default function WizardStep({
label,
completed = false,
index,
last,
helperText,
error
}: WizardStepProps) {
return (
<Step completed={completed} index={index} last={last}>
<WizardStepLabel label={label} helperText={helperText} error={error} />
</Step>
);
}
/**
* The WizardStepLabel is a helper component for the WizardStep component. It provides functionality for the helperText and errorText props. It is a separate component so that it can be used in the MUI Step component and hence access the context state.
*/
function WizardStepLabel({
label,
helperText,
error
}: Pick<WizardStepProps, "label" | "helperText" | "error">) {
// get context state
const stepContext = useStepContext();
let active: boolean, completed: boolean;
if ("completed" in stepContext && "active" in stepContext) {
active = stepContext.active;
completed = stepContext.completed;
} else {
throw new Error(
"WizardStepLabel must be used within a WizardStep component."
);
}
// Initialize step label props
const stepLabelProps: StepLabelProps = {};
// Set optional node for helper text
if (helperText) {
let textColor = "inherit";
if (error) {
textColor = "error";
} else if (active || completed) {
textColor = "textPrimary";
}
stepLabelProps.optional = (
<Typography variant="caption" color={textColor}>
{helperText}
</Typography>
);
}
// Set error icon
if (error) {
stepLabelProps.error = true;
stepLabelProps.icon = (
<SvgIcon component={ErrorIcon} color="error" fontSize="medium" />
);
}
// render
return <StepLabel {...stepLabelProps}>{label}</StepLabel>;
}