Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
  7. import React, { PureComponent } from 'react';
  8. import PropTypes from 'prop-types';
  9. import { interpolate } from 'd3-interpolate';
  10. import { spring, Motion, presets } from 'react-motion';
  11. var ANIMATION_PROPTYPES = PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
  12. stiffness: PropTypes.number,
  13. nonAnimatedProps: PropTypes.arrayOf(PropTypes.string),
  14. damping: PropTypes.number
  15. }), PropTypes.bool]);
  16. var propTypes = {
  17. animatedProps: PropTypes.arrayOf(PropTypes.string).isRequired,
  18. animation: ANIMATION_PROPTYPES,
  19. onStart: PropTypes.func,
  20. onEnd: PropTypes.func
  21. };
  22. /**
  23. * Format the animation style object
  24. * @param {Object|String} animationStyle - The animation style property, either the name of a
  25. * presets are one of noWobble, gentle, wobbly, stiff
  26. */
  27. function getAnimationStyle() {
  28. var animationStyle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : presets.noWobble;
  29. if (typeof animationStyle === 'string') {
  30. return presets[animationStyle] || presets.noWobble;
  31. }
  32. var damping = animationStyle.damping,
  33. stiffness = animationStyle.stiffness;
  34. return _extends({
  35. damping: damping || presets.noWobble.damping,
  36. stiffness: stiffness || presets.noWobble.stiffness
  37. }, animationStyle);
  38. }
  39. /**
  40. * Extract the animated props from the entire props object.
  41. * @param {Object} props Props.
  42. * @returns {Object} Object of animated props.
  43. */
  44. export function extractAnimatedPropValues(props) {
  45. var animatedProps = props.animatedProps,
  46. otherProps = _objectWithoutProperties(props, ['animatedProps']);
  47. return animatedProps.reduce(function (result, animatedPropName) {
  48. if (otherProps.hasOwnProperty(animatedPropName)) {
  49. result[animatedPropName] = otherProps[animatedPropName];
  50. }
  51. return result;
  52. }, {});
  53. }
  54. var Animation = function (_PureComponent) {
  55. _inherits(Animation, _PureComponent);
  56. function Animation(props) {
  57. _classCallCheck(this, Animation);
  58. var _this = _possibleConstructorReturn(this, (Animation.__proto__ || Object.getPrototypeOf(Animation)).call(this, props));
  59. _this._motionEndHandler = function () {
  60. if (_this.props.onEnd) {
  61. _this.props.onEnd();
  62. }
  63. };
  64. _this._renderChildren = function (_ref) {
  65. var i = _ref.i;
  66. var children = _this.props.children;
  67. var interpolator = _this._interpolator;
  68. var child = React.Children.only(children);
  69. var interpolatedProps = interpolator ? interpolator(i) : interpolator;
  70. // interpolator doesnt play nice with deeply nested objected
  71. // so we expose an additional prop for situations like these, soit _data,
  72. // which stores the full tree and can be recombined with the sanitized version
  73. // after interpolation
  74. var data = interpolatedProps && interpolatedProps.data || null;
  75. if (data && child.props._data) {
  76. data = data.map(function (row, index) {
  77. var correspondingCell = child.props._data[index];
  78. return _extends({}, row, {
  79. parent: correspondingCell.parent,
  80. children: correspondingCell.children
  81. });
  82. });
  83. }
  84. return React.cloneElement(child, _extends({}, child.props, interpolatedProps, {
  85. data: data || child.props.data || null,
  86. // enforce re-rendering
  87. _animation: Math.random()
  88. }));
  89. };
  90. _this._updateInterpolator(props);
  91. return _this;
  92. }
  93. _createClass(Animation, [{
  94. key: 'componentWillUpdate',
  95. value: function componentWillUpdate(props) {
  96. this._updateInterpolator(this.props, props);
  97. if (props.onStart) {
  98. props.onStart();
  99. }
  100. }
  101. /**
  102. * Render the child into the parent.
  103. * @param {Number} i Number generated by the spring.
  104. * @returns {React.Component} Rendered react element.
  105. * @private
  106. */
  107. }, {
  108. key: '_updateInterpolator',
  109. /**
  110. * Update the interpolator function and assign it to this._interpolator.
  111. * @param {Object} oldProps Old props.
  112. * @param {Object} newProps New props.
  113. * @private
  114. */
  115. value: function _updateInterpolator(oldProps, newProps) {
  116. this._interpolator = interpolate(extractAnimatedPropValues(oldProps), newProps ? extractAnimatedPropValues(newProps) : null);
  117. }
  118. }, {
  119. key: 'render',
  120. value: function render() {
  121. var animationStyle = getAnimationStyle(this.props.animation);
  122. var defaultStyle = { i: 0 };
  123. var style = { i: spring(1, animationStyle) };
  124. // In order to make Motion re-run animations each time, the random key is
  125. // always passed.
  126. // TODO: find a better solution for the spring.
  127. var key = Math.random();
  128. return React.createElement(
  129. Motion,
  130. _extends({ defaultStyle: defaultStyle, style: style, key: key }, { onRest: this._motionEndHandler }),
  131. this._renderChildren
  132. );
  133. }
  134. }]);
  135. return Animation;
  136. }(PureComponent);
  137. Animation.propTypes = propTypes;
  138. Animation.displayName = 'Animation';
  139. export default Animation;
  140. export var AnimationPropType = ANIMATION_PROPTYPES;