Debian Codegraph @

node/@patternfly/react-core/src/components/Progress/ProgressContainer.tsx

raw
1 import { Fragment, useState, useRef, useEffect } from 'react';
2 import progressStyle from '@patternfly/react-styles/css/components/Progress/progress';
3 import { css } from '@patternfly/react-styles';
4 import { Tooltip, TooltipPosition } from '../Tooltip';
5 import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon';
6 import RhUiErrorFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-error-fill-icon';
7 import RhUiWarningFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-warning-fill-icon';
8 import { AriaProps, ProgressBar } from './ProgressBar';
9 import { ProgressHelperText } from './ProgressHelperText';
10
11 export enum ProgressMeasureLocation {
12 outside = 'outside',
13 inside = 'inside',
14 top = 'top',
15 none = 'none'
16 }
17
18 export enum ProgressVariant {
19 danger = 'danger',
20 success = 'success',
21 warning = 'warning'
22 }
23
24 export interface ProgressContainerProps extends Omit<React.HTMLProps<HTMLDivElement>, 'label' | 'title'> {
25 /** Properties needed for aria support */
26 progressBarAriaProps?: AriaProps;
27 /** Progress component DOM ID. */
28 parentId: string;
29 /** Progress title. The isTitleTruncated property will only affect string titles. Node title truncation must be handled manually. */
30 title?: React.ReactNode;
31 /** Label to indicate what progress is showing. */
32 label?: React.ReactNode;
33 /** Type of progress status. */
34 variant?: 'danger' | 'success' | 'warning';
35 /** Location of progress value. */
36 measureLocation?: 'outside' | 'inside' | 'top' | 'none';
37 /** Actual progress value. */
38 value: number;
39 /** Whether string title should be truncated */
40 isTitleTruncated?: boolean;
41 /** Position of the tooltip which is displayed if title is truncated */
42 tooltipPosition?:
43 | TooltipPosition
44 | 'auto'
45 | 'top'
46 | 'bottom'
47 | 'left'
48 | 'right'
49 | 'top-start'
50 | 'top-end'
51 | 'bottom-start'
52 | 'bottom-end'
53 | 'left-start'
54 | 'left-end'
55 | 'right-start'
56 | 'right-end';
57 /** Content which can be used to convey additional information about the progress component.
58 * We recommend the helper text component as it was designed for this purpose.
59 */
60 helperText?: React.ReactNode;
61 /** Hide the status icon, helpful when space is limited (such as within table cells) */
62 hideStatusIcon?: boolean;
63 }
64
65 const variantToIcon = {
66 danger: RhUiErrorFillIcon,
67 success: RhUiCheckCircleFillIcon,
68 warning: RhUiWarningFillIcon
69 };
70
71 export const ProgressContainer: React.FunctionComponent<ProgressContainerProps> = ({
72 progressBarAriaProps,
73 value,
74 title = '',
75 parentId,
76 label = null,
77 variant = null,
78 measureLocation = ProgressMeasureLocation.top,
79 isTitleTruncated = false,
80 tooltipPosition,
81 helperText,
82 hideStatusIcon = false
83 }: ProgressContainerProps) => {
84 const StatusIcon = !hideStatusIcon && variantToIcon.hasOwnProperty(variant) && variantToIcon[variant];
85 const [tooltip, setTooltip] = useState('');
86 const titleRef = useRef(null);
87 const updateTooltip = (event: any) => {
88 if (event.target.offsetWidth < event.target.scrollWidth) {
89 setTooltip(title || event.target.innerHTML);
90 } else {
91 setTooltip('');
92 }
93 };
94
95 useEffect(() => {
96 if (tooltip !== '') {
97 titleRef.current.focus();
98 }
99 }, [tooltip]);
100
101 const _isTruncatedAndString = isTitleTruncated && typeof title === 'string';
102 const Title = (
103 <div
104 className={css(progressStyle.progressDescription, _isTruncatedAndString && progressStyle.modifiers.truncate)}
105 id={`${parentId}-description`}
106 aria-hidden={_isTruncatedAndString ? null : 'true'}
107 onMouseEnter={_isTruncatedAndString ? updateTooltip : null}
108 onFocus={_isTruncatedAndString ? updateTooltip : null}
109 {...(_isTruncatedAndString && { tabIndex: 0 })}
110 ref={titleRef}
111 >
112 {title}
113 </div>
114 );
115
116 return (
117 <Fragment>
118 {title && (
119 <>
120 {tooltip && <Tooltip position={tooltipPosition} content={tooltip} isVisible triggerRef={titleRef} />}
121 {Title}
122 </>
123 )}
124 {(measureLocation !== ProgressMeasureLocation.none || StatusIcon) && (
125 <div className={css(progressStyle.progressStatus)} aria-hidden="true">
126 {(measureLocation === ProgressMeasureLocation.top || measureLocation === ProgressMeasureLocation.outside) && (
127 <span className={css(progressStyle.progressMeasure)}>{label || `${value}%`}</span>
128 )}
129 {StatusIcon && (
130 <span className={css(progressStyle.progressStatusIcon)}>
131 <StatusIcon />
132 </span>
133 )}
134 </div>
135 )}
136 <ProgressBar progressBarAriaProps={progressBarAriaProps} value={value}>
137 {measureLocation === ProgressMeasureLocation.inside && `${value}%`}
138 </ProgressBar>
139 {helperText && <ProgressHelperText>{helperText}</ProgressHelperText>}
140 </Fragment>
141 );
142 };
143 ProgressContainer.displayName = 'ProgressContainer';