UNPKG

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