index.js
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import * as React from 'react';
import usePatchElement from '../../_util/hooks/usePatchElement';
import HookModal from './HookModal';
import { withConfirm, withInfo, withSuccess, withError, withWarn } from '../confirm';
var uuid = 0;
var ElementsHolder = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef(function (_props, ref) {
var _usePatchElement = usePatchElement(),
_usePatchElement2 = _slicedToArray(_usePatchElement, 2),
elements = _usePatchElement2[0],
patchElement = _usePatchElement2[1];
React.useImperativeHandle(ref, function () {
return {
patchElement: patchElement
};
}, []);
return /*#__PURE__*/React.createElement(React.Fragment, null, elements);
}));
export default function useModal() {
var holderRef = React.useRef(null);
var getConfirmFunc = React.useCallback(function (withFunc) {
return function hookConfirm(config) {
var _a;
uuid += 1;
var modalRef = /*#__PURE__*/React.createRef();
var closeFunc;
var modal = /*#__PURE__*/React.createElement(HookModal, {
key: "modal-".concat(uuid),
config: withFunc(config),
ref: modalRef,
afterClose: function afterClose() {
closeFunc();
}
});
closeFunc = (_a = holderRef.current) === null || _a === void 0 ? void 0 : _a.patchElement(modal);
return {
destroy: function destroy() {
if (modalRef.current) {
modalRef.current.destroy();
}
},
update: function update(newConfig) {
if (modalRef.current) {
modalRef.current.update(newConfig);
}
}
};
};
}, []);
var fns = React.useMemo(function () {
return {
info: getConfirmFunc(withInfo),
success: getConfirmFunc(withSuccess),
error: getConfirmFunc(withError),
warning: getConfirmFunc(withWarn),
confirm: getConfirmFunc(withConfirm)
};
}, []); // eslint-disable-next-line react/jsx-key
return [fns, /*#__PURE__*/React.createElement(ElementsHolder, {
ref: holderRef
})];
}