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';
|
2015-12-02 15:18:15 +08:00
|
|
|
import Button 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
|
|
|
|
2018-12-22 21:21:42 +08:00
|
|
|
type DropdownButtonType = 'primary' | 'ghost' | 'dashed';
|
|
|
|
|
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;
|
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,
|
|
|
|
type,
|
|
|
|
disabled,
|
|
|
|
onClick,
|
|
|
|
htmlType,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
overlay,
|
|
|
|
trigger,
|
|
|
|
align,
|
|
|
|
visible,
|
|
|
|
onVisibleChange,
|
|
|
|
placement,
|
|
|
|
getPopupContainer,
|
|
|
|
href,
|
|
|
|
icon = <EllipsisOutlined />,
|
|
|
|
title,
|
|
|
|
buttonsRender,
|
2021-05-10 10:37:52 +08:00
|
|
|
mouseEnterDelay,
|
|
|
|
mouseLeaveDelay,
|
2021-06-29 17:55:02 +08:00
|
|
|
overlayClassName,
|
|
|
|
overlayStyle,
|
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,
|
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}
|
|
|
|
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;
|
|
|
|
|
|
|
|
DropdownButton.defaultProps = {
|
|
|
|
type: 'default' as DropdownButtonType,
|
|
|
|
buttonsRender: (buttons: React.ReactNode[]) => buttons,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DropdownButton;
|