This repository was archived by the owner on Oct 4, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMultiStepProgress.js
More file actions
204 lines (180 loc) · 4.1 KB
/
Copy pathMultiStepProgress.js
File metadata and controls
204 lines (180 loc) · 4.1 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// External dependencies.
import React from "react";
import PropTypes from "prop-types";
import styled from "styled-components";
// Yoast dependencies.
import { colors } from "@yoast/style-guide";
// Internal dependencies.
import SvgIcon from "./SvgIcon";
const MultiStepProgressContainer = styled.div`
padding: 8px;
`;
/**
* Ordered list containing the steps.
*/
const MultiStepProgressList = styled.ol`
padding: 0;
margin: 0;
list-style: none;
counter-reset: multi-step-progress-counter;
li {
counter-increment: multi-step-progress-counter;
}
`;
/**
* List item for a single step.
*/
const MultiStepProgressListItem = styled.li`
display: flex;
align-items: baseline;
margin: 8px 0;
:first-child {
margin-top: 0;
}
:last-child {
margin-bottom: 0;
}
span {
margin: 0 8px;
}
svg {
position: relative;
top: 2px;
}
::before {
content: counter( multi-step-progress-counter );
font-size: 12px;
background: ${ colors.$color_pink_dark };
border-radius: 50%;
min-width: 16px;
height: 16px;
padding: 4px;
color: ${ colors.$color_white };
text-align: center;
}
`;
/**
* Specific list item for a step in pending state.
*/
const MultiStepProgressPendingStatus = styled( MultiStepProgressListItem )`
span {
color: ${ colors.$palette_grey_text_light };
}
::before {
background-color: ${ colors.$palette_grey_medium_dark };
}
`;
/**
* Specific list item for a step in running state.
*/
const MultiStepProgressRunningStatus = styled( MultiStepProgressListItem )`
::before {
background-color: ${ colors.$palette_grey_medium_dark };
}
`;
/**
* A component to track multiple steps in a process.
*/
class MultiStepProgress extends React.Component {
/**
* Returns the rendered html.
*
* @returns {React.Element} The rendered html.
*/
render() {
return (
<MultiStepProgressContainer
role="status"
aria-live="polite"
aria-relevant="additions text"
aria-atomic={ true }
>
<MultiStepProgressList>
{ this.props.steps.map( ( step ) => {
switch ( step.status ) {
case "running":
return this.renderRunningState( step );
case "failed":
return this.renderFailedState( step );
case "finished":
return this.renderFinishedState( step );
case "pending":
default:
return this.renderPendingState( step );
}
} ) }
</MultiStepProgressList>
</MultiStepProgressContainer>
);
}
/**
* Renders a list item in pending state.
*
* @param {Object} step Step content.
*
* @returns {React.Element} The list item.
*/
renderPendingState( step ) {
return (
<MultiStepProgressPendingStatus key={ step.id }>
<span>{ step.text }</span>
</MultiStepProgressPendingStatus>
);
}
/**
* Renders a list item in running state.
*
* @param {Object} step Step content.
*
* @returns {React.Element} The list item.
*/
renderRunningState( step ) {
return (
<MultiStepProgressRunningStatus key={ step.id }>
<span>{ step.text }</span>
<SvgIcon icon="loading-spinner" />
</MultiStepProgressRunningStatus>
);
}
/**
* Renders a list item in finished state.
*
* @param {Object} step Step content.
*
* @returns {React.Element} The list item.
*/
renderFinishedState( step ) {
return (
<MultiStepProgressListItem key={ step.id }>
<span>{ step.text }</span>
<SvgIcon icon="check" color={ colors.$color_green_medium_light } />
</MultiStepProgressListItem>
);
}
/**
* Renders a list item in failed state.
*
* @param {Object} step Step content.
*
* @returns {React.Element} The list item.
*/
renderFailedState( step ) {
return (
<MultiStepProgressListItem key={ step.id }>
<span>{ step.text }</span>
<SvgIcon icon="times" color={ colors.$color_red } />
</MultiStepProgressListItem>
);
}
}
MultiStepProgress.defaultProps = {
steps: [],
};
MultiStepProgress.propTypes = {
steps: PropTypes.arrayOf( PropTypes.shape( {
status: PropTypes.oneOf( [ "pending", "running", "finished", "failed" ] ).isRequired,
text: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
} ) ),
};
export default MultiStepProgress;