1 | import _extends from "@babel/runtime/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
3 | import classNames from 'classnames';
|
4 | import React, { cloneElement } from 'react';
|
5 | import { useBootstrapPrefix } from './ThemeProvider';
|
6 | import { map } from './ElementChildren';
|
7 | var ROUND_PRECISION = 1000;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function onlyProgressBar(props, propName, componentName) {
|
13 | var children = props[propName];
|
14 |
|
15 | if (!children) {
|
16 | return null;
|
17 | }
|
18 |
|
19 | var error = null;
|
20 | React.Children.forEach(children, function (child) {
|
21 | if (error) {
|
22 | return;
|
23 | }
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var element = React.createElement(ProgressBar, null);
|
33 | if (child.type === element.type) return;
|
34 | var childType = child.type;
|
35 | var childIdentifier = React.isValidElement(child) ? childType.displayName || childType.name || childType : child;
|
36 | error = new Error("Children of " + componentName + " can contain only ProgressBar " + ("components. Found " + childIdentifier + "."));
|
37 | });
|
38 | return error;
|
39 | }
|
40 |
|
41 | var defaultProps = {
|
42 | min: 0,
|
43 | max: 100,
|
44 | animated: false,
|
45 | isChild: false,
|
46 | srOnly: false,
|
47 | striped: false
|
48 | };
|
49 |
|
50 | function getPercentage(now, min, max) {
|
51 | var percentage = (now - min) / (max - min) * 100;
|
52 | return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
|
53 | }
|
54 |
|
55 | function renderProgressBar(_ref, ref) {
|
56 | var _classNames;
|
57 |
|
58 | var min = _ref.min,
|
59 | now = _ref.now,
|
60 | max = _ref.max,
|
61 | label = _ref.label,
|
62 | srOnly = _ref.srOnly,
|
63 | striped = _ref.striped,
|
64 | animated = _ref.animated,
|
65 | className = _ref.className,
|
66 | style = _ref.style,
|
67 | variant = _ref.variant,
|
68 | bsPrefix = _ref.bsPrefix,
|
69 | props = _objectWithoutPropertiesLoose(_ref, ["min", "now", "max", "label", "srOnly", "striped", "animated", "className", "style", "variant", "bsPrefix"]);
|
70 |
|
71 | return React.createElement("div", _extends({
|
72 | ref: ref
|
73 | }, props, {
|
74 | role: "progressbar",
|
75 | className: classNames(className, bsPrefix + "-bar", (_classNames = {}, _classNames["bg-" + variant] = variant, _classNames[bsPrefix + "-bar-animated"] = animated, _classNames[bsPrefix + "-bar-striped"] = animated || striped, _classNames)),
|
76 | style: _extends({
|
77 | width: getPercentage(now, min, max) + "%"
|
78 | }, style),
|
79 | "aria-valuenow": now,
|
80 | "aria-valuemin": min,
|
81 | "aria-valuemax": max
|
82 | }), srOnly ? React.createElement("span", {
|
83 | className: "sr-only"
|
84 | }, label) : label);
|
85 | }
|
86 |
|
87 | var ProgressBar = React.forwardRef(function (_ref2, ref) {
|
88 | var isChild = _ref2.isChild,
|
89 | props = _objectWithoutPropertiesLoose(_ref2, ["isChild"]);
|
90 |
|
91 | props.bsPrefix = useBootstrapPrefix(props.bsPrefix, 'progress');
|
92 |
|
93 | if (isChild) {
|
94 | return renderProgressBar(props, ref);
|
95 | }
|
96 |
|
97 | var min = props.min,
|
98 | now = props.now,
|
99 | max = props.max,
|
100 | label = props.label,
|
101 | srOnly = props.srOnly,
|
102 | striped = props.striped,
|
103 | animated = props.animated,
|
104 | bsPrefix = props.bsPrefix,
|
105 | variant = props.variant,
|
106 | className = props.className,
|
107 | children = props.children,
|
108 | wrapperProps = _objectWithoutPropertiesLoose(props, ["min", "now", "max", "label", "srOnly", "striped", "animated", "bsPrefix", "variant", "className", "children"]);
|
109 |
|
110 | return React.createElement("div", _extends({
|
111 | ref: ref
|
112 | }, wrapperProps, {
|
113 | className: classNames(className, bsPrefix)
|
114 | }), children ? map(children, function (child) {
|
115 | return cloneElement(child, {
|
116 | isChild: true
|
117 | });
|
118 | }) : renderProgressBar({
|
119 | min: min,
|
120 | now: now,
|
121 | max: max,
|
122 | label: label,
|
123 | srOnly: srOnly,
|
124 | striped: striped,
|
125 | animated: animated,
|
126 | bsPrefix: bsPrefix,
|
127 | variant: variant
|
128 | }, ref));
|
129 | });
|
130 | ProgressBar.displayName = 'ProgressBar';
|
131 | ProgressBar.defaultProps = defaultProps;
|
132 | export default ProgressBar; |
\ | No newline at end of file |