UNPKG

6.29 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _values = require('babel-runtime/core-js/object/values');
6
7var _values2 = _interopRequireDefault(_values);
8
9var _extends3 = require('babel-runtime/helpers/extends');
10
11var _extends4 = _interopRequireDefault(_extends3);
12
13var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
14
15var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
16
17var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
18
19var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
20
21var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
22
23var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
24
25var _inherits2 = require('babel-runtime/helpers/inherits');
26
27var _inherits3 = _interopRequireDefault(_inherits2);
28
29var _classnames = require('classnames');
30
31var _classnames2 = _interopRequireDefault(_classnames);
32
33var _react = require('react');
34
35var _react2 = _interopRequireDefault(_react);
36
37var _propTypes = require('prop-types');
38
39var _propTypes2 = _interopRequireDefault(_propTypes);
40
41var _bootstrapUtils = require('./utils/bootstrapUtils');
42
43var _StyleConfig = require('./utils/StyleConfig');
44
45var _ValidComponentChildren = require('./utils/ValidComponentChildren');
46
47var _ValidComponentChildren2 = _interopRequireDefault(_ValidComponentChildren);
48
49function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
50
51var ROUND_PRECISION = 1000;
52
53/**
54 * Validate that children, if any, are instances of `<ProgressBar>`.
55 */
56function 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 // eslint-disable-next-line no-use-before-define
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
79var 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 * @private
91 */
92 isChild: _propTypes2.default.bool
93};
94
95var defaultProps = {
96 min: 0,
97 max: 100,
98 active: false,
99 isChild: false,
100 srOnly: false,
101 striped: false
102};
103
104function getPercentage(now, min, max) {
105 var percentage = (now - min) / (max - min) * 100;
106 return Math.round(percentage * ROUND_PRECISION) / ROUND_PRECISION;
107}
108
109var 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
203ProgressBar.propTypes = propTypes;
204ProgressBar.defaultProps = defaultProps;
205
206exports.default = (0, _bootstrapUtils.bsClass)('progress-bar', (0, _bootstrapUtils.bsStyles)((0, _values2.default)(_StyleConfig.State), ProgressBar));
207module.exports = exports['default'];
\No newline at end of file