You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LinearProgress.js 7.0 KiB

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