UNPKG

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