2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-01-11 10:32:05 +08:00
|
|
|
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
|
2024-04-08 14:04:08 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2022-05-07 14:31:54 +08:00
|
|
|
import Button from '../button';
|
2024-04-08 14:04:08 +08:00
|
|
|
import type { ButtonHTMLType, ButtonProps } from '../button';
|
|
|
|
import type { ButtonGroupProps } from '../button/button-group';
|
2020-05-17 00:51:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-01-11 10:32:05 +08:00
|
|
|
import Space from '../space';
|
2022-10-19 23:36:41 +08:00
|
|
|
import { useCompactItemContext } from '../space/Compact';
|
2022-05-07 14:31:54 +08:00
|
|
|
import Dropdown from './dropdown';
|
2023-01-11 10:32:05 +08:00
|
|
|
import type { DropdownProps } from './dropdown';
|
|
|
|
|
2023-07-20 17:59:35 +08:00
|
|
|
export type DropdownButtonType = 'default' | 'primary' | 'dashed' | 'link' | 'text';
|
2018-12-22 21:21:42 +08:00
|
|
|
|
2022-04-21 22:09:59 +08:00
|
|
|
export interface DropdownButtonProps extends ButtonGroupProps, DropdownProps {
|
2018-12-22 21:21:42 +08:00
|
|
|
type?: DropdownButtonType;
|
2018-09-05 20:09:38 +08:00
|
|
|
htmlType?: ButtonHTMLType;
|
2022-08-04 10:49:55 +08:00
|
|
|
danger?: boolean;
|
2016-09-19 11:42:03 +08:00
|
|
|
disabled?: boolean;
|
2021-10-21 21:07:55 +08:00
|
|
|
loading?: ButtonProps['loading'];
|
2022-12-15 17:48:29 +08:00
|
|
|
onClick?: React.MouseEventHandler<HTMLElement>;
|
2019-05-06 12:04:39 +08:00
|
|
|
icon?: React.ReactNode;
|
2018-12-21 21:12:36 +08:00
|
|
|
href?: string;
|
2019-06-16 20:51:47 +08:00
|
|
|
children?: React.ReactNode;
|
2019-08-09 18:10:00 +08:00
|
|
|
title?: string;
|
2020-01-13 12:15:20 +08:00
|
|
|
buttonsRender?: (buttons: React.ReactNode[]) => React.ReactNode[];
|
2016-08-19 16:43:32 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 14:33:51 +08:00
|
|
|
type CompoundedComponent = React.FC<DropdownButtonProps> & {
|
2022-09-30 14:26:41 +08:00
|
|
|
/** @internal */
|
2020-05-17 00:51:05 +08:00
|
|
|
__ANT_BUTTON: boolean;
|
2022-12-01 14:33:51 +08:00
|
|
|
};
|
2020-05-17 00:51:05 +08:00
|
|
|
|
2022-12-01 14:33:51 +08:00
|
|
|
const DropdownButton: CompoundedComponent = (props) => {
|
2021-05-24 16:24:00 +08:00
|
|
|
const {
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
|
|
|
} = React.useContext(ConfigContext);
|
2020-05-17 00:51:05 +08:00
|
|
|
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
2021-09-13 19:38:09 +08:00
|
|
|
type = 'default',
|
2022-08-04 10:49:55 +08:00
|
|
|
danger,
|
2020-05-17 00:51:05 +08:00
|
|
|
disabled,
|
2021-10-21 21:07:55 +08:00
|
|
|
loading,
|
2020-05-17 00:51:05 +08:00
|
|
|
onClick,
|
|
|
|
htmlType,
|
|
|
|
children,
|
|
|
|
className,
|
2022-10-23 00:33:45 +08:00
|
|
|
menu,
|
|
|
|
arrow,
|
|
|
|
autoFocus,
|
2020-05-17 00:51:05 +08:00
|
|
|
overlay,
|
|
|
|
trigger,
|
|
|
|
align,
|
2022-08-24 17:46:44 +08:00
|
|
|
open,
|
|
|
|
onOpenChange,
|
2020-05-17 00:51:05 +08:00
|
|
|
placement,
|
|
|
|
getPopupContainer,
|
|
|
|
href,
|
|
|
|
icon = <EllipsisOutlined />,
|
|
|
|
title,
|
2021-09-13 19:38:09 +08:00
|
|
|
buttonsRender = (buttons: React.ReactNode[]) => buttons,
|
2021-05-10 10:37:52 +08:00
|
|
|
mouseEnterDelay,
|
|
|
|
mouseLeaveDelay,
|
2021-06-29 17:55:02 +08:00
|
|
|
overlayClassName,
|
|
|
|
overlayStyle,
|
2021-12-28 13:48:23 +08:00
|
|
|
destroyPopupOnHide,
|
2022-11-22 20:33:10 +08:00
|
|
|
dropdownRender,
|
2020-05-17 00:51:05 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
2022-04-25 10:54:00 +08:00
|
|
|
const prefixCls = getPrefixCls('dropdown', customizePrefixCls);
|
|
|
|
const buttonPrefixCls = `${prefixCls}-button`;
|
|
|
|
|
2022-09-30 09:49:28 +08:00
|
|
|
const dropdownProps: DropdownProps = {
|
2022-10-23 00:33:45 +08:00
|
|
|
menu,
|
|
|
|
arrow,
|
|
|
|
autoFocus,
|
2020-05-17 00:51:05 +08:00
|
|
|
align,
|
|
|
|
disabled,
|
|
|
|
trigger: disabled ? [] : trigger,
|
2022-08-24 17:46:44 +08:00
|
|
|
onOpenChange,
|
2020-05-17 00:51:05 +08:00
|
|
|
getPopupContainer: getPopupContainer || getContextPopupContainer,
|
2021-05-10 10:37:52 +08:00
|
|
|
mouseEnterDelay,
|
|
|
|
mouseLeaveDelay,
|
2021-06-29 17:55:02 +08:00
|
|
|
overlayClassName,
|
|
|
|
overlayStyle,
|
2021-12-28 13:48:23 +08:00
|
|
|
destroyPopupOnHide,
|
2022-11-22 20:33:10 +08:00
|
|
|
dropdownRender,
|
2022-10-23 00:33:45 +08:00
|
|
|
};
|
2022-11-09 09:46:27 +08:00
|
|
|
|
2022-10-19 23:36:41 +08:00
|
|
|
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
|
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
const classes = classNames(buttonPrefixCls, compactItemClassnames, className);
|
2020-05-17 00:51:05 +08:00
|
|
|
|
2022-11-09 09:46:27 +08:00
|
|
|
if ('overlay' in props) {
|
|
|
|
dropdownProps.overlay = overlay;
|
|
|
|
}
|
|
|
|
|
2022-08-24 17:46:44 +08:00
|
|
|
if ('open' in props) {
|
|
|
|
dropdownProps.open = open;
|
2020-05-17 00:51:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ('placement' in props) {
|
|
|
|
dropdownProps.placement = placement;
|
|
|
|
} else {
|
|
|
|
dropdownProps.placement = direction === 'rtl' ? 'bottomLeft' : 'bottomRight';
|
|
|
|
}
|
|
|
|
|
|
|
|
const leftButton = (
|
|
|
|
<Button
|
|
|
|
type={type}
|
2022-08-04 10:49:55 +08:00
|
|
|
danger={danger}
|
2020-05-17 00:51:05 +08:00
|
|
|
disabled={disabled}
|
2021-10-21 21:07:55 +08:00
|
|
|
loading={loading}
|
2020-05-17 00:51:05 +08:00
|
|
|
onClick={onClick}
|
|
|
|
htmlType={htmlType}
|
|
|
|
href={href}
|
|
|
|
title={title}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
2022-08-04 10:49:55 +08:00
|
|
|
const rightButton = <Button type={type} danger={danger} icon={icon} />;
|
2020-05-17 00:51:05 +08:00
|
|
|
|
2022-09-30 09:49:28 +08:00
|
|
|
const [leftButtonToRender, rightButtonToRender] = buttonsRender([leftButton, rightButton]);
|
2020-05-17 00:51:05 +08:00
|
|
|
|
2023-11-08 14:56:15 +08:00
|
|
|
return (
|
2022-10-19 23:36:41 +08:00
|
|
|
<Space.Compact className={classes} size={compactSize} block {...restProps}>
|
2020-05-17 00:51:05 +08:00
|
|
|
{leftButtonToRender}
|
|
|
|
<Dropdown {...dropdownProps}>{rightButtonToRender}</Dropdown>
|
2023-11-08 14:56:15 +08:00
|
|
|
</Space.Compact>
|
2020-05-17 00:51:05 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DropdownButton.__ANT_BUTTON = true;
|
|
|
|
|
|
|
|
export default DropdownButton;
|