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';
|
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-04-25 10:54:00 +08:00
|
|
|
import useStyle from './style';
|
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;
|
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;
|
|
|
|
|
2022-04-25 10:54:00 +08:00
|
|
|
const prefixCls = getPrefixCls('dropdown', customizePrefixCls);
|
|
|
|
const buttonPrefixCls = `${prefixCls}-button`;
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
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,
|
2022-04-21 22:09:59 +08:00
|
|
|
} as DropdownProps;
|
2020-05-17 00:51:05 +08:00
|
|
|
|
|
|
|
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]);
|
|
|
|
|
2022-04-25 10:54:00 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<ButtonGroup {...restProps} className={classNames(buttonPrefixCls, className, hashId)}>
|
2020-05-17 00:51:05 +08:00
|
|
|
{leftButtonToRender}
|
|
|
|
<Dropdown {...dropdownProps}>{rightButtonToRender}</Dropdown>
|
2022-04-25 10:54:00 +08:00
|
|
|
</ButtonGroup>,
|
2020-05-17 00:51:05 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DropdownButton.__ANT_BUTTON = true;
|
|
|
|
|
|
|
|
export default DropdownButton;
|