zarm-mobile
Version:
UI for react.js
219 lines (180 loc) • 7.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _classnames4 = require('classnames');
var _classnames5 = _interopRequireDefault(_classnames4);
var _events = require('../utils/events');
var _events2 = _interopRequireDefault(_events);
var _Mask = require('../Mask');
var _Mask2 = _interopRequireDefault(_Mask);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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; }
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; }
var Popup = function (_PureComponent) {
_inherits(Popup, _PureComponent);
function Popup(props) {
_classCallCheck(this, Popup);
var _this = _possibleConstructorReturn(this, (Popup.__proto__ || Object.getPrototypeOf(Popup)).call(this, props));
_this.timer = null;
_this.state = {
isShow: _this.props.visible || false,
isMaskShow: _this.props.visible || false,
animationState: 'enter',
isPending: false
};
_this.animationEnd = _this.animationEnd.bind(_this);
return _this;
}
_createClass(Popup, [{
key: 'componentDidMount',
value: function componentDidMount() {
_events2.default.on(this.popup, 'webkitTransitionEnd', this.animationEnd);
_events2.default.on(this.popup, 'transitionend', this.animationEnd);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
clearTimeout(this.timer);
if (nextProps.visible) {
this.enter(nextProps);
} else {
this.leave();
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
_events2.default.off(this.popup, 'webkitTransitionEnd', this.animationEnd);
_events2.default.off(this.popup, 'transitionend', this.animationEnd);
}
}, {
key: 'enter',
value: function enter(_ref) {
var _this2 = this;
var duration = _ref.duration,
autoClose = _ref.autoClose,
onMaskClick = _ref.onMaskClick;
this.setState({
isShow: true,
isMaskShow: true,
isPending: true,
animationState: 'enter'
});
if (duration === 0) {
return;
}
if (autoClose) {
this.timer = setTimeout(function () {
onMaskClick();
clearTimeout(_this2.timer);
}, duration);
}
}
}, {
key: 'leave',
value: function leave() {
this.setState({
isShow: false,
isPending: true,
animationState: 'leave'
});
}
}, {
key: 'animationEnd',
value: function animationEnd() {
var onClose = this.props.onClose;
var animationState = this.state.animationState;
if (animationState === 'leave') {
this.setState({
isMaskShow: false
});
typeof onClose === 'function' && onClose();
}
}
}, {
key: 'render',
value: function render() {
var _classnames,
_classnames2,
_this3 = this;
var _props = this.props,
prefixCls = _props.prefixCls,
children = _props.children,
onMaskClick = _props.onMaskClick,
animationDuration = _props.animationDuration,
direction = _props.direction,
className = _props.className,
maskType = _props.maskType;
var _state = this.state,
isShow = _state.isShow,
isPending = _state.isPending,
animationState = _state.animationState,
isMaskShow = _state.isMaskShow;
var cls = {
popup: (0, _classnames5.default)((_classnames = {}, _defineProperty(_classnames, '' + prefixCls, true), _defineProperty(_classnames, prefixCls + '-hidden', !isShow), _defineProperty(_classnames, className, !!className), _classnames)),
wrap: (0, _classnames5.default)((_classnames2 = {}, _defineProperty(_classnames2, prefixCls + '-wrapper', true), _defineProperty(_classnames2, prefixCls + '-wrapper-' + direction, true), _classnames2)),
mask: (0, _classnames5.default)(_defineProperty({}, 'fade-' + animationState, isPending))
};
var style = {
wrap: {
transition: 'transform ' + animationDuration + 'ms'
},
mask: {
WebkitAnimationDuration: animationDuration + 'ms',
MozAnimationDuration: animationDuration + 'ms',
msAnimationDuration: animationDuration + 'ms',
OAnimationDuration: animationDuration + 'ms',
animationDuration: animationDuration + 'ms'
}
};
return _react2.default.createElement(
'div',
{ className: cls.popup, ref: function ref(popup) {
_this3.popup = popup;
} },
_react2.default.createElement(
'div',
{ className: cls.wrap, style: style.wrap },
children
),
this.props.mask && _react2.default.createElement(_Mask2.default, { className: cls.mask, style: style.mask, visible: isMaskShow, type: maskType, onClose: onMaskClick })
);
}
}]);
return Popup;
}(_react.PureComponent);
Popup.propTypes = {
prefixCls: _propTypes2.default.string,
className: _propTypes2.default.string,
visible: _propTypes2.default.bool,
mask: _propTypes2.default.bool,
direction: _propTypes2.default.oneOf(['top', 'right', 'bottom', 'left']),
duration: _propTypes2.default.number, // eslint-disable-line
autoClose: _propTypes2.default.bool, // eslint-disable-line
onClose: _propTypes2.default.func,
maskType: _Mask2.default.propTypes.type,
animationDuration: _propTypes2.default.number,
onMaskClick: _Mask2.default.propTypes.onClose
};
Popup.defaultProps = {
prefixCls: 'za-popup',
className: null,
visible: false,
mask: true,
direction: 'bottom',
duration: 3000,
autoClose: false,
onClose: function onClose() {},
animationDuration: 200,
maskType: _Mask2.default.defaultProps.type,
onMaskClick: _Mask2.default.defaultProps.onClose
};
exports.default = Popup;