forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.js
More file actions
145 lines (124 loc) · 3.21 KB
/
Copy pathProgressBar.js
File metadata and controls
145 lines (124 loc) · 3.21 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
import classNames from 'classnames';
import React, { cloneElement, PropTypes } from 'react';
import { bsClass as setBsClass, bsStyles, getClassSet, prefix, splitBsProps }
from './utils/bootstrapUtils';
import { State } from './utils/StyleConfig';
import ValidComponentChildren from './utils/ValidComponentChildren';
const ROUND_PRECISION = 1000;
/**
* Validate that children, if any, are instances of `<ProgressBar>`.
*/
function onlyProgressBar(props, propName, componentName) {
const children = props[propName];
if (!children) {
return null;
}
let error = null;
React.Children.forEach(children, child => {
if (error) {
return;
}
if (child.type === ProgressBar) { // eslint-disable-line no-use-before-define
return;
}
const childIdentifier = React.isValidElement(child) ?
child.type.displayName || child.type.name || child.type :
child;
error = new Error(
`Children of ${componentName} can contain only ProgressBar ` +
`components. Found ${childIdentifier}.`
);
});
return error;
}
const propTypes = {
min: PropTypes.number,
now: PropTypes.number,
max: PropTypes.number,
label: PropTypes.node,
srOnly: PropTypes.bool,
striped: PropTypes.bool,
active: PropTypes.bool,
children: onlyProgressBar,
/**
* @private
*/
isChild: PropTypes.bool,
};
const defaultProps = {
min: 0,
max: 100,
active: false,
isChild: false,
srOnly: false,
striped: false
};
function getPercentage(now, min, max) {
const percentage = (now - min) / (max - min) * 100;
return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
}
class ProgressBar extends React.Component {
renderProgressBar({
min, now, max, label, srOnly, striped, active, className, style, ...props
}) {
const [bsProps, elementProps] = splitBsProps(props);
const classes = {
...getClassSet(bsProps),
active,
[prefix(bsProps, 'striped')]: active || striped,
};
return (
<div
{...elementProps}
role="progressbar"
className={classNames(className, classes)}
style={{ width: `${getPercentage(now, min, max)}%`, ...style }}
aria-valuenow={now}
aria-valuemin={min}
aria-valuemax={max}
>
{srOnly ? <span className="sr-only">{label}</span> : label}
</div>
);
}
render() {
const { isChild, ...props } = this.props;
if (isChild) {
return this.renderProgressBar(props);
}
const {
min,
now,
max,
label,
srOnly,
striped,
active,
bsClass,
bsStyle,
className,
children,
...wrapperProps
} = props;
return (
<div
{...wrapperProps}
className={classNames(className, 'progress')}
>
{children ?
ValidComponentChildren.map(children, child => (
cloneElement(child, { isChild: true }
))) :
this.renderProgressBar({
min, now, max, label, srOnly, striped, active, bsClass, bsStyle,
})
}
</div>
);
}
}
ProgressBar.propTypes = propTypes;
ProgressBar.defaultProps = defaultProps;
export default setBsClass('progress-bar',
bsStyles(Object.values(State), ProgressBar)
);