index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import { isValidElement } from 'react';
  12. import classNames from 'classnames';
  13. import useMergedState from "rc-util/es/hooks/useMergedState";
  14. import KeyCode from "rc-util/es/KeyCode";
  15. import { getRenderPropValue } from '../_util/getRenderPropValue';
  16. import { getTransitionName } from '../_util/motion';
  17. import { cloneElement } from '../_util/reactNode';
  18. import { useComponentConfig } from '../config-provider/context';
  19. import Tooltip from '../tooltip';
  20. import PurePanel, { Overlay } from './PurePanel';
  21. // CSSINJS
  22. import useStyle from './style';
  23. const InternalPopover = /*#__PURE__*/React.forwardRef((props, ref) => {
  24. var _a, _b;
  25. const {
  26. prefixCls: customizePrefixCls,
  27. title,
  28. content,
  29. overlayClassName,
  30. placement = 'top',
  31. trigger = 'hover',
  32. children,
  33. mouseEnterDelay = 0.1,
  34. mouseLeaveDelay = 0.1,
  35. onOpenChange,
  36. overlayStyle = {},
  37. styles,
  38. classNames: popoverClassNames
  39. } = props,
  40. otherProps = __rest(props, ["prefixCls", "title", "content", "overlayClassName", "placement", "trigger", "children", "mouseEnterDelay", "mouseLeaveDelay", "onOpenChange", "overlayStyle", "styles", "classNames"]);
  41. const {
  42. getPrefixCls,
  43. className: contextClassName,
  44. style: contextStyle,
  45. classNames: contextClassNames,
  46. styles: contextStyles
  47. } = useComponentConfig('popover');
  48. const prefixCls = getPrefixCls('popover', customizePrefixCls);
  49. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  50. const rootPrefixCls = getPrefixCls();
  51. const rootClassNames = classNames(overlayClassName, hashId, cssVarCls, contextClassName, contextClassNames.root, popoverClassNames === null || popoverClassNames === void 0 ? void 0 : popoverClassNames.root);
  52. const bodyClassNames = classNames(contextClassNames.body, popoverClassNames === null || popoverClassNames === void 0 ? void 0 : popoverClassNames.body);
  53. const [open, setOpen] = useMergedState(false, {
  54. value: (_a = props.open) !== null && _a !== void 0 ? _a : props.visible,
  55. defaultValue: (_b = props.defaultOpen) !== null && _b !== void 0 ? _b : props.defaultVisible
  56. });
  57. const settingOpen = (value, e) => {
  58. setOpen(value, true);
  59. onOpenChange === null || onOpenChange === void 0 ? void 0 : onOpenChange(value, e);
  60. };
  61. const onKeyDown = e => {
  62. if (e.keyCode === KeyCode.ESC) {
  63. settingOpen(false, e);
  64. }
  65. };
  66. const onInternalOpenChange = value => {
  67. settingOpen(value);
  68. };
  69. const titleNode = getRenderPropValue(title);
  70. const contentNode = getRenderPropValue(content);
  71. return wrapCSSVar(/*#__PURE__*/React.createElement(Tooltip, Object.assign({
  72. placement: placement,
  73. trigger: trigger,
  74. mouseEnterDelay: mouseEnterDelay,
  75. mouseLeaveDelay: mouseLeaveDelay
  76. }, otherProps, {
  77. prefixCls: prefixCls,
  78. classNames: {
  79. root: rootClassNames,
  80. body: bodyClassNames
  81. },
  82. styles: {
  83. root: Object.assign(Object.assign(Object.assign(Object.assign({}, contextStyles.root), contextStyle), overlayStyle), styles === null || styles === void 0 ? void 0 : styles.root),
  84. body: Object.assign(Object.assign({}, contextStyles.body), styles === null || styles === void 0 ? void 0 : styles.body)
  85. },
  86. ref: ref,
  87. open: open,
  88. onOpenChange: onInternalOpenChange,
  89. overlay: titleNode || contentNode ? (/*#__PURE__*/React.createElement(Overlay, {
  90. prefixCls: prefixCls,
  91. title: titleNode,
  92. content: contentNode
  93. })) : null,
  94. transitionName: getTransitionName(rootPrefixCls, 'zoom-big', otherProps.transitionName),
  95. "data-popover-inject": true
  96. }), cloneElement(children, {
  97. onKeyDown: e => {
  98. var _a, _b;
  99. if (/*#__PURE__*/isValidElement(children)) {
  100. (_b = children === null || children === void 0 ? void 0 : (_a = children.props).onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
  101. }
  102. onKeyDown(e);
  103. }
  104. })));
  105. });
  106. const Popover = InternalPopover;
  107. Popover._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
  108. if (process.env.NODE_ENV !== 'production') {
  109. Popover.displayName = 'Popover';
  110. }
  111. export default Popover;