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