UNPKG

4.17 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import classNames from 'classnames';
4import React, { cloneElement } from 'react';
5import { useBootstrapPrefix } from './ThemeProvider';
6import { map } from './ElementChildren';
7var ROUND_PRECISION = 1000;
8/**
9 * Validate that children, if any, are instances of `<ProgressBar>`.
10 */
11
12function 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 * Compare types in a way that works with libraries that patch and proxy
26 * components like react-hot-loader.
27 *
28 * see https://github.com/gaearon/react-hot-loader#checking-element-types
29 */
30
31
32 var element = /*#__PURE__*/React.createElement(ProgressBar, null);
33 if (child.type === element.type) return;
34 var childType = child.type;
35 var childIdentifier = /*#__PURE__*/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
41var defaultProps = {
42 min: 0,
43 max: 100,
44 animated: false,
45 isChild: false,
46 srOnly: false,
47 striped: false
48};
49
50function getPercentage(now, min, max) {
51 var percentage = (now - min) / (max - min) * 100;
52 return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
53}
54
55function 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 /*#__PURE__*/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 ? /*#__PURE__*/React.createElement("span", {
83 className: "sr-only"
84 }, label) : label);
85}
86
87var ProgressBar = /*#__PURE__*/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 /*#__PURE__*/React.createElement("div", _extends({
111 ref: ref
112 }, wrapperProps, {
113 className: classNames(className, bsPrefix)
114 }), children ? map(children, function (child) {
115 return /*#__PURE__*/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});
130ProgressBar.displayName = 'ProgressBar';
131ProgressBar.defaultProps = defaultProps;
132export default ProgressBar;
\No newline at end of file