UNPKG

31.2 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11exports.generateTrigger = generateTrigger;
12
13var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
14
15var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
16
17var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
18
19var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
20
21var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
22
23var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
24
25var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
26
27var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
28
29var React = _interopRequireWildcard(require("react"));
30
31var _reactDom = _interopRequireDefault(require("react-dom"));
32
33var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
34
35var _contains = _interopRequireDefault(require("rc-util/lib/Dom/contains"));
36
37var _findDOMNode = _interopRequireDefault(require("rc-util/lib/Dom/findDOMNode"));
38
39var _ref2 = require("rc-util/lib/ref");
40
41var _addEventListener = _interopRequireDefault(require("rc-util/lib/Dom/addEventListener"));
42
43var _Portal = _interopRequireDefault(require("rc-util/lib/Portal"));
44
45var _classnames = _interopRequireDefault(require("classnames"));
46
47var _alignUtil = require("./utils/alignUtil");
48
49var _Popup = _interopRequireDefault(require("./Popup"));
50
51var _context = _interopRequireDefault(require("./context"));
52
53function noop() {}
54
55function returnEmptyString() {
56 return '';
57}
58
59function returnDocument(element) {
60 if (element) {
61 return element.ownerDocument;
62 }
63
64 return window.document;
65}
66
67var ALL_HANDLERS = ['onClick', 'onMouseDown', 'onTouchStart', 'onMouseEnter', 'onMouseLeave', 'onFocus', 'onBlur', 'onContextMenu'];
68
69/**
70 * Internal usage. Do not use in your code since this will be removed.
71 */
72function generateTrigger(PortalComponent) {
73 var Trigger = /*#__PURE__*/function (_React$Component) {
74 (0, _inherits2.default)(Trigger, _React$Component);
75
76 var _super = (0, _createSuper2.default)(Trigger);
77
78 // ensure `getContainer` will be called only once
79 function Trigger(props) {
80 var _this;
81
82 (0, _classCallCheck2.default)(this, Trigger);
83 _this = _super.call(this, props);
84 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "popupRef", /*#__PURE__*/React.createRef());
85 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "triggerRef", /*#__PURE__*/React.createRef());
86 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "portalContainer", void 0);
87 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "attachId", void 0);
88 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "clickOutsideHandler", void 0);
89 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "touchOutsideHandler", void 0);
90 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "contextMenuOutsideHandler1", void 0);
91 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "contextMenuOutsideHandler2", void 0);
92 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "mouseDownTimeout", void 0);
93 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "focusTime", void 0);
94 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "preClickTime", void 0);
95 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "preTouchTime", void 0);
96 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "delayTimer", void 0);
97 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "hasPopupMouseDown", void 0);
98 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onMouseEnter", function (e) {
99 var mouseEnterDelay = _this.props.mouseEnterDelay;
100
101 _this.fireEvents('onMouseEnter', e);
102
103 _this.delaySetPopupVisible(true, mouseEnterDelay, mouseEnterDelay ? null : e);
104 });
105 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onMouseMove", function (e) {
106 _this.fireEvents('onMouseMove', e);
107
108 _this.setPoint(e);
109 });
110 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onMouseLeave", function (e) {
111 _this.fireEvents('onMouseLeave', e);
112
113 _this.delaySetPopupVisible(false, _this.props.mouseLeaveDelay);
114 });
115 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onPopupMouseEnter", function () {
116 _this.clearDelayTimer();
117 });
118 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onPopupMouseLeave", function (e) {
119 var _this$popupRef$curren;
120
121 // https://github.com/react-component/trigger/pull/13
122 // react bug?
123 if (e.relatedTarget && !e.relatedTarget.setTimeout && (0, _contains.default)((_this$popupRef$curren = _this.popupRef.current) === null || _this$popupRef$curren === void 0 ? void 0 : _this$popupRef$curren.getElement(), e.relatedTarget)) {
124 return;
125 }
126
127 _this.delaySetPopupVisible(false, _this.props.mouseLeaveDelay);
128 });
129 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onFocus", function (e) {
130 _this.fireEvents('onFocus', e); // incase focusin and focusout
131
132
133 _this.clearDelayTimer();
134
135 if (_this.isFocusToShow()) {
136 _this.focusTime = Date.now();
137
138 _this.delaySetPopupVisible(true, _this.props.focusDelay);
139 }
140 });
141 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onMouseDown", function (e) {
142 _this.fireEvents('onMouseDown', e);
143
144 _this.preClickTime = Date.now();
145 });
146 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onTouchStart", function (e) {
147 _this.fireEvents('onTouchStart', e);
148
149 _this.preTouchTime = Date.now();
150 });
151 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onBlur", function (e) {
152 _this.fireEvents('onBlur', e);
153
154 _this.clearDelayTimer();
155
156 if (_this.isBlurToHide()) {
157 _this.delaySetPopupVisible(false, _this.props.blurDelay);
158 }
159 });
160 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onContextMenu", function (e) {
161 e.preventDefault();
162
163 _this.fireEvents('onContextMenu', e);
164
165 _this.setPopupVisible(true, e);
166 });
167 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onContextMenuClose", function () {
168 if (_this.isContextMenuToShow()) {
169 _this.close();
170 }
171 });
172 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onClick", function (event) {
173 _this.fireEvents('onClick', event); // focus will trigger click
174
175
176 if (_this.focusTime) {
177 var preTime;
178
179 if (_this.preClickTime && _this.preTouchTime) {
180 preTime = Math.min(_this.preClickTime, _this.preTouchTime);
181 } else if (_this.preClickTime) {
182 preTime = _this.preClickTime;
183 } else if (_this.preTouchTime) {
184 preTime = _this.preTouchTime;
185 }
186
187 if (Math.abs(preTime - _this.focusTime) < 20) {
188 return;
189 }
190
191 _this.focusTime = 0;
192 }
193
194 _this.preClickTime = 0;
195 _this.preTouchTime = 0; // Only prevent default when all the action is click.
196 // https://github.com/ant-design/ant-design/issues/17043
197 // https://github.com/ant-design/ant-design/issues/17291
198
199 if (_this.isClickToShow() && (_this.isClickToHide() || _this.isBlurToHide()) && event && event.preventDefault) {
200 event.preventDefault();
201 }
202
203 var nextVisible = !_this.state.popupVisible;
204
205 if (_this.isClickToHide() && !nextVisible || nextVisible && _this.isClickToShow()) {
206 _this.setPopupVisible(!_this.state.popupVisible, event);
207 }
208 });
209 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onPopupMouseDown", function () {
210 _this.hasPopupMouseDown = true;
211 clearTimeout(_this.mouseDownTimeout);
212 _this.mouseDownTimeout = window.setTimeout(function () {
213 _this.hasPopupMouseDown = false;
214 }, 0);
215
216 if (_this.context) {
217 var _this$context;
218
219 (_this$context = _this.context).onPopupMouseDown.apply(_this$context, arguments);
220 }
221 });
222 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onDocumentClick", function (event) {
223 if (_this.props.mask && !_this.props.maskClosable) {
224 return;
225 }
226
227 var target = event.target;
228
229 var root = _this.getRootDomNode();
230
231 var popupNode = _this.getPopupDomNode();
232
233 if ( // mousedown on the target should also close popup when action is contextMenu.
234 // https://github.com/ant-design/ant-design/issues/29853
235 (!(0, _contains.default)(root, target) || _this.isContextMenuOnly()) && !(0, _contains.default)(popupNode, target) && !_this.hasPopupMouseDown) {
236 _this.close();
237 }
238 });
239 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getRootDomNode", function () {
240 var getTriggerDOMNode = _this.props.getTriggerDOMNode;
241
242 if (getTriggerDOMNode) {
243 return getTriggerDOMNode(_this.triggerRef.current);
244 }
245
246 try {
247 var domNode = (0, _findDOMNode.default)(_this.triggerRef.current);
248
249 if (domNode) {
250 return domNode;
251 }
252 } catch (err) {// Do nothing
253 }
254
255 return _reactDom.default.findDOMNode((0, _assertThisInitialized2.default)(_this));
256 });
257 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getPopupClassNameFromAlign", function (align) {
258 var className = [];
259 var _this$props = _this.props,
260 popupPlacement = _this$props.popupPlacement,
261 builtinPlacements = _this$props.builtinPlacements,
262 prefixCls = _this$props.prefixCls,
263 alignPoint = _this$props.alignPoint,
264 getPopupClassNameFromAlign = _this$props.getPopupClassNameFromAlign;
265
266 if (popupPlacement && builtinPlacements) {
267 className.push((0, _alignUtil.getAlignPopupClassName)(builtinPlacements, prefixCls, align, alignPoint));
268 }
269
270 if (getPopupClassNameFromAlign) {
271 className.push(getPopupClassNameFromAlign(align));
272 }
273
274 return className.join(' ');
275 });
276 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getComponent", function () {
277 var _this$props2 = _this.props,
278 prefixCls = _this$props2.prefixCls,
279 destroyPopupOnHide = _this$props2.destroyPopupOnHide,
280 popupClassName = _this$props2.popupClassName,
281 onPopupAlign = _this$props2.onPopupAlign,
282 popupMotion = _this$props2.popupMotion,
283 popupAnimation = _this$props2.popupAnimation,
284 popupTransitionName = _this$props2.popupTransitionName,
285 popupStyle = _this$props2.popupStyle,
286 mask = _this$props2.mask,
287 maskAnimation = _this$props2.maskAnimation,
288 maskTransitionName = _this$props2.maskTransitionName,
289 maskMotion = _this$props2.maskMotion,
290 zIndex = _this$props2.zIndex,
291 popup = _this$props2.popup,
292 stretch = _this$props2.stretch,
293 alignPoint = _this$props2.alignPoint,
294 mobile = _this$props2.mobile,
295 forceRender = _this$props2.forceRender,
296 onPopupClick = _this$props2.onPopupClick;
297 var _this$state = _this.state,
298 popupVisible = _this$state.popupVisible,
299 point = _this$state.point;
300
301 var align = _this.getPopupAlign();
302
303 var mouseProps = {};
304
305 if (_this.isMouseEnterToShow()) {
306 mouseProps.onMouseEnter = _this.onPopupMouseEnter;
307 }
308
309 if (_this.isMouseLeaveToHide()) {
310 mouseProps.onMouseLeave = _this.onPopupMouseLeave;
311 }
312
313 mouseProps.onMouseDown = _this.onPopupMouseDown;
314 mouseProps.onTouchStart = _this.onPopupMouseDown;
315 return /*#__PURE__*/React.createElement(_Popup.default, (0, _extends2.default)({
316 prefixCls: prefixCls,
317 destroyPopupOnHide: destroyPopupOnHide,
318 visible: popupVisible,
319 point: alignPoint && point,
320 className: popupClassName,
321 align: align,
322 onAlign: onPopupAlign,
323 animation: popupAnimation,
324 getClassNameFromAlign: _this.getPopupClassNameFromAlign
325 }, mouseProps, {
326 stretch: stretch,
327 getRootDomNode: _this.getRootDomNode,
328 style: popupStyle,
329 mask: mask,
330 zIndex: zIndex,
331 transitionName: popupTransitionName,
332 maskAnimation: maskAnimation,
333 maskTransitionName: maskTransitionName,
334 maskMotion: maskMotion,
335 ref: _this.popupRef,
336 motion: popupMotion,
337 mobile: mobile,
338 forceRender: forceRender,
339 onClick: onPopupClick
340 }), typeof popup === 'function' ? popup() : popup);
341 });
342 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "attachParent", function (popupContainer) {
343 _raf.default.cancel(_this.attachId);
344
345 var _this$props3 = _this.props,
346 getPopupContainer = _this$props3.getPopupContainer,
347 getDocument = _this$props3.getDocument;
348
349 var domNode = _this.getRootDomNode();
350
351 var mountNode;
352
353 if (!getPopupContainer) {
354 mountNode = getDocument(_this.getRootDomNode()).body;
355 } else if (domNode || getPopupContainer.length === 0) {
356 // Compatible for legacy getPopupContainer with domNode argument.
357 // If no need `domNode` argument, will call directly.
358 // https://codesandbox.io/s/eloquent-mclean-ss93m?file=/src/App.js
359 mountNode = getPopupContainer(domNode);
360 }
361
362 if (mountNode) {
363 mountNode.appendChild(popupContainer);
364 } else {
365 // Retry after frame render in case parent not ready
366 _this.attachId = (0, _raf.default)(function () {
367 _this.attachParent(popupContainer);
368 });
369 }
370 });
371 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getContainer", function () {
372 if (!_this.portalContainer) {
373 // In React.StrictMode component will call render multiple time in first mount.
374 // When you want to refactor with FC, useRef will also init multiple time and
375 // point to different useRef instance which will create multiple element
376 // (This multiple render will not trigger effect so you can not clean up this
377 // in effect). But this is safe with class component since it always point to same class instance.
378 var getDocument = _this.props.getDocument;
379 var popupContainer = getDocument(_this.getRootDomNode()).createElement('div'); // Make sure default popup container will never cause scrollbar appearing
380 // https://github.com/react-component/trigger/issues/41
381
382 popupContainer.style.position = 'absolute';
383 popupContainer.style.top = '0';
384 popupContainer.style.left = '0';
385 popupContainer.style.width = '100%';
386 _this.portalContainer = popupContainer;
387 }
388
389 _this.attachParent(_this.portalContainer);
390
391 return _this.portalContainer;
392 });
393 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setPoint", function (point) {
394 var alignPoint = _this.props.alignPoint;
395 if (!alignPoint || !point) return;
396
397 _this.setState({
398 point: {
399 pageX: point.pageX,
400 pageY: point.pageY
401 }
402 });
403 });
404 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handlePortalUpdate", function () {
405 if (_this.state.prevPopupVisible !== _this.state.popupVisible) {
406 _this.props.afterPopupVisibleChange(_this.state.popupVisible);
407 }
408 });
409 (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "triggerContextValue", {
410 onPopupMouseDown: _this.onPopupMouseDown
411 });
412
413 var _popupVisible;
414
415 if ('popupVisible' in props) {
416 _popupVisible = !!props.popupVisible;
417 } else {
418 _popupVisible = !!props.defaultPopupVisible;
419 }
420
421 _this.state = {
422 prevPopupVisible: _popupVisible,
423 popupVisible: _popupVisible
424 };
425 ALL_HANDLERS.forEach(function (h) {
426 _this["fire".concat(h)] = function (e) {
427 _this.fireEvents(h, e);
428 };
429 });
430 return _this;
431 }
432
433 (0, _createClass2.default)(Trigger, [{
434 key: "componentDidMount",
435 value: function componentDidMount() {
436 this.componentDidUpdate();
437 }
438 }, {
439 key: "componentDidUpdate",
440 value: function componentDidUpdate() {
441 var props = this.props;
442 var state = this.state; // We must listen to `mousedown` or `touchstart`, edge case:
443 // https://github.com/ant-design/ant-design/issues/5804
444 // https://github.com/react-component/calendar/issues/250
445 // https://github.com/react-component/trigger/issues/50
446
447 if (state.popupVisible) {
448 var currentDocument;
449
450 if (!this.clickOutsideHandler && (this.isClickToHide() || this.isContextMenuToShow())) {
451 currentDocument = props.getDocument(this.getRootDomNode());
452 this.clickOutsideHandler = (0, _addEventListener.default)(currentDocument, 'mousedown', this.onDocumentClick);
453 } // always hide on mobile
454
455
456 if (!this.touchOutsideHandler) {
457 currentDocument = currentDocument || props.getDocument(this.getRootDomNode());
458 this.touchOutsideHandler = (0, _addEventListener.default)(currentDocument, 'touchstart', this.onDocumentClick);
459 } // close popup when trigger type contains 'onContextMenu' and document is scrolling.
460
461
462 if (!this.contextMenuOutsideHandler1 && this.isContextMenuToShow()) {
463 currentDocument = currentDocument || props.getDocument(this.getRootDomNode());
464 this.contextMenuOutsideHandler1 = (0, _addEventListener.default)(currentDocument, 'scroll', this.onContextMenuClose);
465 } // close popup when trigger type contains 'onContextMenu' and window is blur.
466
467
468 if (!this.contextMenuOutsideHandler2 && this.isContextMenuToShow()) {
469 this.contextMenuOutsideHandler2 = (0, _addEventListener.default)(window, 'blur', this.onContextMenuClose);
470 }
471
472 return;
473 }
474
475 this.clearOutsideHandler();
476 }
477 }, {
478 key: "componentWillUnmount",
479 value: function componentWillUnmount() {
480 this.clearDelayTimer();
481 this.clearOutsideHandler();
482 clearTimeout(this.mouseDownTimeout);
483
484 _raf.default.cancel(this.attachId);
485 }
486 }, {
487 key: "getPopupDomNode",
488 value: function getPopupDomNode() {
489 var _this$popupRef$curren2;
490
491 // for test
492 return ((_this$popupRef$curren2 = this.popupRef.current) === null || _this$popupRef$curren2 === void 0 ? void 0 : _this$popupRef$curren2.getElement()) || null;
493 }
494 }, {
495 key: "getPopupAlign",
496 value: function getPopupAlign() {
497 var props = this.props;
498 var popupPlacement = props.popupPlacement,
499 popupAlign = props.popupAlign,
500 builtinPlacements = props.builtinPlacements;
501
502 if (popupPlacement && builtinPlacements) {
503 return (0, _alignUtil.getAlignFromPlacement)(builtinPlacements, popupPlacement, popupAlign);
504 }
505
506 return popupAlign;
507 }
508 }, {
509 key: "setPopupVisible",
510 value:
511 /**
512 * @param popupVisible Show or not the popup element
513 * @param event SyntheticEvent, used for `pointAlign`
514 */
515 function setPopupVisible(popupVisible, event) {
516 var alignPoint = this.props.alignPoint;
517 var prevPopupVisible = this.state.popupVisible;
518 this.clearDelayTimer();
519
520 if (prevPopupVisible !== popupVisible) {
521 if (!('popupVisible' in this.props)) {
522 this.setState({
523 popupVisible: popupVisible,
524 prevPopupVisible: prevPopupVisible
525 });
526 }
527
528 this.props.onPopupVisibleChange(popupVisible);
529 } // Always record the point position since mouseEnterDelay will delay the show
530
531
532 if (alignPoint && event && popupVisible) {
533 this.setPoint(event);
534 }
535 }
536 }, {
537 key: "delaySetPopupVisible",
538 value: function delaySetPopupVisible(visible, delayS, event) {
539 var _this2 = this;
540
541 var delay = delayS * 1000;
542 this.clearDelayTimer();
543
544 if (delay) {
545 var point = event ? {
546 pageX: event.pageX,
547 pageY: event.pageY
548 } : null;
549 this.delayTimer = window.setTimeout(function () {
550 _this2.setPopupVisible(visible, point);
551
552 _this2.clearDelayTimer();
553 }, delay);
554 } else {
555 this.setPopupVisible(visible, event);
556 }
557 }
558 }, {
559 key: "clearDelayTimer",
560 value: function clearDelayTimer() {
561 if (this.delayTimer) {
562 clearTimeout(this.delayTimer);
563 this.delayTimer = null;
564 }
565 }
566 }, {
567 key: "clearOutsideHandler",
568 value: function clearOutsideHandler() {
569 if (this.clickOutsideHandler) {
570 this.clickOutsideHandler.remove();
571 this.clickOutsideHandler = null;
572 }
573
574 if (this.contextMenuOutsideHandler1) {
575 this.contextMenuOutsideHandler1.remove();
576 this.contextMenuOutsideHandler1 = null;
577 }
578
579 if (this.contextMenuOutsideHandler2) {
580 this.contextMenuOutsideHandler2.remove();
581 this.contextMenuOutsideHandler2 = null;
582 }
583
584 if (this.touchOutsideHandler) {
585 this.touchOutsideHandler.remove();
586 this.touchOutsideHandler = null;
587 }
588 }
589 }, {
590 key: "createTwoChains",
591 value: function createTwoChains(event) {
592 var childPros = this.props.children.props;
593 var props = this.props;
594
595 if (childPros[event] && props[event]) {
596 return this["fire".concat(event)];
597 }
598
599 return childPros[event] || props[event];
600 }
601 }, {
602 key: "isClickToShow",
603 value: function isClickToShow() {
604 var _this$props4 = this.props,
605 action = _this$props4.action,
606 showAction = _this$props4.showAction;
607 return action.indexOf('click') !== -1 || showAction.indexOf('click') !== -1;
608 }
609 }, {
610 key: "isContextMenuOnly",
611 value: function isContextMenuOnly() {
612 var action = this.props.action;
613 return action === 'contextMenu' || action.length === 1 && action[0] === 'contextMenu';
614 }
615 }, {
616 key: "isContextMenuToShow",
617 value: function isContextMenuToShow() {
618 var _this$props5 = this.props,
619 action = _this$props5.action,
620 showAction = _this$props5.showAction;
621 return action.indexOf('contextMenu') !== -1 || showAction.indexOf('contextMenu') !== -1;
622 }
623 }, {
624 key: "isClickToHide",
625 value: function isClickToHide() {
626 var _this$props6 = this.props,
627 action = _this$props6.action,
628 hideAction = _this$props6.hideAction;
629 return action.indexOf('click') !== -1 || hideAction.indexOf('click') !== -1;
630 }
631 }, {
632 key: "isMouseEnterToShow",
633 value: function isMouseEnterToShow() {
634 var _this$props7 = this.props,
635 action = _this$props7.action,
636 showAction = _this$props7.showAction;
637 return action.indexOf('hover') !== -1 || showAction.indexOf('mouseEnter') !== -1;
638 }
639 }, {
640 key: "isMouseLeaveToHide",
641 value: function isMouseLeaveToHide() {
642 var _this$props8 = this.props,
643 action = _this$props8.action,
644 hideAction = _this$props8.hideAction;
645 return action.indexOf('hover') !== -1 || hideAction.indexOf('mouseLeave') !== -1;
646 }
647 }, {
648 key: "isFocusToShow",
649 value: function isFocusToShow() {
650 var _this$props9 = this.props,
651 action = _this$props9.action,
652 showAction = _this$props9.showAction;
653 return action.indexOf('focus') !== -1 || showAction.indexOf('focus') !== -1;
654 }
655 }, {
656 key: "isBlurToHide",
657 value: function isBlurToHide() {
658 var _this$props10 = this.props,
659 action = _this$props10.action,
660 hideAction = _this$props10.hideAction;
661 return action.indexOf('focus') !== -1 || hideAction.indexOf('blur') !== -1;
662 }
663 }, {
664 key: "forcePopupAlign",
665 value: function forcePopupAlign() {
666 if (this.state.popupVisible) {
667 var _this$popupRef$curren3;
668
669 (_this$popupRef$curren3 = this.popupRef.current) === null || _this$popupRef$curren3 === void 0 ? void 0 : _this$popupRef$curren3.forceAlign();
670 }
671 }
672 }, {
673 key: "fireEvents",
674 value: function fireEvents(type, e) {
675 var childCallback = this.props.children.props[type];
676
677 if (childCallback) {
678 childCallback(e);
679 }
680
681 var callback = this.props[type];
682
683 if (callback) {
684 callback(e);
685 }
686 }
687 }, {
688 key: "close",
689 value: function close() {
690 this.setPopupVisible(false);
691 }
692 }, {
693 key: "render",
694 value: function render() {
695 var popupVisible = this.state.popupVisible;
696 var _this$props11 = this.props,
697 children = _this$props11.children,
698 forceRender = _this$props11.forceRender,
699 alignPoint = _this$props11.alignPoint,
700 className = _this$props11.className,
701 autoDestroy = _this$props11.autoDestroy;
702 var child = React.Children.only(children);
703 var newChildProps = {
704 key: 'trigger'
705 }; // ============================== Visible Handlers ==============================
706 // >>> ContextMenu
707
708 if (this.isContextMenuToShow()) {
709 newChildProps.onContextMenu = this.onContextMenu;
710 } else {
711 newChildProps.onContextMenu = this.createTwoChains('onContextMenu');
712 } // >>> Click
713
714
715 if (this.isClickToHide() || this.isClickToShow()) {
716 newChildProps.onClick = this.onClick;
717 newChildProps.onMouseDown = this.onMouseDown;
718 newChildProps.onTouchStart = this.onTouchStart;
719 } else {
720 newChildProps.onClick = this.createTwoChains('onClick');
721 newChildProps.onMouseDown = this.createTwoChains('onMouseDown');
722 newChildProps.onTouchStart = this.createTwoChains('onTouchStart');
723 } // >>> Hover(enter)
724
725
726 if (this.isMouseEnterToShow()) {
727 newChildProps.onMouseEnter = this.onMouseEnter; // Point align
728
729 if (alignPoint) {
730 newChildProps.onMouseMove = this.onMouseMove;
731 }
732 } else {
733 newChildProps.onMouseEnter = this.createTwoChains('onMouseEnter');
734 } // >>> Hover(leave)
735
736
737 if (this.isMouseLeaveToHide()) {
738 newChildProps.onMouseLeave = this.onMouseLeave;
739 } else {
740 newChildProps.onMouseLeave = this.createTwoChains('onMouseLeave');
741 } // >>> Focus
742
743
744 if (this.isFocusToShow() || this.isBlurToHide()) {
745 newChildProps.onFocus = this.onFocus;
746 newChildProps.onBlur = this.onBlur;
747 } else {
748 newChildProps.onFocus = this.createTwoChains('onFocus');
749 newChildProps.onBlur = this.createTwoChains('onBlur');
750 } // =================================== Render ===================================
751
752
753 var childrenClassName = (0, _classnames.default)(child && child.props && child.props.className, className);
754
755 if (childrenClassName) {
756 newChildProps.className = childrenClassName;
757 }
758
759 var cloneProps = (0, _objectSpread2.default)({}, newChildProps);
760
761 if ((0, _ref2.supportRef)(child)) {
762 cloneProps.ref = (0, _ref2.composeRef)(this.triggerRef, child.ref);
763 }
764
765 var trigger = /*#__PURE__*/React.cloneElement(child, cloneProps);
766 var portal; // prevent unmounting after it's rendered
767
768 if (popupVisible || this.popupRef.current || forceRender) {
769 portal = /*#__PURE__*/React.createElement(PortalComponent, {
770 key: "portal",
771 getContainer: this.getContainer,
772 didUpdate: this.handlePortalUpdate
773 }, this.getComponent());
774 }
775
776 if (!popupVisible && autoDestroy) {
777 portal = null;
778 }
779
780 return /*#__PURE__*/React.createElement(_context.default.Provider, {
781 value: this.triggerContextValue
782 }, trigger, portal);
783 }
784 }], [{
785 key: "getDerivedStateFromProps",
786 value: function getDerivedStateFromProps(_ref, prevState) {
787 var popupVisible = _ref.popupVisible;
788 var newState = {};
789
790 if (popupVisible !== undefined && prevState.popupVisible !== popupVisible) {
791 newState.popupVisible = popupVisible;
792 newState.prevPopupVisible = prevState.popupVisible;
793 }
794
795 return newState;
796 }
797 }]);
798 return Trigger;
799 }(React.Component);
800
801 (0, _defineProperty2.default)(Trigger, "contextType", _context.default);
802 (0, _defineProperty2.default)(Trigger, "defaultProps", {
803 prefixCls: 'rc-trigger-popup',
804 getPopupClassNameFromAlign: returnEmptyString,
805 getDocument: returnDocument,
806 onPopupVisibleChange: noop,
807 afterPopupVisibleChange: noop,
808 onPopupAlign: noop,
809 popupClassName: '',
810 mouseEnterDelay: 0,
811 mouseLeaveDelay: 0.1,
812 focusDelay: 0,
813 blurDelay: 0.15,
814 popupStyle: {},
815 destroyPopupOnHide: false,
816 popupAlign: {},
817 defaultPopupVisible: false,
818 mask: false,
819 maskClosable: true,
820 action: [],
821 showAction: [],
822 hideAction: [],
823 autoDestroy: false
824 });
825 return Trigger;
826}
827
828var _default = generateTrigger(_Portal.default);
829
830exports.default = _default;
\No newline at end of file