@txdfe/at
Version:
一个设计体系组件库
265 lines (263 loc) • 13.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactTransitionGroup = require("react-transition-group");
var _util = require("../util");
var _excluded = ["names", "onAppear", "onAppeared", "onAppearing", "onEnter", "onEntering", "onEntered", "onExit", "onExiting", "onExited"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var noop = function noop() {};
var on = _util.events.on,
off = _util.events.off;
var addClass = _util.dom.addClass,
removeClass = _util.dom.removeClass;
var prefixes = ['-webkit-', '-moz-', '-o-', 'ms-', ''];
function getStyleProperty(node, name) {
var style = window.getComputedStyle(node);
var ret = '';
for (var i = 0; i < prefixes.length; i++) {
ret = style.getPropertyValue(prefixes[i] + name);
if (ret) {
break;
}
}
return ret;
}
var AnimateChild = exports["default"] = /*#__PURE__*/function (_Component) {
function AnimateChild(props) {
var _this2;
_classCallCheck(this, AnimateChild);
_this2 = _callSuper(this, AnimateChild, [props]);
_util.func.bindCtx(_this2, ['handleEnter', 'handleEntering', 'handleEntered', 'handleExit', 'handleExiting', 'handleExited', 'addEndListener']);
_this2.endListeners = {
transitionend: [],
animationend: []
};
_this2.timeoutMap = {};
return _this2;
}
_inherits(AnimateChild, _Component);
return _createClass(AnimateChild, [{
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this3 = this;
Object.keys(this.endListeners).forEach(function (eventName) {
_this3.endListeners[eventName].forEach(function (listener) {
off(_this3.node, eventName, listener);
});
});
this.endListeners = {
transitionend: [],
animationend: []
};
}
}, {
key: "generateEndListener",
value: function generateEndListener(node, done, eventName, id) {
var _this = this;
return function endListener(e) {
if (e && e.target === node) {
if (_this.timeoutMap[id]) {
clearTimeout(_this.timeoutMap[id]);
delete _this.timeoutMap[id];
}
done();
off(node, eventName, endListener);
var listeners = _this.endListeners[eventName];
var index = listeners.indexOf(endListener);
index > -1 && listeners.splice(index, 1);
}
};
}
}, {
key: "addEndListener",
value: function addEndListener(node, done) {
var _this4 = this;
if (_util.support.transition || _util.support.animation) {
var id = (0, _util.guid)();
this.node = node;
if (_util.support.transition) {
var transitionEndListener = this.generateEndListener(node, done, 'transitionend', id);
on(node, 'transitionend', transitionEndListener);
this.endListeners.transitionend.push(transitionEndListener);
}
if (_util.support.animation) {
var animationEndListener = this.generateEndListener(node, done, 'animationend', id);
on(node, 'animationend', animationEndListener);
this.endListeners.animationend.push(animationEndListener);
}
setTimeout(function () {
var transitionDelay = parseFloat(getStyleProperty(node, 'transition-delay')) || 0;
var transitionDuration = parseFloat(getStyleProperty(node, 'transition-duration')) || 0;
var animationDelay = parseFloat(getStyleProperty(node, 'animation-delay')) || 0;
var animationDuration = parseFloat(getStyleProperty(node, 'animation-duration')) || 0;
var time = Math.max(transitionDuration + transitionDelay, animationDuration + animationDelay);
if (time) {
_this4.timeoutMap[id] = setTimeout(function () {
done();
}, time * 1000 + 200);
}
}, 15);
} else {
done();
}
}
}, {
key: "removeEndtListener",
value: function removeEndtListener() {
this.transitionOff && this.transitionOff();
this.animationOff && this.animationOff();
}
}, {
key: "removeClassNames",
value: function removeClassNames(node, names) {
Object.keys(names).forEach(function (key) {
removeClass(node, names[key]);
});
}
}, {
key: "handleEnter",
value: function handleEnter(node, isAppearing) {
var names = this.props.names;
if (names) {
this.removeClassNames(node, names);
var className = isAppearing ? 'appear' : 'enter';
addClass(node, names[className]);
}
var hook = isAppearing ? this.props.onAppear : this.props.onEnter;
hook(node);
}
}, {
key: "handleEntering",
value: function handleEntering(node, isAppearing) {
var _this5 = this;
setTimeout(function () {
var names = _this5.props.names;
if (names) {
var className = isAppearing ? 'appearActive' : 'enterActive';
addClass(node, names[className]);
}
var hook = isAppearing ? _this5.props.onAppearing : _this5.props.onEntering;
hook(node);
}, 10);
}
}, {
key: "handleEntered",
value: function handleEntered(node, isAppearing) {
var names = this.props.names;
if (names) {
var classNames = isAppearing ? [names.appear, names.appearActive] : [names.enter, names.enterActive];
classNames.forEach(function (className) {
removeClass(node, className);
});
}
var hook = isAppearing ? this.props.onAppeared : this.props.onEntered;
hook(node);
}
}, {
key: "handleExit",
value: function handleExit(node) {
var names = this.props.names;
if (names) {
this.removeClassNames(node, names);
addClass(node, names.leave);
}
this.props.onExit(node);
}
}, {
key: "handleExiting",
value: function handleExiting(node) {
var _this6 = this;
setTimeout(function () {
var names = _this6.props.names;
if (names) {
addClass(node, names.leaveActive);
}
_this6.props.onExiting(node);
}, 10);
}
}, {
key: "handleExited",
value: function handleExited(node) {
var names = this.props.names;
if (names) {
[names.leave, names.leaveActive].forEach(function (className) {
removeClass(node, className);
});
}
this.props.onExited(node);
}
}, {
key: "render",
value: function render() {
/* eslint-disable no-unused-vars */
var _this$props = this.props,
names = _this$props.names,
onAppear = _this$props.onAppear,
onAppeared = _this$props.onAppeared,
onAppearing = _this$props.onAppearing,
onEnter = _this$props.onEnter,
onEntering = _this$props.onEntering,
onEntered = _this$props.onEntered,
onExit = _this$props.onExit,
onExiting = _this$props.onExiting,
onExited = _this$props.onExited,
others = _objectWithoutProperties(_this$props, _excluded);
/* eslint-enable no-unused-vars */
return /*#__PURE__*/_react["default"].createElement(_reactTransitionGroup.Transition, _extends({}, others, {
onEnter: this.handleEnter,
onEntering: this.handleEntering,
onEntered: this.handleEntered,
onExit: this.handleExit,
onExiting: this.handleExiting,
onExited: this.handleExited,
addEndListener: this.addEndListener
}));
}
}]);
}(_react.Component);
_defineProperty(AnimateChild, "propTypes", {
names: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].object]),
onAppear: _propTypes["default"].func,
onAppearing: _propTypes["default"].func,
onAppeared: _propTypes["default"].func,
onEnter: _propTypes["default"].func,
onEntering: _propTypes["default"].func,
onEntered: _propTypes["default"].func,
onExit: _propTypes["default"].func,
onExiting: _propTypes["default"].func,
onExited: _propTypes["default"].func
});
_defineProperty(AnimateChild, "defaultProps", {
onAppear: noop,
onAppearing: noop,
onAppeared: noop,
onEnter: noop,
onEntering: noop,
onEntered: noop,
onExit: noop,
onExiting: noop,
onExited: noop
});