|
1
|
|
|
import React from "react"; |
|
2
|
|
|
import { ProgressTrackerItem } from "./types"; |
|
3
|
|
|
import ProgressTrackerItemComponent from "./ProgressTrackerItem"; |
|
4
|
|
|
|
|
5
|
|
|
export interface ProgressTrackerProps { |
|
6
|
|
|
items?: ProgressTrackerItem[]; |
|
7
|
|
|
children?: React.ReactNode; |
|
8
|
|
|
backgroundColor?: string; |
|
9
|
|
|
backgroundOpacity?: string; |
|
10
|
|
|
fontColor?: string; |
|
11
|
|
|
classNames?: string; |
|
12
|
|
|
itemsWrapperClassNames?: string; |
|
13
|
|
|
dataIsLoading?: boolean; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
const isActiveItem = ({ state }): boolean => { |
|
17
|
|
|
return state === "active"; |
|
18
|
|
|
}; |
|
19
|
|
|
|
|
20
|
|
|
const ProgressTracker: React.FunctionComponent<ProgressTrackerProps> = ({ |
|
21
|
|
|
items, |
|
22
|
|
|
children, |
|
23
|
|
|
backgroundColor, |
|
24
|
|
|
backgroundOpacity, |
|
25
|
|
|
fontColor, |
|
26
|
|
|
classNames, |
|
27
|
|
|
itemsWrapperClassNames, |
|
28
|
|
|
dataIsLoading, |
|
29
|
|
|
}): React.ReactElement => { |
|
30
|
|
|
const active = items !== undefined ? items.find(isActiveItem) : undefined; |
|
31
|
|
|
const activeItemLabel = active !== undefined ? active.label : ""; |
|
32
|
|
|
const activeItemTitle = active !== undefined ? active.title : ""; |
|
33
|
|
|
const activeIndex = items ? items.findIndex(isActiveItem) : -1; |
|
34
|
|
|
|
|
35
|
|
|
return ( |
|
36
|
|
|
<div |
|
37
|
|
|
data-c-alignment="base(centre)" |
|
38
|
|
|
data-c-background={`${backgroundColor}(${backgroundOpacity || 100})`} |
|
39
|
|
|
className={classNames} |
|
40
|
|
|
data-c-padding="top(quarter) bottom(quarter)" |
|
41
|
|
|
> |
|
42
|
|
|
<ol |
|
43
|
|
|
tabIndex={0} |
|
44
|
|
|
role="progressbar" |
|
45
|
|
|
aria-valuemin={1} |
|
46
|
|
|
aria-valuemax={items && items.length} |
|
47
|
|
|
aria-valuenow={activeIndex} |
|
48
|
|
|
aria-valuetext={`${activeItemLabel} : ${activeItemTitle}`} |
|
49
|
|
|
className={itemsWrapperClassNames} |
|
50
|
|
|
data-c-container="layout" |
|
51
|
|
|
> |
|
52
|
|
|
{/* If items list exists, then return list of progress tracker item components. Also, progress tracker items can be passed down to the children props, and will be printed out below. */} |
|
53
|
|
|
{items && |
|
54
|
|
|
items.map( |
|
55
|
|
|
(item): React.ReactElement => { |
|
56
|
|
|
const { link, state, label, title } = item; |
|
57
|
|
|
return ( |
|
58
|
|
|
<ProgressTrackerItemComponent |
|
59
|
|
|
link={link} |
|
60
|
|
|
key={title} |
|
61
|
|
|
state={state} |
|
62
|
|
|
label={label} |
|
63
|
|
|
title={title} |
|
64
|
|
|
fontColor={fontColor} |
|
65
|
|
|
dataIsLoading={dataIsLoading} |
|
66
|
|
|
/> |
|
67
|
|
|
); |
|
68
|
|
|
}, |
|
69
|
|
|
)} |
|
70
|
|
|
{children} |
|
71
|
|
|
</ol> |
|
72
|
|
|
</div> |
|
73
|
|
|
); |
|
74
|
|
|
}; |
|
75
|
|
|
|
|
76
|
|
|
export default ProgressTracker; |
|
77
|
|
|
|