forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressContainer.tsx
More file actions
140 lines (132 loc) · 4.66 KB
/
ProgressContainer.tsx
File metadata and controls
140 lines (132 loc) · 4.66 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
import { Fragment, useState, useRef, useEffect } from 'react';
import progressStyle from '@patternfly/react-styles/css/components/Progress/progress';
import { css } from '@patternfly/react-styles';
import { Tooltip, TooltipPosition } from '../Tooltip';
import CheckCircleIcon from '@patternfly/react-icons/dist/esm/icons/check-circle-icon';
import TimesCircleIcon from '@patternfly/react-icons/dist/esm/icons/times-circle-icon';
import ExclamationTriangleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-triangle-icon';
import { AriaProps, ProgressBar } from './ProgressBar';
import { ProgressHelperText } from './ProgressHelperText';
export enum ProgressMeasureLocation {
outside = 'outside',
inside = 'inside',
top = 'top',
none = 'none'
}
export enum ProgressVariant {
danger = 'danger',
success = 'success',
warning = 'warning'
}
export interface ProgressContainerProps extends Omit<React.HTMLProps<HTMLDivElement>, 'label' | 'title'> {
/** Properties needed for aria support */
progressBarAriaProps?: AriaProps;
/** Progress component DOM ID. */
parentId: string;
/** Progress title. The isTitleTruncated property will only affect string titles. Node title truncation must be handled manually. */
title?: React.ReactNode;
/** Label to indicate what progress is showing. */
label?: React.ReactNode;
/** Type of progress status. */
variant?: 'danger' | 'success' | 'warning';
/** Location of progress value. */
measureLocation?: 'outside' | 'inside' | 'top' | 'none';
/** Actual progress value. */
value: number;
/** Whether string title should be truncated */
isTitleTruncated?: boolean;
/** Position of the tooltip which is displayed if title is truncated */
tooltipPosition?:
| TooltipPosition
| 'auto'
| 'top'
| 'bottom'
| 'left'
| 'right'
| 'top-start'
| 'top-end'
| 'bottom-start'
| 'bottom-end'
| 'left-start'
| 'left-end'
| 'right-start'
| 'right-end';
/** Content which can be used to convey additional information about the progress component.
* We recommend the helper text component as it was designed for this purpose.
*/
helperText?: React.ReactNode;
}
const variantToIcon = {
danger: TimesCircleIcon,
success: CheckCircleIcon,
warning: ExclamationTriangleIcon
};
export const ProgressContainer: React.FunctionComponent<ProgressContainerProps> = ({
progressBarAriaProps,
value,
title = '',
parentId,
label = null,
variant = null,
measureLocation = ProgressMeasureLocation.top,
isTitleTruncated = false,
tooltipPosition,
helperText
}: ProgressContainerProps) => {
const StatusIcon = variantToIcon.hasOwnProperty(variant) && variantToIcon[variant];
const [tooltip, setTooltip] = useState('');
const titleRef = useRef(null);
const updateTooltip = (event: any) => {
if (event.target.offsetWidth < event.target.scrollWidth) {
setTooltip(title || event.target.innerHTML);
} else {
setTooltip('');
}
};
useEffect(() => {
if (tooltip !== '') {
titleRef.current.focus();
}
}, [tooltip]);
const _isTruncatedAndString = isTitleTruncated && typeof title === 'string';
const Title = (
<div
className={css(progressStyle.progressDescription, _isTruncatedAndString && progressStyle.modifiers.truncate)}
id={`${parentId}-description`}
aria-hidden={_isTruncatedAndString ? null : 'true'}
onMouseEnter={_isTruncatedAndString ? updateTooltip : null}
onFocus={_isTruncatedAndString ? updateTooltip : null}
{...(_isTruncatedAndString && { tabIndex: 0 })}
ref={titleRef}
>
{title}
</div>
);
return (
<Fragment>
{title && (
<>
{tooltip && <Tooltip position={tooltipPosition} content={tooltip} isVisible triggerRef={titleRef} />}
{Title}
</>
)}
{(measureLocation !== ProgressMeasureLocation.none || StatusIcon) && (
<div className={css(progressStyle.progressStatus)} aria-hidden="true">
{(measureLocation === ProgressMeasureLocation.top || measureLocation === ProgressMeasureLocation.outside) && (
<span className={css(progressStyle.progressMeasure)}>{label || `${value}%`}</span>
)}
{StatusIcon && (
<span className={css(progressStyle.progressStatusIcon)}>
<StatusIcon />
</span>
)}
</div>
)}
<ProgressBar progressBarAriaProps={progressBarAriaProps} value={value}>
{measureLocation === ProgressMeasureLocation.inside && `${value}%`}
</ProgressBar>
{helperText && <ProgressHelperText>{helperText}</ProgressHelperText>}
</Fragment>
);
};
ProgressContainer.displayName = 'ProgressContainer';