ReplaceTransition.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _propTypes = _interopRequireDefault(require("prop-types"));
  5. var _react = _interopRequireDefault(require("react"));
  6. var _reactDom = _interopRequireDefault(require("react-dom"));
  7. var _TransitionGroup = _interopRequireDefault(require("./TransitionGroup"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  10. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  11. /**
  12. * The `<ReplaceTransition>` component is a specialized `Transition` component
  13. * that animates between two children.
  14. *
  15. * ```jsx
  16. * <ReplaceTransition in>
  17. * <Fade><div>I appear first</div></Fade>
  18. * <Fade><div>I replace the above</div></Fade>
  19. * </ReplaceTransition>
  20. * ```
  21. */
  22. var ReplaceTransition = /*#__PURE__*/function (_React$Component) {
  23. _inheritsLoose(ReplaceTransition, _React$Component);
  24. function ReplaceTransition() {
  25. var _this;
  26. for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
  27. _args[_key] = arguments[_key];
  28. }
  29. _this = _React$Component.call.apply(_React$Component, [this].concat(_args)) || this;
  30. _this.handleEnter = function () {
  31. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  32. args[_key2] = arguments[_key2];
  33. }
  34. return _this.handleLifecycle('onEnter', 0, args);
  35. };
  36. _this.handleEntering = function () {
  37. for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  38. args[_key3] = arguments[_key3];
  39. }
  40. return _this.handleLifecycle('onEntering', 0, args);
  41. };
  42. _this.handleEntered = function () {
  43. for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  44. args[_key4] = arguments[_key4];
  45. }
  46. return _this.handleLifecycle('onEntered', 0, args);
  47. };
  48. _this.handleExit = function () {
  49. for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
  50. args[_key5] = arguments[_key5];
  51. }
  52. return _this.handleLifecycle('onExit', 1, args);
  53. };
  54. _this.handleExiting = function () {
  55. for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
  56. args[_key6] = arguments[_key6];
  57. }
  58. return _this.handleLifecycle('onExiting', 1, args);
  59. };
  60. _this.handleExited = function () {
  61. for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
  62. args[_key7] = arguments[_key7];
  63. }
  64. return _this.handleLifecycle('onExited', 1, args);
  65. };
  66. return _this;
  67. }
  68. var _proto = ReplaceTransition.prototype;
  69. _proto.handleLifecycle = function handleLifecycle(handler, idx, originalArgs) {
  70. var _child$props;
  71. var children = this.props.children;
  72. var child = _react.default.Children.toArray(children)[idx];
  73. if (child.props[handler]) (_child$props = child.props)[handler].apply(_child$props, originalArgs);
  74. if (this.props[handler]) {
  75. var maybeNode = child.props.nodeRef ? undefined : _reactDom.default.findDOMNode(this);
  76. this.props[handler](maybeNode);
  77. }
  78. };
  79. _proto.render = function render() {
  80. var _this$props = this.props,
  81. children = _this$props.children,
  82. inProp = _this$props.in,
  83. props = _objectWithoutPropertiesLoose(_this$props, ["children", "in"]);
  84. var _React$Children$toArr = _react.default.Children.toArray(children),
  85. first = _React$Children$toArr[0],
  86. second = _React$Children$toArr[1];
  87. delete props.onEnter;
  88. delete props.onEntering;
  89. delete props.onEntered;
  90. delete props.onExit;
  91. delete props.onExiting;
  92. delete props.onExited;
  93. return /*#__PURE__*/_react.default.createElement(_TransitionGroup.default, props, inProp ? _react.default.cloneElement(first, {
  94. key: 'first',
  95. onEnter: this.handleEnter,
  96. onEntering: this.handleEntering,
  97. onEntered: this.handleEntered
  98. }) : _react.default.cloneElement(second, {
  99. key: 'second',
  100. onEnter: this.handleExit,
  101. onEntering: this.handleExiting,
  102. onEntered: this.handleExited
  103. }));
  104. };
  105. return ReplaceTransition;
  106. }(_react.default.Component);
  107. ReplaceTransition.propTypes = process.env.NODE_ENV !== "production" ? {
  108. in: _propTypes.default.bool.isRequired,
  109. children: function children(props, propName) {
  110. if (_react.default.Children.count(props[propName]) !== 2) return new Error("\"" + propName + "\" must be exactly two transition components.");
  111. return null;
  112. }
  113. } : {};
  114. var _default = ReplaceTransition;
  115. exports.default = _default;
  116. module.exports = exports.default;