UNPKG

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