UNPKG

7.17 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends2 = require('babel-runtime/helpers/extends');
8
9var _extends3 = _interopRequireDefault(_extends2);
10
11var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
12
13var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
14
15var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
16
17var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
18
19var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
20
21var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
22
23var _createClass2 = require('babel-runtime/helpers/createClass');
24
25var _createClass3 = _interopRequireDefault(_createClass2);
26
27var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
28
29var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
30
31var _inherits2 = require('babel-runtime/helpers/inherits');
32
33var _inherits3 = _interopRequireDefault(_inherits2);
34
35var _simpleAssign = require('simple-assign');
36
37var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
38
39var _react = require('react');
40
41var _react2 = _interopRequireDefault(_react);
42
43var _propTypes = require('prop-types');
44
45var _propTypes2 = _interopRequireDefault(_propTypes);
46
47var _transitions = require('../styles/transitions');
48
49var _transitions2 = _interopRequireDefault(_transitions);
50
51function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
52
53function getRelativeValue(value, min, max) {
54 var clampedValue = Math.min(Math.max(min, value), max);
55 var rangeValue = max - min;
56 var relValue = Math.round((clampedValue - min) / rangeValue * 10000) / 10000;
57 return relValue * 100;
58}
59
60function getStyles(props, context) {
61 var max = props.max,
62 min = props.min,
63 value = props.value;
64 var _context$muiTheme = context.muiTheme,
65 palette = _context$muiTheme.baseTheme.palette,
66 borderRadius = _context$muiTheme.borderRadius;
67
68
69 var styles = {
70 root: {
71 position: 'relative',
72 height: 4,
73 display: 'block',
74 width: '100%',
75 backgroundColor: palette.primary3Color,
76 borderRadius: borderRadius,
77 margin: 0,
78 overflow: 'hidden'
79 },
80 bar: {
81 height: '100%'
82 },
83 barFragment1: {},
84 barFragment2: {}
85 };
86
87 if (props.mode === 'indeterminate') {
88 styles.barFragment1 = {
89 position: 'absolute',
90 backgroundColor: props.color || palette.primary1Color,
91 top: 0,
92 left: 0,
93 bottom: 0,
94 transition: _transitions2.default.create('all', '840ms', null, 'cubic-bezier(0.650, 0.815, 0.735, 0.395)')
95 };
96
97 styles.barFragment2 = {
98 position: 'absolute',
99 backgroundColor: props.color || palette.primary1Color,
100 top: 0,
101 left: 0,
102 bottom: 0,
103 transition: _transitions2.default.create('all', '840ms', null, 'cubic-bezier(0.165, 0.840, 0.440, 1.000)')
104 };
105 } else {
106 styles.bar.backgroundColor = props.color || palette.primary1Color;
107 styles.bar.transition = _transitions2.default.create('width', '.3s', null, 'linear');
108 styles.bar.width = getRelativeValue(value, min, max) + '%';
109 }
110
111 return styles;
112}
113
114var LinearProgress = function (_Component) {
115 (0, _inherits3.default)(LinearProgress, _Component);
116
117 function LinearProgress() {
118 (0, _classCallCheck3.default)(this, LinearProgress);
119 return (0, _possibleConstructorReturn3.default)(this, (LinearProgress.__proto__ || (0, _getPrototypeOf2.default)(LinearProgress)).apply(this, arguments));
120 }
121
122 (0, _createClass3.default)(LinearProgress, [{
123 key: 'componentDidMount',
124 value: function componentDidMount() {
125 var _this2 = this;
126
127 this.timers = {};
128
129 this.timers.bar1 = this.barUpdate('bar1', 0, this.refs.bar1, [[-35, 100], [100, -90]], 0);
130
131 this.timers.bar2 = setTimeout(function () {
132 _this2.barUpdate('bar2', 0, _this2.refs.bar2, [[-200, 100], [107, -8]], 0);
133 }, 850);
134 }
135 }, {
136 key: 'componentWillUnmount',
137 value: function componentWillUnmount() {
138 clearTimeout(this.timers.bar1);
139 clearTimeout(this.timers.bar2);
140 }
141 }, {
142 key: 'barUpdate',
143 value: function barUpdate(id, step, barElement, stepValues, timeToNextStep) {
144 var _this3 = this;
145
146 if (this.props.mode !== 'indeterminate') return;
147
148 timeToNextStep = timeToNextStep || 420;
149 step = step || 0;
150 step %= 4;
151
152 var right = this.context.muiTheme.isRtl ? 'left' : 'right';
153 var left = this.context.muiTheme.isRtl ? 'right' : 'left';
154
155 if (step === 0) {
156 barElement.style[left] = stepValues[0][0] + '%';
157 barElement.style[right] = stepValues[0][1] + '%';
158 } else if (step === 1) {
159 barElement.style.transitionDuration = '840ms';
160 } else if (step === 2) {
161 barElement.style[left] = stepValues[1][0] + '%';
162 barElement.style[right] = stepValues[1][1] + '%';
163 } else if (step === 3) {
164 barElement.style.transitionDuration = '0ms';
165 }
166 this.timers[id] = setTimeout(function () {
167 return _this3.barUpdate(id, step + 1, barElement, stepValues);
168 }, timeToNextStep);
169 }
170 }, {
171 key: 'render',
172 value: function render() {
173 var _props = this.props,
174 style = _props.style,
175 other = (0, _objectWithoutProperties3.default)(_props, ['style']);
176 var prepareStyles = this.context.muiTheme.prepareStyles;
177
178 var styles = getStyles(this.props, this.context);
179
180 return _react2.default.createElement(
181 'div',
182 (0, _extends3.default)({}, other, { style: prepareStyles((0, _simpleAssign2.default)(styles.root, style)) }),
183 _react2.default.createElement(
184 'div',
185 { style: prepareStyles(styles.bar) },
186 _react2.default.createElement('div', { ref: 'bar1', style: prepareStyles(styles.barFragment1) }),
187 _react2.default.createElement('div', { ref: 'bar2', style: prepareStyles(styles.barFragment2) })
188 )
189 );
190 }
191 }]);
192 return LinearProgress;
193}(_react.Component);
194
195LinearProgress.defaultProps = {
196 mode: 'indeterminate',
197 value: 0,
198 min: 0,
199 max: 100
200};
201LinearProgress.contextTypes = {
202 muiTheme: _propTypes2.default.object.isRequired
203};
204LinearProgress.propTypes = process.env.NODE_ENV !== "production" ? {
205 /**
206 * The color of the progress bar, defaults to
207 * primary color of theme.
208 */
209 color: _propTypes2.default.string,
210 /**
211 * The max value of progress, only works in determinate mode.
212 */
213 max: _propTypes2.default.number,
214 /**
215 * The min value of progress, only works in determinate mode.
216 */
217 min: _propTypes2.default.number,
218 /**
219 * The mode of show your progress, indeterminate for when
220 * there is no value for progress.
221 */
222 mode: _propTypes2.default.oneOf(['determinate', 'indeterminate']),
223 /**
224 * Override the inline-styles of the root element.
225 */
226 style: _propTypes2.default.object,
227 /**
228 * The value of progress, only works in determinate mode.
229 */
230 value: _propTypes2.default.number
231} : {};
232exports.default = LinearProgress;
\No newline at end of file