Debian Codegraph @

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

raw
1 import { Component } from 'react';
2 import styles from '@patternfly/react-styles/css/components/Progress/progress';
3 import { css } from '@patternfly/react-styles';
4 import { ProgressContainer, ProgressMeasureLocation } from './ProgressContainer';
5 import { AriaProps } from './ProgressBar';
6 import { GenerateId } from '../../helpers';
7
8 export enum ProgressSize {
9 sm = 'sm',
10 md = 'md',
11 lg = 'lg'
12 }
13
14 export interface ProgressProps extends Omit<React.HTMLProps<HTMLDivElement>, 'size' | 'label' | 'title'> {
15 /** Classname for progress component. */
16 className?: string;
17 /** Size variant of progress. */
18 size?: 'sm' | 'md' | 'lg';
19 /** Where the measure percent will be located. */
20 measureLocation?: 'outside' | 'inside' | 'top' | 'none';
21 /** Status variant of progress. */
22 variant?: 'danger' | 'success' | 'warning';
23 /** Title above progress. The isTitleTruncated property will only affect string titles. Node title truncation must be handled manually. */
24 title?: React.ReactNode;
25 /** Text description of current progress value to display instead of percentage. */
26 label?: React.ReactNode;
27 /** Actual value of progress. */
28 value?: number;
29 /** DOM id for progress component. */
30 id?: string;
31 /** Minimal value of progress. */
32 min?: number;
33 /** Maximum value of progress. */
34 max?: number;
35 /** Accessible text description of current progress value, for when value is not a percentage. Use with label. */
36 valueText?: string;
37 /** Indicate whether to truncate the string title */
38 isTitleTruncated?: boolean;
39 /** Position of the tooltip which is displayed if title is truncated */
40 tooltipPosition?: 'auto' | 'top' | 'bottom' | 'left' | 'right';
41 /** Adds accessible text to the ProgressBar. Required when title not used and there is not any label associated with the progress bar */
42 'aria-label'?: string;
43 /** Associates the ProgressBar with it's label for accessibility purposes. Required when title not used */
44 'aria-labelledby'?: string;
45 /** Adds an accessible description to the ProgressBar via space separated list of ids. Required when helperText is passed in. */
46 'aria-describedby'?: string;
47 /** Content which can be used to convey additional information about the progress component.
48 * We recommend the helper text component as it was designed for this purpose.
49 */
50 helperText?: React.ReactNode;
51 /** Flag indicating whether the status icon should be hidden, helpful when space is limited (such as within table cells). When set to true, you must ensure the context of the status is provided in another way, such as via the progress measure. */
52 hideStatusIcon?: boolean;
53 }
54
55 class Progress extends Component<ProgressProps> {
56 static displayName = 'Progress';
57 static defaultProps: ProgressProps = {
58 className: '',
59 measureLocation: ProgressMeasureLocation.top,
60 variant: null,
61 id: '',
62 title: '',
63 min: 0,
64 max: 100,
65 size: null as ProgressSize,
66 label: null as React.ReactNode,
67 value: 0,
68 valueText: null as string,
69 isTitleTruncated: false,
70 tooltipPosition: 'top' as 'auto' | 'top' | 'bottom' | 'left' | 'right',
71 'aria-label': null as string,
72 'aria-labelledby': null as string,
73 'aria-describedby': null as string
74 };
75
76 render() {
77 const {
78 id: idProp,
79 size,
80
81 className,
82 value,
83 title,
84 label,
85 variant,
86 measureLocation,
87 min,
88 max,
89 valueText,
90 isTitleTruncated,
91 tooltipPosition,
92 'aria-label': ariaLabel,
93 'aria-labelledby': ariaLabelledBy,
94 'aria-describedby': ariaDescribedBy,
95 helperText,
96 hideStatusIcon,
97 ...props
98 } = this.props;
99
100 if (!title && !ariaLabelledBy && !ariaLabel) {
101 /* eslint-disable no-console */
102 console.warn(
103 'One of aria-label or aria-labelledby properties should be passed when using the progress component without a title.'
104 );
105 }
106
107 return (
108 <GenerateId prefix="pf-progress-">
109 {(generatedId) => {
110 const id = idProp || generatedId;
111
112 const progressBarAriaProps: AriaProps = {
113 'aria-valuemin': min,
114 'aria-valuenow': value,
115 'aria-valuemax': max
116 };
117
118 if (title || ariaLabelledBy) {
119 progressBarAriaProps['aria-labelledby'] = title ? `${id}-description` : ariaLabelledBy;
120 }
121
122 if (ariaLabel) {
123 progressBarAriaProps['aria-label'] = ariaLabel;
124 }
125
126 if (ariaDescribedBy) {
127 progressBarAriaProps['aria-describedby'] = ariaDescribedBy;
128 }
129
130 if (valueText) {
131 progressBarAriaProps['aria-valuetext'] = valueText;
132 }
133
134 const scaledValue = Math.min(100, Math.max(0, Math.floor(((value - min) / (max - min)) * 100))) || 0;
135 return (
136 <div
137 {...props}
138 className={css(
139 styles.progress,
140 styles.modifiers[variant],
141 ['inside', 'outside'].includes(measureLocation) &&
142 styles.modifiers[measureLocation as 'inside' | 'outside'],
143 measureLocation === 'inside'
144 ? styles.modifiers[ProgressSize.lg]
145 : styles.modifiers[size as 'sm' | 'lg'],
146 !title && styles.modifiers.singleline,
147 className
148 )}
149 id={id}
150 >
151 <ProgressContainer
152 parentId={id}
153 value={scaledValue}
154 title={title}
155 label={label}
156 variant={variant}
157 measureLocation={measureLocation}
158 progressBarAriaProps={progressBarAriaProps}
159 isTitleTruncated={isTitleTruncated}
160 tooltipPosition={tooltipPosition}
161 helperText={helperText}
162 hideStatusIcon={hideStatusIcon}
163 />
164 </div>
165 );
166 }}
167 </GenerateId>
168 );
169 }
170 }
171
172 export { Progress };