508 lines
17 KiB
JavaScript
Raw Normal View History

2020-05-19 11:43:42 +03:00
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactDom = require('react-dom');
var _createReactClass = require('create-react-class');
var _createReactClass2 = _interopRequireDefault(_createReactClass);
var _contains = require('rc-util/lib/Dom/contains');
var _contains2 = _interopRequireDefault(_contains);
var _addEventListener = require('rc-util/lib/Dom/addEventListener');
var _addEventListener2 = _interopRequireDefault(_addEventListener);
var _Popup = require('./Popup');
var _Popup2 = _interopRequireDefault(_Popup);
var _utils = require('./utils');
var _getContainerRenderMixin = require('rc-util/lib/getContainerRenderMixin');
var _getContainerRenderMixin2 = _interopRequireDefault(_getContainerRenderMixin);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function noop() {}
function returnEmptyString() {
return '';
}
function returnDocument() {
return window.document;
}
var isMobile = typeof navigator !== 'undefined' && !!navigator.userAgent.match(/(Android|iPhone|iPad|iPod|iOS|UCWEB)/i);
var ALL_HANDLERS = ['onClick', 'onMouseDown', 'onTouchStart', 'onMouseEnter', 'onMouseLeave', 'onFocus', 'onBlur'];
var Trigger = (0, _createReactClass2['default'])({
displayName: 'Trigger',
propTypes: {
children: _propTypes2['default'].any,
action: _propTypes2['default'].oneOfType([_propTypes2['default'].string, _propTypes2['default'].arrayOf(_propTypes2['default'].string)]),
showAction: _propTypes2['default'].any,
hideAction: _propTypes2['default'].any,
getPopupClassNameFromAlign: _propTypes2['default'].any,
onPopupVisibleChange: _propTypes2['default'].func,
afterPopupVisibleChange: _propTypes2['default'].func,
popup: _propTypes2['default'].oneOfType([_propTypes2['default'].node, _propTypes2['default'].func]).isRequired,
popupStyle: _propTypes2['default'].object,
prefixCls: _propTypes2['default'].string,
popupClassName: _propTypes2['default'].string,
popupPlacement: _propTypes2['default'].string,
builtinPlacements: _propTypes2['default'].object,
popupTransitionName: _propTypes2['default'].oneOfType([_propTypes2['default'].string, _propTypes2['default'].object]),
popupAnimation: _propTypes2['default'].any,
mouseEnterDelay: _propTypes2['default'].number,
mouseLeaveDelay: _propTypes2['default'].number,
zIndex: _propTypes2['default'].number,
focusDelay: _propTypes2['default'].number,
blurDelay: _propTypes2['default'].number,
getPopupContainer: _propTypes2['default'].func,
getDocument: _propTypes2['default'].func,
destroyPopupOnHide: _propTypes2['default'].bool,
mask: _propTypes2['default'].bool,
maskClosable: _propTypes2['default'].bool,
onPopupAlign: _propTypes2['default'].func,
popupAlign: _propTypes2['default'].object,
popupVisible: _propTypes2['default'].bool,
maskTransitionName: _propTypes2['default'].oneOfType([_propTypes2['default'].string, _propTypes2['default'].object]),
maskAnimation: _propTypes2['default'].string
},
mixins: [(0, _getContainerRenderMixin2['default'])({
autoMount: false,
isVisible: function isVisible(instance) {
return instance.state.popupVisible;
},
getContainer: function getContainer(instance) {
var props = instance.props;
var popupContainer = document.createElement('div');
// Make sure default popup container will never cause scrollbar appearing
// https://github.com/react-component/trigger/issues/41
popupContainer.style.position = 'absolute';
popupContainer.style.top = '0';
popupContainer.style.left = '0';
popupContainer.style.width = '100%';
var mountNode = props.getPopupContainer ? props.getPopupContainer((0, _reactDom.findDOMNode)(instance)) : props.getDocument().body;
mountNode.appendChild(popupContainer);
return popupContainer;
}
})],
getDefaultProps: function getDefaultProps() {
return {
prefixCls: 'rc-trigger-popup',
getPopupClassNameFromAlign: returnEmptyString,
getDocument: returnDocument,
onPopupVisibleChange: noop,
afterPopupVisibleChange: noop,
onPopupAlign: noop,
popupClassName: '',
mouseEnterDelay: 0,
mouseLeaveDelay: 0.1,
focusDelay: 0,
blurDelay: 0.15,
popupStyle: {},
destroyPopupOnHide: false,
popupAlign: {},
defaultPopupVisible: false,
mask: false,
maskClosable: true,
action: [],
showAction: [],
hideAction: []
};
},
getInitialState: function getInitialState() {
var props = this.props;
var popupVisible = void 0;
if ('popupVisible' in props) {
popupVisible = !!props.popupVisible;
} else {
popupVisible = !!props.defaultPopupVisible;
}
return {
popupVisible: popupVisible
};
},
componentWillMount: function componentWillMount() {
var _this = this;
ALL_HANDLERS.forEach(function (h) {
_this['fire' + h] = function (e) {
_this.fireEvents(h, e);
};
});
},
componentDidMount: function componentDidMount() {
this.componentDidUpdate({}, {
popupVisible: this.state.popupVisible
});
},
componentWillReceiveProps: function componentWillReceiveProps(_ref) {
var popupVisible = _ref.popupVisible;
if (popupVisible !== undefined) {
this.setState({
popupVisible: popupVisible
});
}
},
componentDidUpdate: function componentDidUpdate(_, prevState) {
var props = this.props;
var state = this.state;
this.renderComponent(null, function () {
if (prevState.popupVisible !== state.popupVisible) {
props.afterPopupVisibleChange(state.popupVisible);
}
});
// We must listen to `mousedown`, edge case:
// https://github.com/ant-design/ant-design/issues/5804
// https://github.com/react-component/calendar/issues/250
// https://github.com/react-component/trigger/issues/50
if (state.popupVisible) {
var currentDocument = void 0;
if (!this.clickOutsideHandler && this.isClickToHide()) {
currentDocument = props.getDocument();
this.clickOutsideHandler = (0, _addEventListener2['default'])(currentDocument, 'mousedown', this.onDocumentClick);
}
// always hide on mobile
// `isMobile` fix: mask clicked will cause below element events triggered
// https://github.com/ant-design/ant-design-mobile/issues/1909
// https://github.com/ant-design/ant-design-mobile/issues/1928
if (!this.touchOutsideHandler && isMobile) {
currentDocument = currentDocument || props.getDocument();
this.touchOutsideHandler = (0, _addEventListener2['default'])(currentDocument, 'click', this.onDocumentClick);
}
return;
}
this.clearOutsideHandler();
},
componentWillUnmount: function componentWillUnmount() {
this.clearDelayTimer();
this.clearOutsideHandler();
},
onMouseEnter: function onMouseEnter(e) {
this.fireEvents('onMouseEnter', e);
this.delaySetPopupVisible(true, this.props.mouseEnterDelay);
},
onMouseLeave: function onMouseLeave(e) {
this.fireEvents('onMouseLeave', e);
this.delaySetPopupVisible(false, this.props.mouseLeaveDelay);
},
onPopupMouseEnter: function onPopupMouseEnter() {
this.clearDelayTimer();
},
onPopupMouseLeave: function onPopupMouseLeave(e) {
// https://github.com/react-component/trigger/pull/13
// react bug?
if (e.relatedTarget && !e.relatedTarget.setTimeout && this._component && this._component.getPopupDomNode && (0, _contains2['default'])(this._component.getPopupDomNode(), e.relatedTarget)) {
return;
}
this.delaySetPopupVisible(false, this.props.mouseLeaveDelay);
},
onFocus: function onFocus(e) {
this.fireEvents('onFocus', e);
// incase focusin and focusout
this.clearDelayTimer();
if (this.isFocusToShow()) {
this.focusTime = Date.now();
this.delaySetPopupVisible(true, this.props.focusDelay);
}
},
onMouseDown: function onMouseDown(e) {
this.fireEvents('onMouseDown', e);
this.preClickTime = Date.now();
},
onTouchStart: function onTouchStart(e) {
this.fireEvents('onTouchStart', e);
this.preTouchTime = Date.now();
},
onBlur: function onBlur(e) {
this.fireEvents('onBlur', e);
this.clearDelayTimer();
if (this.isBlurToHide()) {
this.delaySetPopupVisible(false, this.props.blurDelay);
}
},
onClick: function onClick(event) {
this.fireEvents('onClick', event);
// focus will trigger click
if (this.focusTime) {
var preTime = void 0;
if (this.preClickTime && this.preTouchTime) {
preTime = Math.min(this.preClickTime, this.preTouchTime);
} else if (this.preClickTime) {
preTime = this.preClickTime;
} else if (this.preTouchTime) {
preTime = this.preTouchTime;
}
if (Math.abs(preTime - this.focusTime) < 20) {
return;
}
this.focusTime = 0;
}
this.preClickTime = 0;
this.preTouchTime = 0;
event.preventDefault();
var nextVisible = !this.state.popupVisible;
if (this.isClickToHide() && !nextVisible || nextVisible && this.isClickToShow()) {
this.setPopupVisible(!this.state.popupVisible);
}
},
onDocumentClick: function onDocumentClick(event) {
if (this.props.mask && !this.props.maskClosable) {
return;
}
var target = event.target;
var root = (0, _reactDom.findDOMNode)(this);
var popupNode = this.getPopupDomNode();
if (!(0, _contains2['default'])(root, target) && !(0, _contains2['default'])(popupNode, target)) {
this.close();
}
},
getPopupDomNode: function getPopupDomNode() {
// for test
if (this._component && this._component.getPopupDomNode) {
return this._component.getPopupDomNode();
}
return null;
},
getRootDomNode: function getRootDomNode() {
return (0, _reactDom.findDOMNode)(this);
},
getPopupClassNameFromAlign: function getPopupClassNameFromAlign(align) {
var className = [];
var props = this.props;
var popupPlacement = props.popupPlacement,
builtinPlacements = props.builtinPlacements,
prefixCls = props.prefixCls;
if (popupPlacement && builtinPlacements) {
className.push((0, _utils.getPopupClassNameFromAlign)(builtinPlacements, prefixCls, align));
}
if (props.getPopupClassNameFromAlign) {
className.push(props.getPopupClassNameFromAlign(align));
}
return className.join(' ');
},
getPopupAlign: function getPopupAlign() {
var props = this.props;
var popupPlacement = props.popupPlacement,
popupAlign = props.popupAlign,
builtinPlacements = props.builtinPlacements;
if (popupPlacement && builtinPlacements) {
return (0, _utils.getAlignFromPlacement)(builtinPlacements, popupPlacement, popupAlign);
}
return popupAlign;
},
getComponent: function getComponent() {
var props = this.props,
state = this.state;
var mouseProps = {};
if (this.isMouseEnterToShow()) {
mouseProps.onMouseEnter = this.onPopupMouseEnter;
}
if (this.isMouseLeaveToHide()) {
mouseProps.onMouseLeave = this.onPopupMouseLeave;
}
return _react2['default'].createElement(
_Popup2['default'],
(0, _extends3['default'])({
prefixCls: props.prefixCls,
destroyPopupOnHide: props.destroyPopupOnHide,
visible: state.popupVisible,
className: props.popupClassName,
action: props.action,
align: this.getPopupAlign(),
onAlign: props.onPopupAlign,
animation: props.popupAnimation,
getClassNameFromAlign: this.getPopupClassNameFromAlign
}, mouseProps, {
getRootDomNode: this.getRootDomNode,
style: props.popupStyle,
mask: props.mask,
zIndex: props.zIndex,
transitionName: props.popupTransitionName,
maskAnimation: props.maskAnimation,
maskTransitionName: props.maskTransitionName
}),
typeof props.popup === 'function' ? props.popup() : props.popup
);
},
setPopupVisible: function setPopupVisible(popupVisible) {
this.clearDelayTimer();
if (this.state.popupVisible !== popupVisible) {
if (!('popupVisible' in this.props)) {
this.setState({
popupVisible: popupVisible
});
}
this.props.onPopupVisibleChange(popupVisible);
}
},
delaySetPopupVisible: function delaySetPopupVisible(visible, delayS) {
var _this2 = this;
var delay = delayS * 1000;
this.clearDelayTimer();
if (delay) {
this.delayTimer = setTimeout(function () {
_this2.setPopupVisible(visible);
_this2.clearDelayTimer();
}, delay);
} else {
this.setPopupVisible(visible);
}
},
clearDelayTimer: function clearDelayTimer() {
if (this.delayTimer) {
clearTimeout(this.delayTimer);
this.delayTimer = null;
}
},
clearOutsideHandler: function clearOutsideHandler() {
if (this.clickOutsideHandler) {
this.clickOutsideHandler.remove();
this.clickOutsideHandler = null;
}
if (this.touchOutsideHandler) {
this.touchOutsideHandler.remove();
this.touchOutsideHandler = null;
}
},
createTwoChains: function createTwoChains(event) {
var childPros = this.props.children.props;
var props = this.props;
if (childPros[event] && props[event]) {
return this['fire' + event];
}
return childPros[event] || props[event];
},
isClickToShow: function isClickToShow() {
var _props = this.props,
action = _props.action,
showAction = _props.showAction;
return action.indexOf('click') !== -1 || showAction.indexOf('click') !== -1;
},
isClickToHide: function isClickToHide() {
var _props2 = this.props,
action = _props2.action,
hideAction = _props2.hideAction;
return action.indexOf('click') !== -1 || hideAction.indexOf('click') !== -1;
},
isMouseEnterToShow: function isMouseEnterToShow() {
var _props3 = this.props,
action = _props3.action,
showAction = _props3.showAction;
return action.indexOf('hover') !== -1 || showAction.indexOf('mouseEnter') !== -1;
},
isMouseLeaveToHide: function isMouseLeaveToHide() {
var _props4 = this.props,
action = _props4.action,
hideAction = _props4.hideAction;
return action.indexOf('hover') !== -1 || hideAction.indexOf('mouseLeave') !== -1;
},
isFocusToShow: function isFocusToShow() {
var _props5 = this.props,
action = _props5.action,
showAction = _props5.showAction;
return action.indexOf('focus') !== -1 || showAction.indexOf('focus') !== -1;
},
isBlurToHide: function isBlurToHide() {
var _props6 = this.props,
action = _props6.action,
hideAction = _props6.hideAction;
return action.indexOf('focus') !== -1 || hideAction.indexOf('blur') !== -1;
},
forcePopupAlign: function forcePopupAlign() {
if (this.state.popupVisible && this._component && this._component.alignInstance) {
this._component.alignInstance.forceAlign();
}
},
fireEvents: function fireEvents(type, e) {
var childCallback = this.props.children.props[type];
if (childCallback) {
childCallback(e);
}
var callback = this.props[type];
if (callback) {
callback(e);
}
},
close: function close() {
this.setPopupVisible(false);
},
render: function render() {
var props = this.props;
var children = props.children;
var child = _react2['default'].Children.only(children);
var newChildProps = {};
if (this.isClickToHide() || this.isClickToShow()) {
newChildProps.onClick = this.onClick;
newChildProps.onMouseDown = this.onMouseDown;
newChildProps.onTouchStart = this.onTouchStart;
} else {
newChildProps.onClick = this.createTwoChains('onClick');
newChildProps.onMouseDown = this.createTwoChains('onMouseDown');
newChildProps.onTouchStart = this.createTwoChains('onTouchStart');
}
if (this.isMouseEnterToShow()) {
newChildProps.onMouseEnter = this.onMouseEnter;
} else {
newChildProps.onMouseEnter = this.createTwoChains('onMouseEnter');
}
if (this.isMouseLeaveToHide()) {
newChildProps.onMouseLeave = this.onMouseLeave;
} else {
newChildProps.onMouseLeave = this.createTwoChains('onMouseLeave');
}
if (this.isFocusToShow() || this.isBlurToHide()) {
newChildProps.onFocus = this.onFocus;
newChildProps.onBlur = this.onBlur;
} else {
newChildProps.onFocus = this.createTwoChains('onFocus');
newChildProps.onBlur = this.createTwoChains('onBlur');
}
return _react2['default'].cloneElement(child, newChildProps);
}
});
exports['default'] = Trigger;
module.exports = exports['default'];