UNPKG

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