react-zoom-children
Version:
react-zoom-children for react
170 lines (133 loc) • 7.58 kB
JavaScript
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
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 _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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
import React, { Component } from 'react';
import Target from './target';
import { listen } from './utils';
var Child =
/*#__PURE__*/
function (_Component) {
_inherits(Child, _Component);
function Child(props) {
var _this;
_classCallCheck(this, Child);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Child).call(this, props));
_this.zoomIn = function (dom) {
var cb = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _this.options.onOpen;
if (_this.contentRef && dom.children.length !== 0) {
_this.target = new Target(dom.children[0], _assertThisInitialized(_this));
if (typeof _this.options.onBeforeClose === 'function') _this.options.onBeforeOpen(_this.contentRef);
_this.target.zoomIn();
var onOpenEnd = function onOpenEnd() {
listen(_this.contentRef, 'transitionend', onOpenEnd, false);
cb(_this.contentRef);
};
listen(_this.contentRef, 'transitionend', onOpenEnd);
}
};
_this.zoomOut = function () {
var cb = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.options.onClose;
_this.target.zoomOut();
if (_this.contentRef && typeof _this.options.onBeforeClose === 'function') _this.options.onBeforeClose(_this.contentRef);
if (_this.contentRef) {
var onCloseEnd = function onCloseEnd() {
listen(_this.contentRef, 'transitionend', onCloseEnd, false);
cb(_this.contentRef);
};
listen(_this.contentRef, 'transitionend', onCloseEnd);
}
_this.timer = setTimeout(function () {
if (typeof _this.props.onClose === 'function') _this.props.onClose();
}, 300);
};
_this.state = {
_self: _assertThisInitialized(_this)
};
return _this;
}
_createClass(Child, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this$props = this.props,
open = _this$props.open,
handlerRef = _this$props.handlerRef;
if (open && handlerRef) {
this.zoomIn(handlerRef());
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps
/* , prevState */
) {
var _this$props2 = this.props,
open = _this$props2.open,
handlerRef = _this$props2.handlerRef;
if (open !== prevProps.open) {
if (open && handlerRef) {
this.zoomIn(handlerRef());
}
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.timer) clearTimeout(this.timer);
} // 放大
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props3 = this.props,
_this$props3$prefix = _this$props3.prefix,
prefix = _this$props3$prefix === void 0 ? 'rzc' : _this$props3$prefix,
style = _this$props3.style,
_this$props3$maskStyl = _this$props3.maskStyle,
maskStyleProps = _this$props3$maskStyl === void 0 ? {} : _this$props3$maskStyl;
var _this$options = this.options,
enlargedContent = _this$options.enlargedContent,
maskColor = _this$options.maskColor,
maskOpacity = _this$options.maskOpacity;
var maskStyle = _objectSpread({
backgroundColor: maskColor,
opacity: maskOpacity
}, maskStyleProps);
return React.createElement("div", {
className: "".concat(prefix, "_wrapper"),
style: style
}, React.createElement("div", {
style: maskStyle,
className: "".concat(prefix, "_mask")
}), React.createElement("div", {
className: "".concat(prefix, "_pop"),
onClick: this.zoomOut,
ref: function ref(i) {
return _this2.contentRef = i;
}
}, enlargedContent ? React.createElement("div", {
className: "".concat(prefix, "_center")
}, enlargedContent) : this.props.children));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(props, _ref) {
var _self = _ref._self;
var nextState = {
prevProps: props
};
_self.options = Object.assign({}, props.options);
return nextState;
}
}]);
return Child;
}(Component);
export default Child;