UNPKG

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