Panel.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _typeof from "@babel/runtime/helpers/esm/typeof";
  3. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  4. import classNames from 'classnames';
  5. import { useComposeRef } from "rc-util/es/ref";
  6. import React, { useMemo, useRef } from 'react';
  7. import { RefContext } from "../../context";
  8. import MemoChildren from "./MemoChildren";
  9. import pickAttrs from "rc-util/es/pickAttrs";
  10. var sentinelStyle = {
  11. width: 0,
  12. height: 0,
  13. overflow: 'hidden',
  14. outline: 'none'
  15. };
  16. var entityStyle = {
  17. outline: 'none'
  18. };
  19. var Panel = /*#__PURE__*/React.forwardRef(function (props, ref) {
  20. var prefixCls = props.prefixCls,
  21. className = props.className,
  22. style = props.style,
  23. title = props.title,
  24. ariaId = props.ariaId,
  25. footer = props.footer,
  26. closable = props.closable,
  27. closeIcon = props.closeIcon,
  28. onClose = props.onClose,
  29. children = props.children,
  30. bodyStyle = props.bodyStyle,
  31. bodyProps = props.bodyProps,
  32. modalRender = props.modalRender,
  33. onMouseDown = props.onMouseDown,
  34. onMouseUp = props.onMouseUp,
  35. holderRef = props.holderRef,
  36. visible = props.visible,
  37. forceRender = props.forceRender,
  38. width = props.width,
  39. height = props.height,
  40. modalClassNames = props.classNames,
  41. modalStyles = props.styles;
  42. // ================================= Refs =================================
  43. var _React$useContext = React.useContext(RefContext),
  44. panelRef = _React$useContext.panel;
  45. var mergedRef = useComposeRef(holderRef, panelRef);
  46. var sentinelStartRef = useRef();
  47. var sentinelEndRef = useRef();
  48. React.useImperativeHandle(ref, function () {
  49. return {
  50. focus: function focus() {
  51. var _sentinelStartRef$cur;
  52. (_sentinelStartRef$cur = sentinelStartRef.current) === null || _sentinelStartRef$cur === void 0 || _sentinelStartRef$cur.focus({
  53. preventScroll: true
  54. });
  55. },
  56. changeActive: function changeActive(next) {
  57. var _document = document,
  58. activeElement = _document.activeElement;
  59. if (next && activeElement === sentinelEndRef.current) {
  60. sentinelStartRef.current.focus({
  61. preventScroll: true
  62. });
  63. } else if (!next && activeElement === sentinelStartRef.current) {
  64. sentinelEndRef.current.focus({
  65. preventScroll: true
  66. });
  67. }
  68. }
  69. };
  70. });
  71. // ================================ Style =================================
  72. var contentStyle = {};
  73. if (width !== undefined) {
  74. contentStyle.width = width;
  75. }
  76. if (height !== undefined) {
  77. contentStyle.height = height;
  78. }
  79. // ================================ Render ================================
  80. var footerNode = footer ? /*#__PURE__*/React.createElement("div", {
  81. className: classNames("".concat(prefixCls, "-footer"), modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.footer),
  82. style: _objectSpread({}, modalStyles === null || modalStyles === void 0 ? void 0 : modalStyles.footer)
  83. }, footer) : null;
  84. var headerNode = title ? /*#__PURE__*/React.createElement("div", {
  85. className: classNames("".concat(prefixCls, "-header"), modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.header),
  86. style: _objectSpread({}, modalStyles === null || modalStyles === void 0 ? void 0 : modalStyles.header)
  87. }, /*#__PURE__*/React.createElement("div", {
  88. className: "".concat(prefixCls, "-title"),
  89. id: ariaId
  90. }, title)) : null;
  91. var closableObj = useMemo(function () {
  92. if (_typeof(closable) === 'object' && closable !== null) {
  93. return closable;
  94. }
  95. if (closable) {
  96. return {
  97. closeIcon: closeIcon !== null && closeIcon !== void 0 ? closeIcon : /*#__PURE__*/React.createElement("span", {
  98. className: "".concat(prefixCls, "-close-x")
  99. })
  100. };
  101. }
  102. return {};
  103. }, [closable, closeIcon, prefixCls]);
  104. var ariaProps = pickAttrs(closableObj, true);
  105. var closeBtnIsDisabled = _typeof(closable) === 'object' && closable.disabled;
  106. var closerNode = closable ? /*#__PURE__*/React.createElement("button", _extends({
  107. type: "button",
  108. onClick: onClose,
  109. "aria-label": "Close"
  110. }, ariaProps, {
  111. className: "".concat(prefixCls, "-close"),
  112. disabled: closeBtnIsDisabled
  113. }), closableObj.closeIcon) : null;
  114. var content = /*#__PURE__*/React.createElement("div", {
  115. className: classNames("".concat(prefixCls, "-content"), modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.content),
  116. style: modalStyles === null || modalStyles === void 0 ? void 0 : modalStyles.content
  117. }, closerNode, headerNode, /*#__PURE__*/React.createElement("div", _extends({
  118. className: classNames("".concat(prefixCls, "-body"), modalClassNames === null || modalClassNames === void 0 ? void 0 : modalClassNames.body),
  119. style: _objectSpread(_objectSpread({}, bodyStyle), modalStyles === null || modalStyles === void 0 ? void 0 : modalStyles.body)
  120. }, bodyProps), children), footerNode);
  121. return /*#__PURE__*/React.createElement("div", {
  122. key: "dialog-element",
  123. role: "dialog",
  124. "aria-labelledby": title ? ariaId : null,
  125. "aria-modal": "true",
  126. ref: mergedRef,
  127. style: _objectSpread(_objectSpread({}, style), contentStyle),
  128. className: classNames(prefixCls, className),
  129. onMouseDown: onMouseDown,
  130. onMouseUp: onMouseUp
  131. }, /*#__PURE__*/React.createElement("div", {
  132. ref: sentinelStartRef,
  133. tabIndex: 0,
  134. style: entityStyle
  135. }, /*#__PURE__*/React.createElement(MemoChildren, {
  136. shouldUpdate: visible || forceRender
  137. }, modalRender ? modalRender(content) : content)), /*#__PURE__*/React.createElement("div", {
  138. tabIndex: 0,
  139. ref: sentinelEndRef,
  140. style: sentinelStyle
  141. }));
  142. });
  143. if (process.env.NODE_ENV !== 'production') {
  144. Panel.displayName = 'Panel';
  145. }
  146. export default Panel;