FloatButton.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 React, { useContext } from 'react';
  11. import classNames from 'classnames';
  12. import omit from "rc-util/es/omit";
  13. import convertToTooltipProps from '../_util/convertToTooltipProps';
  14. import { useZIndex } from '../_util/hooks/useZIndex';
  15. import { devUseWarning } from '../_util/warning';
  16. import Badge from '../badge';
  17. import { ConfigContext } from '../config-provider';
  18. import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
  19. import Tooltip from '../tooltip';
  20. import FloatButtonGroupContext from './context';
  21. import Content from './FloatButtonContent';
  22. import useStyle from './style';
  23. export const floatButtonPrefixCls = 'float-btn';
  24. const InternalFloatButton = /*#__PURE__*/React.forwardRef((props, ref) => {
  25. const {
  26. prefixCls: customizePrefixCls,
  27. className,
  28. rootClassName,
  29. style,
  30. type = 'default',
  31. shape = 'circle',
  32. icon,
  33. description,
  34. tooltip,
  35. htmlType = 'button',
  36. badge = {}
  37. } = props,
  38. restProps = __rest(props, ["prefixCls", "className", "rootClassName", "style", "type", "shape", "icon", "description", "tooltip", "htmlType", "badge"]);
  39. const {
  40. getPrefixCls,
  41. direction
  42. } = useContext(ConfigContext);
  43. const groupShape = useContext(FloatButtonGroupContext);
  44. const prefixCls = getPrefixCls(floatButtonPrefixCls, customizePrefixCls);
  45. const rootCls = useCSSVarCls(prefixCls);
  46. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
  47. const mergedShape = groupShape || shape;
  48. const classString = classNames(hashId, cssVarCls, rootCls, prefixCls, className, rootClassName, `${prefixCls}-${type}`, `${prefixCls}-${mergedShape}`, {
  49. [`${prefixCls}-rtl`]: direction === 'rtl'
  50. });
  51. // ============================ zIndex ============================
  52. const [zIndex] = useZIndex('FloatButton', style === null || style === void 0 ? void 0 : style.zIndex);
  53. const mergedStyle = Object.assign(Object.assign({}, style), {
  54. zIndex
  55. });
  56. // 虽然在 ts 中已经 omit 过了,但是为了防止多余的属性被透传进来,这里再 omit 一遍,以防万一
  57. const badgeProps = omit(badge, ['title', 'children', 'status', 'text']);
  58. let buttonNode = /*#__PURE__*/React.createElement("div", {
  59. className: `${prefixCls}-body`
  60. }, /*#__PURE__*/React.createElement(Content, {
  61. prefixCls: prefixCls,
  62. description: description,
  63. icon: icon
  64. }));
  65. if ('badge' in props) {
  66. buttonNode = /*#__PURE__*/React.createElement(Badge, Object.assign({}, badgeProps), buttonNode);
  67. }
  68. // ============================ Tooltip ============================
  69. const tooltipProps = convertToTooltipProps(tooltip);
  70. if (tooltipProps) {
  71. buttonNode = /*#__PURE__*/React.createElement(Tooltip, Object.assign({}, tooltipProps), buttonNode);
  72. }
  73. if (process.env.NODE_ENV !== 'production') {
  74. const warning = devUseWarning('FloatButton');
  75. process.env.NODE_ENV !== "production" ? warning(!(mergedShape === 'circle' && description), 'usage', 'supported only when `shape` is `square`. Due to narrow space for text, short sentence is recommended.') : void 0;
  76. }
  77. return wrapCSSVar(props.href ? (/*#__PURE__*/React.createElement("a", Object.assign({
  78. ref: ref
  79. }, restProps, {
  80. className: classString,
  81. style: mergedStyle
  82. }), buttonNode)) : (/*#__PURE__*/React.createElement("button", Object.assign({
  83. ref: ref
  84. }, restProps, {
  85. className: classString,
  86. style: mergedStyle,
  87. type: htmlType
  88. }), buttonNode)));
  89. });
  90. const FloatButton = InternalFloatButton;
  91. if (process.env.NODE_ENV !== 'production') {
  92. FloatButton.displayName = 'FloatButton';
  93. }
  94. export default FloatButton;