2023-11-30 11:06:41 +08:00
|
|
|
import React, { useContext, useMemo } from 'react';
|
2022-10-24 23:15:39 +08:00
|
|
|
import classNames from 'classnames';
|
2023-03-20 10:28:20 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-03-20 10:28:20 +08:00
|
|
|
import Badge from '../badge';
|
2022-10-24 23:15:39 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import Tooltip from '../tooltip';
|
2023-03-21 14:11:39 +08:00
|
|
|
import FloatButtonGroupContext from './context';
|
2023-09-11 17:28:04 +08:00
|
|
|
import Content from './FloatButtonContent';
|
2022-10-24 23:15:39 +08:00
|
|
|
import type {
|
|
|
|
CompoundedComponent,
|
2023-03-20 10:28:20 +08:00
|
|
|
FloatButtonBadgeProps,
|
2022-10-24 23:15:39 +08:00
|
|
|
FloatButtonContentProps,
|
2023-11-30 11:06:41 +08:00
|
|
|
FloatButtonElement,
|
2022-10-24 23:15:39 +08:00
|
|
|
FloatButtonProps,
|
|
|
|
FloatButtonShape,
|
|
|
|
} from './interface';
|
2023-03-01 19:00:17 +08:00
|
|
|
import useStyle from './style';
|
2023-11-15 11:46:17 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2022-10-24 23:15:39 +08:00
|
|
|
|
|
|
|
export const floatButtonPrefixCls = 'float-btn';
|
|
|
|
|
2023-11-30 11:06:41 +08:00
|
|
|
const FloatButton = React.forwardRef<FloatButtonElement, FloatButtonProps>((props, ref) => {
|
2022-10-24 23:15:39 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-10-24 23:15:39 +08:00
|
|
|
type = 'default',
|
|
|
|
shape = 'circle',
|
|
|
|
icon,
|
|
|
|
description,
|
|
|
|
tooltip,
|
2023-03-20 10:28:20 +08:00
|
|
|
badge = {},
|
2022-10-24 23:15:39 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const { getPrefixCls, direction } = useContext<ConfigConsumerProps>(ConfigContext);
|
|
|
|
const groupShape = useContext<FloatButtonShape | undefined>(FloatButtonGroupContext);
|
|
|
|
const prefixCls = getPrefixCls(floatButtonPrefixCls, customizePrefixCls);
|
2023-11-15 11:46:17 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2022-10-24 23:15:39 +08:00
|
|
|
|
|
|
|
const mergeShape = groupShape || shape;
|
|
|
|
|
|
|
|
const classString = classNames(
|
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-11-15 11:46:17 +08:00
|
|
|
rootCls,
|
2022-10-24 23:15:39 +08:00
|
|
|
prefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-10-24 23:15:39 +08:00
|
|
|
`${prefixCls}-${type}`,
|
|
|
|
`${prefixCls}-${mergeShape}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-03-20 10:28:20 +08:00
|
|
|
// 虽然在 ts 中已经 omit 过了,但是为了防止多余的属性被透传进来,这里再 omit 一遍,以防万一
|
|
|
|
const badgeProps = useMemo<FloatButtonBadgeProps>(
|
2023-03-21 14:11:39 +08:00
|
|
|
() => omit(badge, ['title', 'children', 'status', 'text'] as any[]),
|
2023-03-20 10:28:20 +08:00
|
|
|
[badge],
|
|
|
|
);
|
|
|
|
|
2022-10-24 23:15:39 +08:00
|
|
|
const contentProps = useMemo<FloatButtonContentProps>(
|
|
|
|
() => ({ prefixCls, description, icon, type }),
|
|
|
|
[prefixCls, description, icon, type],
|
|
|
|
);
|
|
|
|
|
2023-08-08 18:05:16 +08:00
|
|
|
let buttonNode = (
|
|
|
|
<div className={`${prefixCls}-body`}>
|
|
|
|
<Content {...contentProps} />
|
|
|
|
</div>
|
2022-10-24 23:15:39 +08:00
|
|
|
);
|
|
|
|
|
2023-08-08 18:05:16 +08:00
|
|
|
if ('badge' in props) {
|
|
|
|
buttonNode = <Badge {...badgeProps}>{buttonNode}</Badge>;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('tooltip' in props) {
|
|
|
|
buttonNode = (
|
|
|
|
<Tooltip title={tooltip} placement={direction === 'rtl' ? 'right' : 'left'}>
|
|
|
|
{buttonNode}
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-24 23:15:39 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('FloatButton');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-10-24 23:15:39 +08:00
|
|
|
warning(
|
|
|
|
!(shape === 'circle' && description),
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2022-10-24 23:15:39 +08:00
|
|
|
'supported only when `shape` is `square`. Due to narrow space for text, short sentence is recommended.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-15 11:46:17 +08:00
|
|
|
return wrapCSSVar(
|
2022-10-24 23:15:39 +08:00
|
|
|
props.href ? (
|
2023-10-20 17:19:17 +08:00
|
|
|
<a ref={ref} {...restProps} className={classString}>
|
2022-10-24 23:15:39 +08:00
|
|
|
{buttonNode}
|
|
|
|
</a>
|
|
|
|
) : (
|
2023-10-20 17:19:17 +08:00
|
|
|
<button ref={ref} {...restProps} className={classString} type="button">
|
2022-10-24 23:15:39 +08:00
|
|
|
{buttonNode}
|
|
|
|
</button>
|
|
|
|
),
|
|
|
|
);
|
2023-10-20 17:19:17 +08:00
|
|
|
}) as CompoundedComponent;
|
2022-10-24 23:15:39 +08:00
|
|
|
|
2023-07-17 23:43:32 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-10-20 17:19:17 +08:00
|
|
|
FloatButton.displayName = 'FloatButton';
|
2023-07-17 23:43:32 +08:00
|
|
|
}
|
|
|
|
|
2023-10-20 17:19:17 +08:00
|
|
|
export default FloatButton;
|