123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- import _extends from "@babel/runtime/helpers/esm/extends";
- import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
- import classNames from 'classnames';
- import React, { cloneElement } from 'react';
- import { useBootstrapPrefix } from './ThemeProvider';
- import { map } from './utils/ElementChildren';
- var ROUND_PRECISION = 1000;
- /**
- * Validate that children, if any, are instances of `<ProgressBar>`.
- */
-
- function onlyProgressBar(props, propName, componentName) {
- var children = props[propName];
-
- if (!children) {
- return null;
- }
-
- var error = null;
- React.Children.forEach(children, function (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
- */
-
-
- var element = React.createElement(ProgressBar, null);
- if (child.type === element.type) return;
- var childIdentifier = React.isValidElement(child) ? child.type.displayName || child.type.name || child.type : child;
- error = new Error("Children of " + componentName + " can contain only ProgressBar " + ("components. Found " + childIdentifier + "."));
- });
- return error;
- }
-
- var defaultProps = {
- min: 0,
- max: 100,
- animated: false,
- isChild: false,
- srOnly: false,
- striped: false
- };
-
- function getPercentage(now, min, max) {
- var percentage = (now - min) / (max - min) * 100;
- return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
- }
-
- function renderProgressBar(_ref, ref) {
- var _classNames;
-
- var min = _ref.min,
- now = _ref.now,
- max = _ref.max,
- label = _ref.label,
- srOnly = _ref.srOnly,
- striped = _ref.striped,
- animated = _ref.animated,
- className = _ref.className,
- style = _ref.style,
- variant = _ref.variant,
- bsPrefix = _ref.bsPrefix,
- props = _objectWithoutPropertiesLoose(_ref, ["min", "now", "max", "label", "srOnly", "striped", "animated", "className", "style", "variant", "bsPrefix"]);
-
- return React.createElement("div", _extends({
- ref: ref
- }, props, {
- role: "progressbar",
- className: classNames(className, bsPrefix + "-bar", (_classNames = {}, _classNames["bg-" + variant] = variant, _classNames[bsPrefix + "-bar-animated"] = animated, _classNames[bsPrefix + "-bar-striped"] = animated || striped, _classNames)),
- style: _extends({
- width: getPercentage(now, min, max) + "%"
- }, style),
- "aria-valuenow": now,
- "aria-valuemin": min,
- "aria-valuemax": max
- }), srOnly ? React.createElement("span", {
- className: "sr-only"
- }, label) : label);
- }
-
- var ProgressBar = React.forwardRef(function (_ref2, ref) {
- var isChild = _ref2.isChild,
- props = _objectWithoutPropertiesLoose(_ref2, ["isChild"]);
-
- props.bsPrefix = useBootstrapPrefix(props.bsPrefix, 'progress');
-
- if (isChild) {
- return renderProgressBar(props, ref);
- }
-
- var min = props.min,
- now = props.now,
- max = props.max,
- label = props.label,
- srOnly = props.srOnly,
- striped = props.striped,
- animated = props.animated,
- bsPrefix = props.bsPrefix,
- variant = props.variant,
- className = props.className,
- children = props.children,
- wrapperProps = _objectWithoutPropertiesLoose(props, ["min", "now", "max", "label", "srOnly", "striped", "animated", "bsPrefix", "variant", "className", "children"]);
-
- return React.createElement("div", _extends({
- ref: ref
- }, wrapperProps, {
- className: classNames(className, bsPrefix)
- }), children ? map(children, function (child) {
- return cloneElement(child, {
- isChild: true
- });
- }) : renderProgressBar({
- min: min,
- now: now,
- max: max,
- label: label,
- srOnly: srOnly,
- striped: striped,
- animated: animated,
- bsPrefix: bsPrefix,
- variant: variant
- }, ref));
- });
- ProgressBar.displayName = 'ProgressBar';
- ProgressBar.defaultProps = defaultProps;
- export default ProgressBar;
|