1 | import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
|
2 | import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
|
3 | import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
|
4 | import _assertThisInitialized from "@babel/runtime-corejs2/helpers/esm/assertThisInitialized";
|
5 | import classNames from 'classnames';
|
6 | import React from 'react';
|
7 | import PropTypes from 'prop-types';
|
8 | import ReactDOM from 'react-dom';
|
9 | import transition from 'dom-helpers/transition';
|
10 | var propTypes = {
|
11 | direction: PropTypes.oneOf(['prev', 'next']),
|
12 | onAnimateOutEnd: PropTypes.func,
|
13 | active: PropTypes.bool,
|
14 | animateIn: PropTypes.bool,
|
15 | animateOut: PropTypes.bool,
|
16 | index: PropTypes.number
|
17 | };
|
18 | var defaultProps = {
|
19 | active: false,
|
20 | animateIn: false,
|
21 | animateOut: false
|
22 | };
|
23 |
|
24 | var CarouselItem =
|
25 |
|
26 | function (_React$Component) {
|
27 | _inheritsLoose(CarouselItem, _React$Component);
|
28 |
|
29 | function CarouselItem(props, context) {
|
30 | var _this;
|
31 |
|
32 | _this = _React$Component.call(this, props, context) || this;
|
33 | _this.handleAnimateOutEnd = _this.handleAnimateOutEnd.bind(_assertThisInitialized(_assertThisInitialized(_this)));
|
34 | _this.state = {
|
35 | direction: null
|
36 | };
|
37 | _this.isUnmounted = false;
|
38 | return _this;
|
39 | }
|
40 |
|
41 | var _proto = CarouselItem.prototype;
|
42 |
|
43 | _proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
44 | if (this.props.active !== nextProps.active) {
|
45 | this.setState({
|
46 | direction: null
|
47 | });
|
48 | }
|
49 | };
|
50 |
|
51 | _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
|
52 | var _this2 = this;
|
53 |
|
54 | var active = this.props.active;
|
55 | var prevActive = prevProps.active;
|
56 |
|
57 | if (!active && prevActive) {
|
58 | transition.end(ReactDOM.findDOMNode(this), this.handleAnimateOutEnd);
|
59 | }
|
60 |
|
61 | if (active !== prevActive) {
|
62 | setTimeout(function () {
|
63 | return _this2.startAnimation();
|
64 | }, 20);
|
65 | }
|
66 | };
|
67 |
|
68 | _proto.componentWillUnmount = function componentWillUnmount() {
|
69 | this.isUnmounted = true;
|
70 | };
|
71 |
|
72 | _proto.handleAnimateOutEnd = function handleAnimateOutEnd() {
|
73 | if (this.isUnmounted) {
|
74 | return;
|
75 | }
|
76 |
|
77 | if (this.props.onAnimateOutEnd) {
|
78 | this.props.onAnimateOutEnd(this.props.index);
|
79 | }
|
80 | };
|
81 |
|
82 | _proto.startAnimation = function startAnimation() {
|
83 | if (this.isUnmounted) {
|
84 | return;
|
85 | }
|
86 |
|
87 | this.setState({
|
88 | direction: this.props.direction === 'prev' ? 'right' : 'left'
|
89 | });
|
90 | };
|
91 |
|
92 | _proto.render = function render() {
|
93 | var _this$props = this.props,
|
94 | direction = _this$props.direction,
|
95 | active = _this$props.active,
|
96 | animateIn = _this$props.animateIn,
|
97 | animateOut = _this$props.animateOut,
|
98 | className = _this$props.className,
|
99 | props = _objectWithoutPropertiesLoose(_this$props, ["direction", "active", "animateIn", "animateOut", "className"]);
|
100 |
|
101 | delete props.onAnimateOutEnd;
|
102 | delete props.index;
|
103 | var classes = {
|
104 | item: true,
|
105 | active: active && !animateIn || animateOut
|
106 | };
|
107 |
|
108 | if (direction && active && animateIn) {
|
109 | classes[direction] = true;
|
110 | }
|
111 |
|
112 | if (this.state.direction && (animateIn || animateOut)) {
|
113 | classes[this.state.direction] = true;
|
114 | }
|
115 |
|
116 | return React.createElement("div", _extends({}, props, {
|
117 | className: classNames(className, classes)
|
118 | }));
|
119 | };
|
120 |
|
121 | return CarouselItem;
|
122 | }(React.Component);
|
123 |
|
124 | CarouselItem.propTypes = propTypes;
|
125 | CarouselItem.defaultProps = defaultProps;
|
126 | export default CarouselItem; |
\ | No newline at end of file |