/
index.tsx
163 lines (153 loc) · 4.23 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as React from "react";
import styled, { css } from "styled-components";
import WizardStep from "./WizardStep";
import { WizardStepContextProvider } from "./WizardContext";
import Button from "../Button";
import Stack from "../Stack";
import ChevronDown from "../icons/ChevronDown";
import Portal from "../Portal";
import Modal from "../Modal";
import { CardSection } from "../Card";
import useMediaQuery from "../hooks/useMediaQuery";
import defaultTheme from "../defaultTheme";
import mq from "../utils/mediaQuery";
import type { Props, WizardStepProps } from "./types";
const unstyledListMixin = css`
list-style-type: none;
margin: 0;
padding: 0;
`;
// support column layout on desktop
// https://github.com/kiwicom/orbit/issues/3308
const StyledList = styled.ul<{ $direction?: "row" | "column" }>`
${({ $direction }) => css`
display: flex;
${unstyledListMixin};
${mq.largeMobile(css`
flex-direction: ${$direction};
li {
flex: 1 1 0%;
}
`)}
`}
`;
StyledList.defaultProps = {
theme: defaultTheme,
};
const Wizard = ({
dataTest,
lockScrolling = true,
direction,
id,
labelClose = "Close",
labelProgress,
completedSteps,
activeStep,
children,
onChangeStep,
}: Props) => {
const { isLargeMobile } = useMediaQuery();
const [open, setOpen] = React.useState(false);
const toggle = React.useRef(null);
const isCompact = !isLargeMobile;
const childrenArray = React.Children.toArray(children) as React.ReactElement<WizardStepProps>[];
const stepStatuses = childrenArray.map((step, index) => {
if (index < completedSteps) return "completed";
if (index === completedSteps) return "available";
return "disabled";
});
const activeStepTitle = childrenArray.find((_, index) => index === activeStep)?.props.title;
const stepsCount = React.Children.count(children);
const steps = React.Children.map(children, (step, index) => (
<WizardStepContextProvider
index={index}
status={stepStatuses[index]}
isLastStep={index === stepsCount - 1}
isColumnOnDesktop={direction === "column"}
nextStepStatus={stepStatuses[index + 1]}
isCompact={isCompact}
isActive={activeStep === index}
onChangeStep={onChangeStep}
onClose={() => setOpen(false)}
>
{step}
</WizardStepContextProvider>
));
if (isCompact) {
return (
<>
<Button
ref={toggle}
dataTest={dataTest}
ariaControls={id}
ariaExpanded={open}
type="secondary"
fullWidth
iconRight={<ChevronDown />}
onClick={() => {
setOpen(true);
}}
>
<Stack as="span" inline>
<b>{labelProgress}</b>
<span
css={css`
font-weight: normal;
`}
>
{activeStepTitle}
</span>
</Stack>
</Button>
<Portal>
<div id={id}>
{open && (
<Modal
hasCloseButton={false}
lockScrolling={lockScrolling}
onClose={() => {
setOpen(false);
}}
>
<nav
css={css`
/* matching this to ModalBody's border-radius */
padding-top: 9px;
`}
>
<ul
css={css`
${unstyledListMixin};
`}
>
{steps}
<li>
<CardSection>
<Button
type="secondary"
fullWidth
onClick={() => {
setOpen(false);
}}
>
{labelClose}
</Button>
</CardSection>
</li>
</ul>
</nav>
</Modal>
)}
</div>
</Portal>
</>
);
}
return (
<nav>
<StyledList $direction={direction}>{steps}</StyledList>
</nav>
);
};
export default Wizard;
export { WizardStep };