2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2019-05-06 12:04:39 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
|
2021-10-21 21:07:55 +08:00
|
|
|
import Button, { ButtonProps } from '../button';
|
2018-09-05 20:09:38 +08:00
|
|
|
import { ButtonHTMLType } from '../button/button';
|
2017-06-16 20:28:42 +08:00
|
|
|
import { ButtonGroupProps } from '../button/button-group';
|
2020-05-17 00:51:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2017-06-20 09:46:23 +08:00
|
|
|
import Dropdown, { DropDownProps } 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
|
|
|
|
2017-06-20 09:46:23 +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;
|
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> {
|
|
|
|
__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',
|
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,
|
|
|
|
onVisibleChange,
|
|
|
|
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);
|
|
|
|
const dropdownProps = {
|
|
|
|
align,
|
|
|
|
overlay,
|
|
|
|
disabled,
|
|
|
|
trigger: disabled ? [] : trigger,
|
|
|
|
onVisibleChange,
|
|
|
|
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,
|
2020-05-17 00:51:05 +08:00
|
|
|
} as DropDownProps;
|
|
|
|
|
|
|
|
if ('visible' in props) {
|
|
|
|
dropdownProps.visible = visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('placement' in props) {
|
|
|
|
dropdownProps.placement = placement;
|
|
|
|
} else {
|
|
|
|
dropdownProps.placement = direction === 'rtl' ? 'bottomLeft' : 'bottomRight';
|
|
|
|
}
|
|
|
|
|
|
|
|
const leftButton = (
|
|
|
|
<Button
|
|
|
|
type={type}
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
|
|
|
|
const rightButton = <Button type={type} icon={icon} />;
|
|
|
|
|
|
|
|
const [leftButtonToRender, rightButtonToRender] = buttonsRender!([leftButton, rightButton]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ButtonGroup {...restProps} className={classNames(prefixCls, className)}>
|
|
|
|
{leftButtonToRender}
|
|
|
|
<Dropdown {...dropdownProps}>{rightButtonToRender}</Dropdown>
|
|
|
|
</ButtonGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DropdownButton.__ANT_BUTTON = true;
|
|
|
|
|
|
|
|
export default DropdownButton;
|