1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _values = require('babel-runtime/core-js/object/values');
|
6 |
|
7 | var _values2 = _interopRequireDefault(_values);
|
8 |
|
9 | var _extends3 = require('babel-runtime/helpers/extends');
|
10 |
|
11 | var _extends4 = _interopRequireDefault(_extends3);
|
12 |
|
13 | var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
14 |
|
15 | var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
16 |
|
17 | var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
18 |
|
19 | var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
20 |
|
21 | var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
22 |
|
23 | var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
24 |
|
25 | var _inherits2 = require('babel-runtime/helpers/inherits');
|
26 |
|
27 | var _inherits3 = _interopRequireDefault(_inherits2);
|
28 |
|
29 | var _classnames = require('classnames');
|
30 |
|
31 | var _classnames2 = _interopRequireDefault(_classnames);
|
32 |
|
33 | var _react = require('react');
|
34 |
|
35 | var _react2 = _interopRequireDefault(_react);
|
36 |
|
37 | var _propTypes = require('prop-types');
|
38 |
|
39 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
40 |
|
41 | var _bootstrapUtils = require('./utils/bootstrapUtils');
|
42 |
|
43 | var _StyleConfig = require('./utils/StyleConfig');
|
44 |
|
45 | var _ValidComponentChildren = require('./utils/ValidComponentChildren');
|
46 |
|
47 | var _ValidComponentChildren2 = _interopRequireDefault(_ValidComponentChildren);
|
48 |
|
49 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
50 |
|
51 | var ROUND_PRECISION = 1000;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function onlyProgressBar(props, propName, componentName) {
|
57 | var children = props[propName];
|
58 | if (!children) {
|
59 | return null;
|
60 | }
|
61 |
|
62 | var error = null;
|
63 |
|
64 | _react2.default.Children.forEach(children, function (child) {
|
65 | if (error) {
|
66 | return;
|
67 | }
|
68 |
|
69 |
|
70 | if (child.type === ProgressBar) return;
|
71 |
|
72 | var childIdentifier = _react2.default.isValidElement(child) ? child.type.displayName || child.type.name || child.type : child;
|
73 | error = new Error('Children of ' + componentName + ' can contain only ProgressBar ' + ('components. Found ' + childIdentifier + '.'));
|
74 | });
|
75 |
|
76 | return error;
|
77 | }
|
78 |
|
79 | var propTypes = {
|
80 | min: _propTypes2.default.number,
|
81 | now: _propTypes2.default.number,
|
82 | max: _propTypes2.default.number,
|
83 | label: _propTypes2.default.node,
|
84 | srOnly: _propTypes2.default.bool,
|
85 | striped: _propTypes2.default.bool,
|
86 | active: _propTypes2.default.bool,
|
87 | children: onlyProgressBar,
|
88 |
|
89 | |
90 |
|
91 |
|
92 | isChild: _propTypes2.default.bool
|
93 | };
|
94 |
|
95 | var defaultProps = {
|
96 | min: 0,
|
97 | max: 100,
|
98 | active: false,
|
99 | isChild: false,
|
100 | srOnly: false,
|
101 | striped: false
|
102 | };
|
103 |
|
104 | function getPercentage(now, min, max) {
|
105 | var percentage = (now - min) / (max - min) * 100;
|
106 | return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
|
107 | }
|
108 |
|
109 | var ProgressBar = function (_React$Component) {
|
110 | (0, _inherits3.default)(ProgressBar, _React$Component);
|
111 |
|
112 | function ProgressBar() {
|
113 | (0, _classCallCheck3.default)(this, ProgressBar);
|
114 | return (0, _possibleConstructorReturn3.default)(this, _React$Component.apply(this, arguments));
|
115 | }
|
116 |
|
117 | ProgressBar.prototype.renderProgressBar = function renderProgressBar(_ref) {
|
118 | var _extends2;
|
119 |
|
120 | var min = _ref.min,
|
121 | now = _ref.now,
|
122 | max = _ref.max,
|
123 | label = _ref.label,
|
124 | srOnly = _ref.srOnly,
|
125 | striped = _ref.striped,
|
126 | active = _ref.active,
|
127 | className = _ref.className,
|
128 | style = _ref.style,
|
129 | props = (0, _objectWithoutProperties3.default)(_ref, ['min', 'now', 'max', 'label', 'srOnly', 'striped', 'active', 'className', 'style']);
|
130 |
|
131 | var _splitBsProps = (0, _bootstrapUtils.splitBsProps)(props),
|
132 | bsProps = _splitBsProps[0],
|
133 | elementProps = _splitBsProps[1];
|
134 |
|
135 | var classes = (0, _extends4.default)({}, (0, _bootstrapUtils.getClassSet)(bsProps), (_extends2 = {
|
136 | active: active
|
137 | }, _extends2[(0, _bootstrapUtils.prefix)(bsProps, 'striped')] = active || striped, _extends2));
|
138 |
|
139 | return _react2.default.createElement(
|
140 | 'div',
|
141 | (0, _extends4.default)({}, elementProps, {
|
142 | role: 'progressbar',
|
143 | className: (0, _classnames2.default)(className, classes),
|
144 | style: (0, _extends4.default)({ width: getPercentage(now, min, max) + '%' }, style),
|
145 | 'aria-valuenow': now,
|
146 | 'aria-valuemin': min,
|
147 | 'aria-valuemax': max
|
148 | }),
|
149 | srOnly ? _react2.default.createElement(
|
150 | 'span',
|
151 | { className: 'sr-only' },
|
152 | label
|
153 | ) : label
|
154 | );
|
155 | };
|
156 |
|
157 | ProgressBar.prototype.render = function render() {
|
158 | var _props = this.props,
|
159 | isChild = _props.isChild,
|
160 | props = (0, _objectWithoutProperties3.default)(_props, ['isChild']);
|
161 |
|
162 |
|
163 | if (isChild) {
|
164 | return this.renderProgressBar(props);
|
165 | }
|
166 |
|
167 | var min = props.min,
|
168 | now = props.now,
|
169 | max = props.max,
|
170 | label = props.label,
|
171 | srOnly = props.srOnly,
|
172 | striped = props.striped,
|
173 | active = props.active,
|
174 | bsClass = props.bsClass,
|
175 | bsStyle = props.bsStyle,
|
176 | className = props.className,
|
177 | children = props.children,
|
178 | wrapperProps = (0, _objectWithoutProperties3.default)(props, ['min', 'now', 'max', 'label', 'srOnly', 'striped', 'active', 'bsClass', 'bsStyle', 'className', 'children']);
|
179 |
|
180 |
|
181 | return _react2.default.createElement(
|
182 | 'div',
|
183 | (0, _extends4.default)({}, wrapperProps, { className: (0, _classnames2.default)(className, 'progress') }),
|
184 | children ? _ValidComponentChildren2.default.map(children, function (child) {
|
185 | return (0, _react.cloneElement)(child, { isChild: true });
|
186 | }) : this.renderProgressBar({
|
187 | min: min,
|
188 | now: now,
|
189 | max: max,
|
190 | label: label,
|
191 | srOnly: srOnly,
|
192 | striped: striped,
|
193 | active: active,
|
194 | bsClass: bsClass,
|
195 | bsStyle: bsStyle
|
196 | })
|
197 | );
|
198 | };
|
199 |
|
200 | return ProgressBar;
|
201 | }(_react2.default.Component);
|
202 |
|
203 | ProgressBar.propTypes = propTypes;
|
204 | ProgressBar.defaultProps = defaultProps;
|
205 |
|
206 | exports.default = (0, _bootstrapUtils.bsClass)('progress-bar', (0, _bootstrapUtils.bsStyles)((0, _values2.default)(_StyleConfig.State), ProgressBar));
|
207 | module.exports = exports['default']; |
\ | No newline at end of file |