HookModal.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use client";
  2. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  3. var __rest = this && this.__rest || function (s, e) {
  4. var t = {};
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  6. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  8. }
  9. return t;
  10. };
  11. import * as React from 'react';
  12. import { ConfigContext } from '../../config-provider';
  13. import defaultLocale from '../../locale/en_US';
  14. import useLocale from '../../locale/useLocale';
  15. import ConfirmDialog from '../ConfirmDialog';
  16. const HookModal = (_a, ref) => {
  17. var _b;
  18. var {
  19. afterClose: hookAfterClose,
  20. config
  21. } = _a,
  22. restProps = __rest(_a, ["afterClose", "config"]);
  23. const [open, setOpen] = React.useState(true);
  24. const [innerConfig, setInnerConfig] = React.useState(config);
  25. const {
  26. direction,
  27. getPrefixCls
  28. } = React.useContext(ConfigContext);
  29. const prefixCls = getPrefixCls('modal');
  30. const rootPrefixCls = getPrefixCls();
  31. const afterClose = () => {
  32. var _a;
  33. hookAfterClose();
  34. (_a = innerConfig.afterClose) === null || _a === void 0 ? void 0 : _a.call(innerConfig);
  35. };
  36. const close = (...args) => {
  37. var _a;
  38. setOpen(false);
  39. const triggerCancel = args.some(param => param === null || param === void 0 ? void 0 : param.triggerCancel);
  40. if (triggerCancel) {
  41. var _a2;
  42. (_a = innerConfig.onCancel) === null || _a === void 0 ? void 0 : (_a2 = _a).call.apply(_a2, [innerConfig, () => {}].concat(_toConsumableArray(args.slice(1))));
  43. }
  44. };
  45. React.useImperativeHandle(ref, () => ({
  46. destroy: close,
  47. update: newConfig => {
  48. setInnerConfig(originConfig => {
  49. const nextConfig = typeof newConfig === 'function' ? newConfig(originConfig) : newConfig;
  50. return Object.assign(Object.assign({}, originConfig), nextConfig);
  51. });
  52. }
  53. }));
  54. const mergedOkCancel = (_b = innerConfig.okCancel) !== null && _b !== void 0 ? _b : innerConfig.type === 'confirm';
  55. const [contextLocale] = useLocale('Modal', defaultLocale.Modal);
  56. return /*#__PURE__*/React.createElement(ConfirmDialog, Object.assign({
  57. prefixCls: prefixCls,
  58. rootPrefixCls: rootPrefixCls
  59. }, innerConfig, {
  60. close: close,
  61. open: open,
  62. afterClose: afterClose,
  63. okText: innerConfig.okText || (mergedOkCancel ? contextLocale === null || contextLocale === void 0 ? void 0 : contextLocale.okText : contextLocale === null || contextLocale === void 0 ? void 0 : contextLocale.justOkText),
  64. direction: innerConfig.direction || direction,
  65. cancelText: innerConfig.cancelText || (contextLocale === null || contextLocale === void 0 ? void 0 : contextLocale.cancelText)
  66. }, restProps));
  67. };
  68. export default /*#__PURE__*/React.forwardRef(HookModal);