UNPKG

6.03 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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; };
8
9var _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; }; }();
10
11var _react = require('react');
12
13var _react2 = _interopRequireDefault(_react);
14
15var _classnames = require('classnames');
16
17var _classnames2 = _interopRequireDefault(_classnames);
18
19var _lodash = require('lodash.omit');
20
21var _lodash2 = _interopRequireDefault(_lodash);
22
23var _utils = require('./utils');
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
28
29function _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; }
30
31function _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; }
32
33var propTypes = {
34 baseClass: _react.PropTypes.string,
35 baseClassIn: _react.PropTypes.string,
36 tag: _react.PropTypes.oneOfType([_react.PropTypes.func, _react.PropTypes.string]),
37 className: _react.PropTypes.string,
38 cssModule: _react.PropTypes.object,
39 transitionAppearTimeout: _react.PropTypes.number,
40 transitionEnterTimeout: _react.PropTypes.number,
41 transitionLeaveTimeout: _react.PropTypes.number,
42 transitionAppear: _react.PropTypes.bool,
43 transitionEnter: _react.PropTypes.bool,
44 transitionLeave: _react.PropTypes.bool,
45 onLeave: _react.PropTypes.func,
46 onEnter: _react.PropTypes.func
47};
48
49var defaultProps = {
50 tag: 'div',
51 baseClass: 'fade',
52 baseClassIn: 'show',
53 transitionAppearTimeout: 0,
54 transitionEnterTimeout: 0,
55 transitionLeaveTimeout: 0,
56 transitionAppear: true,
57 transitionEnter: true,
58 transitionLeave: true
59};
60
61var Fade = function (_React$Component) {
62 _inherits(Fade, _React$Component);
63
64 function Fade(props) {
65 _classCallCheck(this, Fade);
66
67 var _this = _possibleConstructorReturn(this, (Fade.__proto__ || Object.getPrototypeOf(Fade)).call(this, props));
68
69 _this.state = {
70 mounted: !props.transitionAppear
71 };
72
73 _this.onLeave = _this.onLeave.bind(_this);
74 _this.onEnter = _this.onEnter.bind(_this);
75 _this.timers = [];
76 return _this;
77 }
78
79 _createClass(Fade, [{
80 key: 'componentWillUnmount',
81 value: function componentWillUnmount() {
82 this.timers.forEach(function (timer) {
83 return clearTimeout(timer);
84 });
85 }
86 }, {
87 key: 'onEnter',
88 value: function onEnter(cb) {
89 var _this2 = this;
90
91 return function () {
92 cb();
93 if (_this2.props.onEnter) {
94 _this2.props.onEnter();
95 }
96 };
97 }
98 }, {
99 key: 'onLeave',
100 value: function onLeave(cb) {
101 var _this3 = this;
102
103 return function () {
104 cb();
105 if (_this3.props.onLeave) {
106 _this3.props.onLeave();
107 }
108 };
109 }
110 }, {
111 key: 'componentWillAppear',
112 value: function componentWillAppear(cb) {
113 if (!this.props.transitionAppear) {
114 this.onEnter(cb)();
115 }
116
117 this.timers.push(setTimeout(this.onEnter(cb), this.props.transitionAppearTimeout));
118 }
119 }, {
120 key: 'componentDidAppear',
121 value: function componentDidAppear() {
122 this.setState({
123 mounted: true
124 });
125 }
126 }, {
127 key: 'componentWillEnter',
128 value: function componentWillEnter(cb) {
129 if (!this.props.transitionEnter) {
130 this.onEnter(cb)();
131 }
132
133 this.timers.push(setTimeout(this.onEnter(cb), this.props.transitionEnterTimeout));
134 }
135 }, {
136 key: 'componentDidEnter',
137 value: function componentDidEnter() {
138 this.setState({
139 mounted: true
140 });
141 }
142 }, {
143 key: 'componentWillLeave',
144 value: function componentWillLeave(cb) {
145 this.setState({
146 mounted: false
147 });
148
149 if (!this.props.transitionLeave) {
150 this.onLeave(cb)();
151 }
152
153 this.timers.push(setTimeout(this.onLeave(cb), this.props.transitionLeaveTimeout));
154 }
155 }, {
156 key: 'render',
157 value: function render() {
158 var _props = this.props,
159 baseClass = _props.baseClass,
160 baseClassIn = _props.baseClassIn,
161 className = _props.className,
162 cssModule = _props.cssModule,
163 Tag = _props.tag;
164
165 var attributes = (0, _lodash2.default)(this.props, Object.keys(propTypes));
166
167 var classes = (0, _utils.mapToCssModules)((0, _classnames2.default)(className, baseClass, this.state.mounted ? baseClassIn : false), cssModule);
168
169 return _react2.default.createElement(Tag, _extends({}, attributes, { className: classes }));
170 }
171 }]);
172
173 return Fade;
174}(_react2.default.Component);
175
176Fade.propTypes = propTypes;
177Fade.defaultProps = defaultProps;
178
179exports.default = Fade;
\No newline at end of file