UNPKG

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