137 lines
4.3 KiB
JavaScript
Executable File
137 lines
4.3 KiB
JavaScript
Executable File
"use strict";
|
|
"use client";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
exports.__esModule = true;
|
|
exports.default = void 0;
|
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
var _react = _interopRequireWildcard(require("react"));
|
|
var React = _react;
|
|
var _ThemeProvider = require("./ThemeProvider");
|
|
var _ElementChildren = require("./ElementChildren");
|
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
const ROUND_PRECISION = 1000;
|
|
|
|
/**
|
|
* Validate that children, if any, are instances of `ProgressBar`.
|
|
*/
|
|
function onlyProgressBar(props, propName, componentName) {
|
|
const children = props[propName];
|
|
if (!children) {
|
|
return null;
|
|
}
|
|
let error = null;
|
|
React.Children.forEach(children, child => {
|
|
if (error) {
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Compare types in a way that works with libraries that patch and proxy
|
|
* components like react-hot-loader.
|
|
*
|
|
* see https://github.com/gaearon/react-hot-loader#checking-element-types
|
|
*/
|
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
const element = /*#__PURE__*/(0, _jsxRuntime.jsx)(ProgressBar, {});
|
|
if (child.type === element.type) return;
|
|
const childType = child.type;
|
|
const childIdentifier = /*#__PURE__*/React.isValidElement(child) ? childType.displayName || childType.name || childType : child;
|
|
error = new Error(`Children of ${componentName} can contain only ProgressBar ` + `components. Found ${childIdentifier}.`);
|
|
});
|
|
return error;
|
|
}
|
|
function getPercentage(now, min, max) {
|
|
const percentage = (now - min) / (max - min) * 100;
|
|
return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
|
|
}
|
|
function renderProgressBar({
|
|
min,
|
|
now,
|
|
max,
|
|
label,
|
|
visuallyHidden,
|
|
striped,
|
|
animated,
|
|
className,
|
|
style,
|
|
variant,
|
|
bsPrefix,
|
|
...props
|
|
}, ref) {
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
ref: ref,
|
|
...props,
|
|
role: "progressbar",
|
|
className: (0, _classnames.default)(className, `${bsPrefix}-bar`, {
|
|
[`bg-${variant}`]: variant,
|
|
[`${bsPrefix}-bar-animated`]: animated,
|
|
[`${bsPrefix}-bar-striped`]: animated || striped
|
|
}),
|
|
style: {
|
|
width: `${getPercentage(now, min, max)}%`,
|
|
...style
|
|
},
|
|
"aria-valuenow": now,
|
|
"aria-valuemin": min,
|
|
"aria-valuemax": max,
|
|
children: visuallyHidden ? /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
|
|
className: "visually-hidden",
|
|
children: label
|
|
}) : label
|
|
});
|
|
}
|
|
const ProgressBar = /*#__PURE__*/React.forwardRef(({
|
|
isChild = false,
|
|
...rest
|
|
}, ref) => {
|
|
const props = {
|
|
min: 0,
|
|
max: 100,
|
|
animated: false,
|
|
visuallyHidden: false,
|
|
striped: false,
|
|
...rest
|
|
};
|
|
props.bsPrefix = (0, _ThemeProvider.useBootstrapPrefix)(props.bsPrefix, 'progress');
|
|
if (isChild) {
|
|
return renderProgressBar(props, ref);
|
|
}
|
|
const {
|
|
min,
|
|
now,
|
|
max,
|
|
label,
|
|
visuallyHidden,
|
|
striped,
|
|
animated,
|
|
bsPrefix,
|
|
variant,
|
|
className,
|
|
children,
|
|
...wrapperProps
|
|
} = props;
|
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
|
|
ref: ref,
|
|
...wrapperProps,
|
|
className: (0, _classnames.default)(className, bsPrefix),
|
|
children: children ? (0, _ElementChildren.map)(children, child => /*#__PURE__*/(0, _react.cloneElement)(child, {
|
|
isChild: true
|
|
})) : renderProgressBar({
|
|
min,
|
|
now,
|
|
max,
|
|
label,
|
|
visuallyHidden,
|
|
striped,
|
|
animated,
|
|
bsPrefix,
|
|
variant
|
|
}, ref)
|
|
});
|
|
});
|
|
ProgressBar.displayName = 'ProgressBar';
|
|
var _default = exports.default = ProgressBar;
|
|
module.exports = exports.default; |