2020-03-02 12:09:38 +08:00
|
|
|
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
|
2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import * as React from 'react';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { ButtonProps } from '../button';
|
|
|
|
import Button from '../button';
|
|
|
|
import type { ButtonHTMLType } from '../button/button';
|
|
|
|
import type { ButtonGroupProps } from '../button/button-group';
|
2020-05-17 00:51:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type { DropdownProps } from './dropdown';
|
|
|
|
import Dropdown from './dropdown';
|
2019-05-06 12:04:39 +08:00
|
|
|
|
2017-06-20 09:46:23 +08:00
|
|
|
const ButtonGroup = Button.Group;
|
2016-08-19 16:43:32 +08:00
|
|
|
|
2021-09-13 19:38:09 +08:00
|
|
|
export type DropdownButtonType = 'default' | 'primary' | 'ghost' | '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'];
|
2018-05-04 14:48:41 +08:00
|
|
|
onClick?: React.MouseEventHandler<HTMLButtonElement>;
|
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
|
|
|
}
|
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
interface DropdownButtonInterface extends React.FC<DropdownButtonProps> {
|
2022-09-30 14:26:41 +08:00
|
|
|
/** @internal */
|
2020-05-17 00:51:05 +08:00
|
|
|
__ANT_BUTTON: boolean;
|
2016-03-18 10:31:25 +08:00
|
|
|
}
|
2020-05-17 00:51:05 +08:00
|
|
|
|
|
|
|
const DropdownButton: DropdownButtonInterface = 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,
|
|
|
|
overlay,
|
|
|
|
trigger,
|
|
|
|
align,
|
|
|
|
visible,
|
2022-09-02 17:22:23 +08:00
|
|
|
open,
|
2020-05-17 00:51:05 +08:00
|
|
|
onVisibleChange,
|
2022-09-02 17:22:23 +08:00
|
|
|
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,
|
2020-05-17 00:51:05 +08:00
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const prefixCls = getPrefixCls('dropdown-button', customizePrefixCls);
|
2022-09-30 09:49:28 +08:00
|
|
|
const dropdownProps: DropdownProps = {
|
2020-05-17 00:51:05 +08:00
|
|
|
align,
|
|
|
|
overlay,
|
|
|
|
disabled,
|
|
|
|
trigger: disabled ? [] : trigger,
|
2022-09-02 17:22:23 +08:00
|
|
|
onOpenChange: onOpenChange || onVisibleChange,
|
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-09-30 09:49:28 +08:00
|
|
|
};
|
2020-05-17 00:51:05 +08:00
|
|
|
|
2022-09-02 17:22:23 +08:00
|
|
|
if ('open' in props) {
|
|
|
|
dropdownProps.open = open;
|
|
|
|
} else if ('visible' in props) {
|
|
|
|
dropdownProps.open = visible;
|
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
|
|
|
|
|
|
|
return (
|
|
|
|
<ButtonGroup {...restProps} className={classNames(prefixCls, className)}>
|
|
|
|
{leftButtonToRender}
|
|
|
|
<Dropdown {...dropdownProps}>{rightButtonToRender}</Dropdown>
|
|
|
|
</ButtonGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DropdownButton.__ANT_BUTTON = true;
|
|
|
|
|
|
|
|
export default DropdownButton;
|