-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathprogressbar.tsx
More file actions
161 lines (155 loc) · 4.69 KB
/
progressbar.tsx
File metadata and controls
161 lines (155 loc) · 4.69 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
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { getOverrides } from '../helpers/overrides';
import { SIZE } from './constants';
import {
StyledRoot,
StyledBarContainer,
StyledBar,
StyledLabel,
StyledBarProgress,
StyledInfiniteBar,
} from './styled-components';
import type { ProgressBarProps } from './types';
class ProgressBar extends React.Component<
ProgressBarProps & {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
forwardedRef: any;
}
> {
static defaultProps = {
getProgressLabel: (value: number, maxValue: number, minValue: number) =>
`${Math.round(((value - minValue) / (maxValue - minValue)) * 100)}% Complete`,
infinite: false,
overrides: {},
showLabel: false,
size: SIZE.medium,
steps: 1,
successValue: 100,
minValue: 0,
maxValue: 100,
value: 0,
};
render() {
const {
overrides = {},
getProgressLabel,
value,
size,
steps,
successValue,
minValue,
maxValue,
showLabel,
infinite,
intent,
errorMessage, // Deprecated, ignored
forwardedRef,
...restProps
} = this.props;
const propsAriaLabel = this.props['aria-label'] || this.props.ariaLabel;
const progressLabel = getProgressLabel(value, maxValue, minValue);
const stepsLabel = getStepProgressLabel(value, maxValue, minValue, steps);
const ariaLabel =
propsAriaLabel || this.props.infinite
? 'Loading'
: this.props.steps > 1
? stepsLabel
: progressLabel;
// fallback on successValue (and it's default) if maxValue is not set by user
const maximumValue = maxValue !== 100 ? maxValue : successValue;
const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
const [BarContainer, barContainerProps] = getOverrides(
overrides.BarContainer,
StyledBarContainer
);
const [Bar, barProps] = getOverrides(overrides.Bar, StyledBar);
const [BarProgress, barProgressProps] = getOverrides(overrides.BarProgress, StyledBarProgress);
const [Label, labelProps] = getOverrides(overrides.Label, StyledLabel);
const [InfiniteBar, infiniteBarProps] = getOverrides(overrides.InfiniteBar, StyledInfiniteBar);
const sharedProps = {
$infinite: infinite,
$intent: intent,
$size: size,
$steps: steps,
$successValue: maximumValue,
$minValue: minValue,
$maxValue: maximumValue,
$value: value,
};
function renderProgressBar() {
const children = [];
for (let i = 0; i < steps; i++) {
children.push(
// @ts-ignore
<Bar key={i} {...sharedProps} {...barProps}>
<BarProgress $index={i} {...sharedProps} {...barProgressProps} />
</Bar>
);
}
return children;
}
function getStepProgressLabel(
value: number,
maxValue: number,
minValue: number,
steps: number
) {
return `Step ${Math.ceil(((value - minValue) / (maxValue - minValue)) * steps)} of ${steps}`;
}
return (
<Root
ref={forwardedRef}
data-baseweb="progress-bar"
role="progressbar"
aria-label={ariaLabel}
aria-valuenow={infinite ? null : value}
aria-valuemin={infinite ? null : minValue}
aria-valuemax={infinite ? null : maximumValue}
aria-live="polite"
aria-busy={infinite ? true : null}
{...restProps}
{...sharedProps}
{...rootProps}
>
<BarContainer {...sharedProps} {...barContainerProps}>
{infinite ? (
<React.Fragment>
<InfiniteBar
$isLeft={true}
$size={sharedProps.$size}
$intent={sharedProps.$intent}
{...infiniteBarProps}
/>
<InfiniteBar
$size={sharedProps.$size}
$intent={sharedProps.$intent}
{...infiniteBarProps}
/>
</React.Fragment>
) : (
renderProgressBar()
)}
</BarContainer>
{showLabel && (
<Label {...sharedProps} {...labelProps}>
{getProgressLabel(value, maximumValue, minValue)}
</Label>
)}
</Root>
);
}
}
const ForwardedProgressBar = React.forwardRef<HTMLDivElement, Partial<ProgressBarProps>>(
// @ts-ignore
(props: ProgressBarProps, ref) => (
//$FlowExpectedError[cannot-spread-inexact]
<ProgressBar forwardedRef={ref} {...props} />
)
);
ForwardedProgressBar.displayName = 'ProgressBar';
export default ForwardedProgressBar;