1 | function _typeof(obj) { "@babel/helpers - typeof"; 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); }
|
2 |
|
3 | 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; }
|
4 |
|
5 | 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; }
|
6 |
|
7 | 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; }
|
8 |
|
9 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
10 |
|
11 | 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); } }
|
12 |
|
13 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
14 |
|
15 | 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); }
|
16 |
|
17 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
18 |
|
19 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function () { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
20 |
|
21 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
22 |
|
23 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
24 |
|
25 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
|
26 |
|
27 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
28 |
|
29 | import React from 'react';
|
30 | import ReactDOM from 'react-dom';
|
31 | import contains from "rc-util/es/Dom/contains";
|
32 | import findDOMNode from "rc-util/es/Dom/findDOMNode";
|
33 | import { composeRef, supportRef } from "rc-util/es/ref";
|
34 | import addEventListener from "rc-util/es/Dom/addEventListener";
|
35 | import Portal from "rc-util/es/Portal";
|
36 | import classNames from 'classnames';
|
37 | import { getAlignFromPlacement, getAlignPopupClassName } from './utils/alignUtil';
|
38 | import Popup from './Popup';
|
39 | import TriggerContext from './context';
|
40 |
|
41 | function noop() {}
|
42 |
|
43 | function returnEmptyString() {
|
44 | return '';
|
45 | }
|
46 |
|
47 | function returnDocument() {
|
48 | return window.document;
|
49 | }
|
50 |
|
51 | var ALL_HANDLERS = ['onClick', 'onMouseDown', 'onTouchStart', 'onMouseEnter', 'onMouseLeave', 'onFocus', 'onBlur', 'onContextMenu'];
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | export function generateTrigger(PortalComponent) {
|
57 | var Trigger = function (_React$Component) {
|
58 | _inherits(Trigger, _React$Component);
|
59 |
|
60 | var _super = _createSuper(Trigger);
|
61 |
|
62 | function Trigger(props) {
|
63 | var _this;
|
64 |
|
65 | _classCallCheck(this, Trigger);
|
66 |
|
67 | _this = _super.call(this, props);
|
68 | _this.popupRef = React.createRef();
|
69 | _this.triggerRef = React.createRef();
|
70 |
|
71 | _this.onMouseEnter = function (e) {
|
72 | var mouseEnterDelay = _this.props.mouseEnterDelay;
|
73 |
|
74 | _this.fireEvents('onMouseEnter', e);
|
75 |
|
76 | _this.delaySetPopupVisible(true, mouseEnterDelay, mouseEnterDelay ? null : e);
|
77 | };
|
78 |
|
79 | _this.onMouseMove = function (e) {
|
80 | _this.fireEvents('onMouseMove', e);
|
81 |
|
82 | _this.setPoint(e);
|
83 | };
|
84 |
|
85 | _this.onMouseLeave = function (e) {
|
86 | _this.fireEvents('onMouseLeave', e);
|
87 |
|
88 | _this.delaySetPopupVisible(false, _this.props.mouseLeaveDelay);
|
89 | };
|
90 |
|
91 | _this.onPopupMouseEnter = function () {
|
92 | _this.clearDelayTimer();
|
93 | };
|
94 |
|
95 | _this.onPopupMouseLeave = function (e) {
|
96 |
|
97 |
|
98 | if (e.relatedTarget && !e.relatedTarget.setTimeout && _this.popupRef.current && _this.popupRef.current.popupRef.current && contains(_this.popupRef.current.popupRef.current, e.relatedTarget)) {
|
99 | return;
|
100 | }
|
101 |
|
102 | _this.delaySetPopupVisible(false, _this.props.mouseLeaveDelay);
|
103 | };
|
104 |
|
105 | _this.onFocus = function (e) {
|
106 | _this.fireEvents('onFocus', e);
|
107 |
|
108 |
|
109 | _this.clearDelayTimer();
|
110 |
|
111 | if (_this.isFocusToShow()) {
|
112 | _this.focusTime = Date.now();
|
113 |
|
114 | _this.delaySetPopupVisible(true, _this.props.focusDelay);
|
115 | }
|
116 | };
|
117 |
|
118 | _this.onMouseDown = function (e) {
|
119 | _this.fireEvents('onMouseDown', e);
|
120 |
|
121 | _this.preClickTime = Date.now();
|
122 | };
|
123 |
|
124 | _this.onTouchStart = function (e) {
|
125 | _this.fireEvents('onTouchStart', e);
|
126 |
|
127 | _this.preTouchTime = Date.now();
|
128 | };
|
129 |
|
130 | _this.onBlur = function (e) {
|
131 | _this.fireEvents('onBlur', e);
|
132 |
|
133 | _this.clearDelayTimer();
|
134 |
|
135 | if (_this.isBlurToHide()) {
|
136 | _this.delaySetPopupVisible(false, _this.props.blurDelay);
|
137 | }
|
138 | };
|
139 |
|
140 | _this.onContextMenu = function (e) {
|
141 | e.preventDefault();
|
142 |
|
143 | _this.fireEvents('onContextMenu', e);
|
144 |
|
145 | _this.setPopupVisible(true, e);
|
146 | };
|
147 |
|
148 | _this.onContextMenuClose = function () {
|
149 | if (_this.isContextMenuToShow()) {
|
150 | _this.close();
|
151 | }
|
152 | };
|
153 |
|
154 | _this.onClick = function (event) {
|
155 | _this.fireEvents('onClick', event);
|
156 |
|
157 |
|
158 | if (_this.focusTime) {
|
159 | var preTime;
|
160 |
|
161 | if (_this.preClickTime && _this.preTouchTime) {
|
162 | preTime = Math.min(_this.preClickTime, _this.preTouchTime);
|
163 | } else if (_this.preClickTime) {
|
164 | preTime = _this.preClickTime;
|
165 | } else if (_this.preTouchTime) {
|
166 | preTime = _this.preTouchTime;
|
167 | }
|
168 |
|
169 | if (Math.abs(preTime - _this.focusTime) < 20) {
|
170 | return;
|
171 | }
|
172 |
|
173 | _this.focusTime = 0;
|
174 | }
|
175 |
|
176 | _this.preClickTime = 0;
|
177 | _this.preTouchTime = 0;
|
178 |
|
179 |
|
180 |
|
181 | if (_this.isClickToShow() && (_this.isClickToHide() || _this.isBlurToHide()) && event && event.preventDefault) {
|
182 | event.preventDefault();
|
183 | }
|
184 |
|
185 | var nextVisible = !_this.state.popupVisible;
|
186 |
|
187 | if (_this.isClickToHide() && !nextVisible || nextVisible && _this.isClickToShow()) {
|
188 | _this.setPopupVisible(!_this.state.popupVisible, event);
|
189 | }
|
190 | };
|
191 |
|
192 | _this.onPopupMouseDown = function () {
|
193 | _this.hasPopupMouseDown = true;
|
194 | clearTimeout(_this.mouseDownTimeout);
|
195 | _this.mouseDownTimeout = window.setTimeout(function () {
|
196 | _this.hasPopupMouseDown = false;
|
197 | }, 0);
|
198 |
|
199 | if (_this.context) {
|
200 | var _this$context;
|
201 |
|
202 | (_this$context = _this.context).onPopupMouseDown.apply(_this$context, arguments);
|
203 | }
|
204 | };
|
205 |
|
206 | _this.onDocumentClick = function (event) {
|
207 | if (_this.props.mask && !_this.props.maskClosable) {
|
208 | return;
|
209 | }
|
210 |
|
211 | var target = event.target;
|
212 |
|
213 | var root = _this.getRootDomNode();
|
214 |
|
215 | var popupNode = _this.getPopupDomNode();
|
216 |
|
217 | if (!contains(root, target) && !contains(popupNode, target) && !_this.hasPopupMouseDown) {
|
218 | _this.close();
|
219 | }
|
220 | };
|
221 |
|
222 | _this.getRootDomNode = function () {
|
223 | var getTriggerDOMNode = _this.props.getTriggerDOMNode;
|
224 |
|
225 | if (getTriggerDOMNode) {
|
226 | return getTriggerDOMNode(_this.triggerRef.current);
|
227 | }
|
228 |
|
229 | try {
|
230 | var domNode = findDOMNode(_this.triggerRef.current);
|
231 |
|
232 | if (domNode) {
|
233 | return domNode;
|
234 | }
|
235 | } catch (err) {
|
236 | }
|
237 |
|
238 | return ReactDOM.findDOMNode(_assertThisInitialized(_this));
|
239 | };
|
240 |
|
241 | _this.getPopupClassNameFromAlign = function (align) {
|
242 | var className = [];
|
243 | var _this$props = _this.props,
|
244 | popupPlacement = _this$props.popupPlacement,
|
245 | builtinPlacements = _this$props.builtinPlacements,
|
246 | prefixCls = _this$props.prefixCls,
|
247 | alignPoint = _this$props.alignPoint,
|
248 | getPopupClassNameFromAlign = _this$props.getPopupClassNameFromAlign;
|
249 |
|
250 | if (popupPlacement && builtinPlacements) {
|
251 | className.push(getAlignPopupClassName(builtinPlacements, prefixCls, align, alignPoint));
|
252 | }
|
253 |
|
254 | if (getPopupClassNameFromAlign) {
|
255 | className.push(getPopupClassNameFromAlign(align));
|
256 | }
|
257 |
|
258 | return className.join(' ');
|
259 | };
|
260 |
|
261 | _this.getComponent = function () {
|
262 | var _this$props2 = _this.props,
|
263 | prefixCls = _this$props2.prefixCls,
|
264 | destroyPopupOnHide = _this$props2.destroyPopupOnHide,
|
265 | popupClassName = _this$props2.popupClassName,
|
266 | onPopupAlign = _this$props2.onPopupAlign,
|
267 | popupMotion = _this$props2.popupMotion,
|
268 | popupAnimation = _this$props2.popupAnimation,
|
269 | popupTransitionName = _this$props2.popupTransitionName,
|
270 | popupStyle = _this$props2.popupStyle,
|
271 | mask = _this$props2.mask,
|
272 | maskAnimation = _this$props2.maskAnimation,
|
273 | maskTransitionName = _this$props2.maskTransitionName,
|
274 | maskMotion = _this$props2.maskMotion,
|
275 | zIndex = _this$props2.zIndex,
|
276 | popup = _this$props2.popup,
|
277 | stretch = _this$props2.stretch,
|
278 | alignPoint = _this$props2.alignPoint;
|
279 | var _this$state = _this.state,
|
280 | popupVisible = _this$state.popupVisible,
|
281 | point = _this$state.point;
|
282 |
|
283 | var align = _this.getPopupAlign();
|
284 |
|
285 | var mouseProps = {};
|
286 |
|
287 | if (_this.isMouseEnterToShow()) {
|
288 | mouseProps.onMouseEnter = _this.onPopupMouseEnter;
|
289 | }
|
290 |
|
291 | if (_this.isMouseLeaveToHide()) {
|
292 | mouseProps.onMouseLeave = _this.onPopupMouseLeave;
|
293 | }
|
294 |
|
295 | mouseProps.onMouseDown = _this.onPopupMouseDown;
|
296 | mouseProps.onTouchStart = _this.onPopupMouseDown;
|
297 | return React.createElement(Popup, Object.assign({
|
298 | prefixCls: prefixCls,
|
299 | destroyPopupOnHide: destroyPopupOnHide,
|
300 | visible: popupVisible,
|
301 | point: alignPoint && point,
|
302 | className: popupClassName,
|
303 | align: align,
|
304 | onAlign: onPopupAlign,
|
305 | animation: popupAnimation,
|
306 | getClassNameFromAlign: _this.getPopupClassNameFromAlign
|
307 | }, mouseProps, {
|
308 | stretch: stretch,
|
309 | getRootDomNode: _this.getRootDomNode,
|
310 | style: popupStyle,
|
311 | mask: mask,
|
312 | zIndex: zIndex,
|
313 | transitionName: popupTransitionName,
|
314 | maskAnimation: maskAnimation,
|
315 | maskTransitionName: maskTransitionName,
|
316 | maskMotion: maskMotion,
|
317 | ref: _this.popupRef,
|
318 | motion: popupMotion
|
319 | }), typeof popup === 'function' ? popup() : popup);
|
320 | };
|
321 |
|
322 | _this.getContainer = function () {
|
323 | var _assertThisInitialize = _assertThisInitialized(_this),
|
324 | props = _assertThisInitialize.props;
|
325 |
|
326 | var popupContainer = document.createElement('div');
|
327 |
|
328 |
|
329 | popupContainer.style.position = 'absolute';
|
330 | popupContainer.style.top = '0';
|
331 | popupContainer.style.left = '0';
|
332 | popupContainer.style.width = '100%';
|
333 | var mountNode = props.getPopupContainer ? props.getPopupContainer(_this.getRootDomNode()) : props.getDocument().body;
|
334 | mountNode.appendChild(popupContainer);
|
335 | return popupContainer;
|
336 | };
|
337 |
|
338 | _this.setPoint = function (point) {
|
339 | var alignPoint = _this.props.alignPoint;
|
340 | if (!alignPoint || !point) return;
|
341 |
|
342 | _this.setState({
|
343 | point: {
|
344 | pageX: point.pageX,
|
345 | pageY: point.pageY
|
346 | }
|
347 | });
|
348 | };
|
349 |
|
350 | _this.handlePortalUpdate = function () {
|
351 | if (_this.state.prevPopupVisible !== _this.state.popupVisible) {
|
352 | _this.props.afterPopupVisibleChange(_this.state.popupVisible);
|
353 | }
|
354 | };
|
355 |
|
356 | var popupVisible;
|
357 |
|
358 | if ('popupVisible' in props) {
|
359 | popupVisible = !!props.popupVisible;
|
360 | } else {
|
361 | popupVisible = !!props.defaultPopupVisible;
|
362 | }
|
363 |
|
364 | _this.state = {
|
365 | prevPopupVisible: popupVisible,
|
366 | popupVisible: popupVisible
|
367 | };
|
368 | ALL_HANDLERS.forEach(function (h) {
|
369 | _this["fire".concat(h)] = function (e) {
|
370 | _this.fireEvents(h, e);
|
371 | };
|
372 | });
|
373 | return _this;
|
374 | }
|
375 |
|
376 | _createClass(Trigger, [{
|
377 | key: "componentDidMount",
|
378 | value: function componentDidMount() {
|
379 | this.componentDidUpdate();
|
380 | }
|
381 | }, {
|
382 | key: "componentDidUpdate",
|
383 | value: function componentDidUpdate() {
|
384 | var props = this.props;
|
385 | var state = this.state;
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | if (state.popupVisible) {
|
391 | var currentDocument;
|
392 |
|
393 | if (!this.clickOutsideHandler && (this.isClickToHide() || this.isContextMenuToShow())) {
|
394 | currentDocument = props.getDocument();
|
395 | this.clickOutsideHandler = addEventListener(currentDocument, 'mousedown', this.onDocumentClick);
|
396 | }
|
397 |
|
398 |
|
399 | if (!this.touchOutsideHandler) {
|
400 | currentDocument = currentDocument || props.getDocument();
|
401 | this.touchOutsideHandler = addEventListener(currentDocument, 'touchstart', this.onDocumentClick);
|
402 | }
|
403 |
|
404 |
|
405 | if (!this.contextMenuOutsideHandler1 && this.isContextMenuToShow()) {
|
406 | currentDocument = currentDocument || props.getDocument();
|
407 | this.contextMenuOutsideHandler1 = addEventListener(currentDocument, 'scroll', this.onContextMenuClose);
|
408 | }
|
409 |
|
410 |
|
411 | if (!this.contextMenuOutsideHandler2 && this.isContextMenuToShow()) {
|
412 | this.contextMenuOutsideHandler2 = addEventListener(window, 'blur', this.onContextMenuClose);
|
413 | }
|
414 |
|
415 | return;
|
416 | }
|
417 |
|
418 | this.clearOutsideHandler();
|
419 | }
|
420 | }, {
|
421 | key: "componentWillUnmount",
|
422 | value: function componentWillUnmount() {
|
423 | this.clearDelayTimer();
|
424 | this.clearOutsideHandler();
|
425 | clearTimeout(this.mouseDownTimeout);
|
426 | }
|
427 | }, {
|
428 | key: "getPopupDomNode",
|
429 | value: function getPopupDomNode() {
|
430 |
|
431 | if (this.popupRef.current && this.popupRef.current.popupRef.current) {
|
432 | return this.popupRef.current.popupRef.current;
|
433 | }
|
434 |
|
435 | return null;
|
436 | }
|
437 | }, {
|
438 | key: "getPopupAlign",
|
439 | value: function getPopupAlign() {
|
440 | var props = this.props;
|
441 | var popupPlacement = props.popupPlacement,
|
442 | popupAlign = props.popupAlign,
|
443 | builtinPlacements = props.builtinPlacements;
|
444 |
|
445 | if (popupPlacement && builtinPlacements) {
|
446 | return getAlignFromPlacement(builtinPlacements, popupPlacement, popupAlign);
|
447 | }
|
448 |
|
449 | return popupAlign;
|
450 | }
|
451 | |
452 |
|
453 |
|
454 |
|
455 |
|
456 | }, {
|
457 | key: "setPopupVisible",
|
458 | value: function setPopupVisible(popupVisible, event) {
|
459 | var alignPoint = this.props.alignPoint;
|
460 | var prevPopupVisible = this.state.popupVisible;
|
461 | this.clearDelayTimer();
|
462 |
|
463 | if (prevPopupVisible !== popupVisible) {
|
464 | if (!('popupVisible' in this.props)) {
|
465 | this.setState({
|
466 | popupVisible: popupVisible,
|
467 | prevPopupVisible: prevPopupVisible
|
468 | });
|
469 | }
|
470 |
|
471 | this.props.onPopupVisibleChange(popupVisible);
|
472 | }
|
473 |
|
474 |
|
475 | if (alignPoint && event) {
|
476 | this.setPoint(event);
|
477 | }
|
478 | }
|
479 | }, {
|
480 | key: "delaySetPopupVisible",
|
481 | value: function delaySetPopupVisible(visible, delayS, event) {
|
482 | var _this2 = this;
|
483 |
|
484 | var delay = delayS * 1000;
|
485 | this.clearDelayTimer();
|
486 |
|
487 | if (delay) {
|
488 | var point = event ? {
|
489 | pageX: event.pageX,
|
490 | pageY: event.pageY
|
491 | } : null;
|
492 | this.delayTimer = window.setTimeout(function () {
|
493 | _this2.setPopupVisible(visible, point);
|
494 |
|
495 | _this2.clearDelayTimer();
|
496 | }, delay);
|
497 | } else {
|
498 | this.setPopupVisible(visible, event);
|
499 | }
|
500 | }
|
501 | }, {
|
502 | key: "clearDelayTimer",
|
503 | value: function clearDelayTimer() {
|
504 | if (this.delayTimer) {
|
505 | clearTimeout(this.delayTimer);
|
506 | this.delayTimer = null;
|
507 | }
|
508 | }
|
509 | }, {
|
510 | key: "clearOutsideHandler",
|
511 | value: function clearOutsideHandler() {
|
512 | if (this.clickOutsideHandler) {
|
513 | this.clickOutsideHandler.remove();
|
514 | this.clickOutsideHandler = null;
|
515 | }
|
516 |
|
517 | if (this.contextMenuOutsideHandler1) {
|
518 | this.contextMenuOutsideHandler1.remove();
|
519 | this.contextMenuOutsideHandler1 = null;
|
520 | }
|
521 |
|
522 | if (this.contextMenuOutsideHandler2) {
|
523 | this.contextMenuOutsideHandler2.remove();
|
524 | this.contextMenuOutsideHandler2 = null;
|
525 | }
|
526 |
|
527 | if (this.touchOutsideHandler) {
|
528 | this.touchOutsideHandler.remove();
|
529 | this.touchOutsideHandler = null;
|
530 | }
|
531 | }
|
532 | }, {
|
533 | key: "createTwoChains",
|
534 | value: function createTwoChains(event) {
|
535 | var childPros = this.props.children.props;
|
536 | var props = this.props;
|
537 |
|
538 | if (childPros[event] && props[event]) {
|
539 | return this["fire".concat(event)];
|
540 | }
|
541 |
|
542 | return childPros[event] || props[event];
|
543 | }
|
544 | }, {
|
545 | key: "isClickToShow",
|
546 | value: function isClickToShow() {
|
547 | var _this$props3 = this.props,
|
548 | action = _this$props3.action,
|
549 | showAction = _this$props3.showAction;
|
550 | return action.indexOf('click') !== -1 || showAction.indexOf('click') !== -1;
|
551 | }
|
552 | }, {
|
553 | key: "isContextMenuToShow",
|
554 | value: function isContextMenuToShow() {
|
555 | var _this$props4 = this.props,
|
556 | action = _this$props4.action,
|
557 | showAction = _this$props4.showAction;
|
558 | return action.indexOf('contextMenu') !== -1 || showAction.indexOf('contextMenu') !== -1;
|
559 | }
|
560 | }, {
|
561 | key: "isClickToHide",
|
562 | value: function isClickToHide() {
|
563 | var _this$props5 = this.props,
|
564 | action = _this$props5.action,
|
565 | hideAction = _this$props5.hideAction;
|
566 | return action.indexOf('click') !== -1 || hideAction.indexOf('click') !== -1;
|
567 | }
|
568 | }, {
|
569 | key: "isMouseEnterToShow",
|
570 | value: function isMouseEnterToShow() {
|
571 | var _this$props6 = this.props,
|
572 | action = _this$props6.action,
|
573 | showAction = _this$props6.showAction;
|
574 | return action.indexOf('hover') !== -1 || showAction.indexOf('mouseEnter') !== -1;
|
575 | }
|
576 | }, {
|
577 | key: "isMouseLeaveToHide",
|
578 | value: function isMouseLeaveToHide() {
|
579 | var _this$props7 = this.props,
|
580 | action = _this$props7.action,
|
581 | hideAction = _this$props7.hideAction;
|
582 | return action.indexOf('hover') !== -1 || hideAction.indexOf('mouseLeave') !== -1;
|
583 | }
|
584 | }, {
|
585 | key: "isFocusToShow",
|
586 | value: function isFocusToShow() {
|
587 | var _this$props8 = this.props,
|
588 | action = _this$props8.action,
|
589 | showAction = _this$props8.showAction;
|
590 | return action.indexOf('focus') !== -1 || showAction.indexOf('focus') !== -1;
|
591 | }
|
592 | }, {
|
593 | key: "isBlurToHide",
|
594 | value: function isBlurToHide() {
|
595 | var _this$props9 = this.props,
|
596 | action = _this$props9.action,
|
597 | hideAction = _this$props9.hideAction;
|
598 | return action.indexOf('focus') !== -1 || hideAction.indexOf('blur') !== -1;
|
599 | }
|
600 | }, {
|
601 | key: "forcePopupAlign",
|
602 | value: function forcePopupAlign() {
|
603 | if (this.state.popupVisible && this.popupRef.current && this.popupRef.current.alignRef.current) {
|
604 | this.popupRef.current.alignRef.current.forceAlign();
|
605 | }
|
606 | }
|
607 | }, {
|
608 | key: "fireEvents",
|
609 | value: function fireEvents(type, e) {
|
610 | var childCallback = this.props.children.props[type];
|
611 |
|
612 | if (childCallback) {
|
613 | childCallback(e);
|
614 | }
|
615 |
|
616 | var callback = this.props[type];
|
617 |
|
618 | if (callback) {
|
619 | callback(e);
|
620 | }
|
621 | }
|
622 | }, {
|
623 | key: "close",
|
624 | value: function close() {
|
625 | this.setPopupVisible(false);
|
626 | }
|
627 | }, {
|
628 | key: "render",
|
629 | value: function render() {
|
630 | var popupVisible = this.state.popupVisible;
|
631 | var _this$props10 = this.props,
|
632 | children = _this$props10.children,
|
633 | forceRender = _this$props10.forceRender,
|
634 | alignPoint = _this$props10.alignPoint,
|
635 | className = _this$props10.className,
|
636 | autoDestroy = _this$props10.autoDestroy;
|
637 | var child = React.Children.only(children);
|
638 | var newChildProps = {
|
639 | key: 'trigger'
|
640 | };
|
641 |
|
642 | if (this.isContextMenuToShow()) {
|
643 | newChildProps.onContextMenu = this.onContextMenu;
|
644 | } else {
|
645 | newChildProps.onContextMenu = this.createTwoChains('onContextMenu');
|
646 | }
|
647 |
|
648 | if (this.isClickToHide() || this.isClickToShow()) {
|
649 | newChildProps.onClick = this.onClick;
|
650 | newChildProps.onMouseDown = this.onMouseDown;
|
651 | newChildProps.onTouchStart = this.onTouchStart;
|
652 | } else {
|
653 | newChildProps.onClick = this.createTwoChains('onClick');
|
654 | newChildProps.onMouseDown = this.createTwoChains('onMouseDown');
|
655 | newChildProps.onTouchStart = this.createTwoChains('onTouchStart');
|
656 | }
|
657 |
|
658 | if (this.isMouseEnterToShow()) {
|
659 | newChildProps.onMouseEnter = this.onMouseEnter;
|
660 |
|
661 | if (alignPoint) {
|
662 | newChildProps.onMouseMove = this.onMouseMove;
|
663 | }
|
664 | } else {
|
665 | newChildProps.onMouseEnter = this.createTwoChains('onMouseEnter');
|
666 | }
|
667 |
|
668 | if (this.isMouseLeaveToHide()) {
|
669 | newChildProps.onMouseLeave = this.onMouseLeave;
|
670 | } else {
|
671 | newChildProps.onMouseLeave = this.createTwoChains('onMouseLeave');
|
672 | }
|
673 |
|
674 | if (this.isFocusToShow() || this.isBlurToHide()) {
|
675 | newChildProps.onFocus = this.onFocus;
|
676 | newChildProps.onBlur = this.onBlur;
|
677 | } else {
|
678 | newChildProps.onFocus = this.createTwoChains('onFocus');
|
679 | newChildProps.onBlur = this.createTwoChains('onBlur');
|
680 | }
|
681 |
|
682 | var childrenClassName = classNames(child && child.props && child.props.className, className);
|
683 |
|
684 | if (childrenClassName) {
|
685 | newChildProps.className = childrenClassName;
|
686 | }
|
687 |
|
688 | var cloneProps = _objectSpread({}, newChildProps);
|
689 |
|
690 | if (supportRef(child)) {
|
691 | cloneProps.ref = composeRef(this.triggerRef, child.ref);
|
692 | }
|
693 |
|
694 | var trigger = React.cloneElement(child, cloneProps);
|
695 | var portal;
|
696 |
|
697 | if (popupVisible || this.popupRef.current || forceRender) {
|
698 | portal = React.createElement(PortalComponent, {
|
699 | key: "portal",
|
700 | getContainer: this.getContainer,
|
701 | didUpdate: this.handlePortalUpdate
|
702 | }, this.getComponent());
|
703 | }
|
704 |
|
705 | if (!popupVisible && autoDestroy) {
|
706 | portal = null;
|
707 | }
|
708 |
|
709 | return React.createElement(TriggerContext.Provider, {
|
710 | value: {
|
711 | onPopupMouseDown: this.onPopupMouseDown
|
712 | }
|
713 | }, trigger, portal);
|
714 | }
|
715 | }], [{
|
716 | key: "getDerivedStateFromProps",
|
717 | value: function getDerivedStateFromProps(_ref, prevState) {
|
718 | var popupVisible = _ref.popupVisible;
|
719 | var newState = {};
|
720 |
|
721 | if (popupVisible !== undefined && prevState.popupVisible !== popupVisible) {
|
722 | newState.popupVisible = popupVisible;
|
723 | newState.prevPopupVisible = prevState.popupVisible;
|
724 | }
|
725 |
|
726 | return newState;
|
727 | }
|
728 | }]);
|
729 |
|
730 | return Trigger;
|
731 | }(React.Component);
|
732 |
|
733 | Trigger.contextType = TriggerContext;
|
734 | Trigger.defaultProps = {
|
735 | prefixCls: 'rc-trigger-popup',
|
736 | getPopupClassNameFromAlign: returnEmptyString,
|
737 | getDocument: returnDocument,
|
738 | onPopupVisibleChange: noop,
|
739 | afterPopupVisibleChange: noop,
|
740 | onPopupAlign: noop,
|
741 | popupClassName: '',
|
742 | mouseEnterDelay: 0,
|
743 | mouseLeaveDelay: 0.1,
|
744 | focusDelay: 0,
|
745 | blurDelay: 0.15,
|
746 | popupStyle: {},
|
747 | destroyPopupOnHide: false,
|
748 | popupAlign: {},
|
749 | defaultPopupVisible: false,
|
750 | mask: false,
|
751 | maskClosable: true,
|
752 | action: [],
|
753 | showAction: [],
|
754 | hideAction: [],
|
755 | autoDestroy: false
|
756 | };
|
757 | return Trigger;
|
758 | }
|
759 | export default generateTrigger(Portal); |
\ | No newline at end of file |