UNPKG

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