1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.default = void 0;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
15 |
|
16 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
17 |
|
18 | var React = _interopRequireWildcard(require("react"));
|
19 |
|
20 | var _rcAlign = _interopRequireDefault(require("rc-align"));
|
21 |
|
22 | var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
|
23 |
|
24 | var _rcMotion = _interopRequireDefault(require("rc-motion"));
|
25 |
|
26 | var _classnames = _interopRequireDefault(require("classnames"));
|
27 |
|
28 | var _useVisibleStatus3 = _interopRequireDefault(require("./useVisibleStatus"));
|
29 |
|
30 | var _legacyUtil = require("../utils/legacyUtil");
|
31 |
|
32 | var _useStretchStyle3 = _interopRequireDefault(require("./useStretchStyle"));
|
33 |
|
34 | var PopupInner = React.forwardRef(function (props, ref) {
|
35 | var visible = props.visible,
|
36 | prefixCls = props.prefixCls,
|
37 | className = props.className,
|
38 | style = props.style,
|
39 | children = props.children,
|
40 | zIndex = props.zIndex,
|
41 | stretch = props.stretch,
|
42 | destroyPopupOnHide = props.destroyPopupOnHide,
|
43 | forceRender = props.forceRender,
|
44 | align = props.align,
|
45 | point = props.point,
|
46 | getRootDomNode = props.getRootDomNode,
|
47 | getClassNameFromAlign = props.getClassNameFromAlign,
|
48 | onAlign = props.onAlign,
|
49 | onMouseEnter = props.onMouseEnter,
|
50 | onMouseLeave = props.onMouseLeave,
|
51 | onMouseDown = props.onMouseDown,
|
52 | onTouchStart = props.onTouchStart,
|
53 | onClick = props.onClick;
|
54 | var alignRef = (0, React.useRef)();
|
55 | var elementRef = (0, React.useRef)();
|
56 |
|
57 | var _useState = (0, React.useState)(),
|
58 | _useState2 = (0, _slicedToArray2.default)(_useState, 2),
|
59 | alignedClassName = _useState2[0],
|
60 | setAlignedClassName = _useState2[1];
|
61 |
|
62 |
|
63 | var _useStretchStyle = (0, _useStretchStyle3.default)(stretch),
|
64 | _useStretchStyle2 = (0, _slicedToArray2.default)(_useStretchStyle, 2),
|
65 | stretchStyle = _useStretchStyle2[0],
|
66 | measureStretchStyle = _useStretchStyle2[1];
|
67 |
|
68 | function doMeasure() {
|
69 | if (stretch) {
|
70 | measureStretchStyle(getRootDomNode());
|
71 | }
|
72 | }
|
73 |
|
74 |
|
75 | var _useVisibleStatus = (0, _useVisibleStatus3.default)(visible, doMeasure),
|
76 | _useVisibleStatus2 = (0, _slicedToArray2.default)(_useVisibleStatus, 2),
|
77 | status = _useVisibleStatus2[0],
|
78 | goNextStatus = _useVisibleStatus2[1];
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | var _useState3 = (0, React.useState)(0),
|
96 | _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
|
97 | alignTimes = _useState4[0],
|
98 | setAlignTimes = _useState4[1];
|
99 |
|
100 | var prepareResolveRef = (0, React.useRef)();
|
101 | (0, _useLayoutEffect.default)(function () {
|
102 | if (status === 'alignPre') {
|
103 | setAlignTimes(0);
|
104 | }
|
105 | }, [status]);
|
106 |
|
107 |
|
108 | function getAlignTarget() {
|
109 | if (point) {
|
110 | return point;
|
111 | }
|
112 |
|
113 | return getRootDomNode;
|
114 | }
|
115 |
|
116 | function forceAlign() {
|
117 | var _alignRef$current;
|
118 |
|
119 | (_alignRef$current = alignRef.current) === null || _alignRef$current === void 0 ? void 0 : _alignRef$current.forceAlign();
|
120 | }
|
121 |
|
122 | function onInternalAlign(popupDomNode, matchAlign) {
|
123 | var nextAlignedClassName = getClassNameFromAlign(matchAlign);
|
124 |
|
125 | if (alignedClassName !== nextAlignedClassName) {
|
126 | setAlignedClassName(nextAlignedClassName);
|
127 | }
|
128 |
|
129 |
|
130 | setAlignTimes(function (val) {
|
131 | return val + 1;
|
132 | });
|
133 |
|
134 | if (status === 'align') {
|
135 | onAlign === null || onAlign === void 0 ? void 0 : onAlign(popupDomNode, matchAlign);
|
136 | }
|
137 | }
|
138 |
|
139 |
|
140 | (0, _useLayoutEffect.default)(function () {
|
141 | if (status === 'align') {
|
142 |
|
143 | if (alignTimes < 3) {
|
144 | forceAlign();
|
145 | } else {
|
146 | goNextStatus(function () {
|
147 | var _prepareResolveRef$cu;
|
148 |
|
149 | (_prepareResolveRef$cu = prepareResolveRef.current) === null || _prepareResolveRef$cu === void 0 ? void 0 : _prepareResolveRef$cu.call(prepareResolveRef);
|
150 | });
|
151 | }
|
152 | }
|
153 | }, [alignTimes]);
|
154 |
|
155 | var motion = (0, _objectSpread2.default)({}, (0, _legacyUtil.getMotion)(props));
|
156 | ['onAppearEnd', 'onEnterEnd', 'onLeaveEnd'].forEach(function (eventName) {
|
157 | var originHandler = motion[eventName];
|
158 |
|
159 | motion[eventName] = function (element, event) {
|
160 | goNextStatus();
|
161 | return originHandler === null || originHandler === void 0 ? void 0 : originHandler(element, event);
|
162 | };
|
163 | });
|
164 |
|
165 | function onShowPrepare() {
|
166 | return new Promise(function (resolve) {
|
167 | prepareResolveRef.current = resolve;
|
168 | });
|
169 | }
|
170 |
|
171 |
|
172 | React.useEffect(function () {
|
173 | if (!motion.motionName && status === 'motion') {
|
174 | goNextStatus();
|
175 | }
|
176 | }, [motion.motionName, status]);
|
177 |
|
178 | React.useImperativeHandle(ref, function () {
|
179 | return {
|
180 | forceAlign: forceAlign,
|
181 | getElement: function getElement() {
|
182 | return elementRef.current;
|
183 | }
|
184 | };
|
185 | });
|
186 |
|
187 | var mergedStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, stretchStyle), {}, {
|
188 | zIndex: zIndex,
|
189 | opacity: status === 'motion' || status === 'stable' || !visible ? undefined : 0,
|
190 |
|
191 |
|
192 | pointerEvents: !visible && status !== 'stable' ? 'none' : undefined
|
193 | }, style);
|
194 |
|
195 | var alignDisabled = true;
|
196 |
|
197 | if (align !== null && align !== void 0 && align.points && (status === 'align' || status === 'stable')) {
|
198 | alignDisabled = false;
|
199 | }
|
200 |
|
201 | var childNode = children;
|
202 |
|
203 | if (React.Children.count(children) > 1) {
|
204 | childNode = React.createElement("div", {
|
205 | className: "".concat(prefixCls, "-content")
|
206 | }, children);
|
207 | }
|
208 |
|
209 | return React.createElement(_rcMotion.default, (0, _extends2.default)({
|
210 | visible: visible,
|
211 | ref: elementRef,
|
212 | leavedClassName: "".concat(prefixCls, "-hidden")
|
213 | }, motion, {
|
214 | onAppearPrepare: onShowPrepare,
|
215 | onEnterPrepare: onShowPrepare,
|
216 | removeOnLeave: destroyPopupOnHide,
|
217 | forceRender: forceRender
|
218 | }), function (_ref, motionRef) {
|
219 | var motionClassName = _ref.className,
|
220 | motionStyle = _ref.style;
|
221 | var mergedClassName = (0, _classnames.default)(prefixCls, className, alignedClassName, motionClassName);
|
222 | return React.createElement(_rcAlign.default, {
|
223 | target: getAlignTarget(),
|
224 | key: "popup",
|
225 | ref: alignRef,
|
226 | monitorWindowResize: true,
|
227 | disabled: alignDisabled,
|
228 | align: align,
|
229 | onAlign: onInternalAlign
|
230 | }, React.createElement("div", {
|
231 | ref: motionRef,
|
232 | className: mergedClassName,
|
233 | onMouseEnter: onMouseEnter,
|
234 | onMouseLeave: onMouseLeave,
|
235 | onMouseDownCapture: onMouseDown,
|
236 | onTouchStartCapture: onTouchStart,
|
237 | onClick: onClick,
|
238 | style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), mergedStyle)
|
239 | }, childNode));
|
240 | });
|
241 | });
|
242 | PopupInner.displayName = 'PopupInner';
|
243 | var _default = PopupInner;
|
244 | exports.default = _default; |
\ | No newline at end of file |