ant-design/components/dropdown/dropdown-button.tsx
MadCcc 96fa23ea7c
refactor: Tooltip, Popover, Dropdown, Tour support css var (#45705)
* feat: Tooltip, Popover, Dropdown, Tour support css var

* chore: update snapshot

* chore: code clean

* chore: code clean

* chore: update snapshot

* chore: update snapshot

* chore: update snapshot

* chore: add test
2023-11-08 14:56:15 +08:00

143 lines
3.5 KiB
TypeScript

import classNames from 'classnames';
import * as React from 'react';
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
import Button from '../button';
import { ConfigContext } from '../config-provider';
import Space from '../space';
import { useCompactItemContext } from '../space/Compact';
import Dropdown from './dropdown';
import type { ButtonProps, ButtonHTMLType } from '../button';
import type { ButtonGroupProps } from '../button/button-group';
import type { DropdownProps } from './dropdown';
export type DropdownButtonType = 'default' | 'primary' | 'dashed' | 'link' | 'text';
export interface DropdownButtonProps extends ButtonGroupProps, DropdownProps {
type?: DropdownButtonType;
htmlType?: ButtonHTMLType;
danger?: boolean;
disabled?: boolean;
loading?: ButtonProps['loading'];
onClick?: React.MouseEventHandler<HTMLElement>;
icon?: React.ReactNode;
href?: string;
children?: React.ReactNode;
title?: string;
buttonsRender?: (buttons: React.ReactNode[]) => React.ReactNode[];
}
type CompoundedComponent = React.FC<DropdownButtonProps> & {
/** @internal */
__ANT_BUTTON: boolean;
};
const DropdownButton: CompoundedComponent = (props) => {
const {
getPopupContainer: getContextPopupContainer,
getPrefixCls,
direction,
} = React.useContext(ConfigContext);
const {
prefixCls: customizePrefixCls,
type = 'default',
danger,
disabled,
loading,
onClick,
htmlType,
children,
className,
menu,
arrow,
autoFocus,
overlay,
trigger,
align,
open,
onOpenChange,
placement,
getPopupContainer,
href,
icon = <EllipsisOutlined />,
title,
buttonsRender = (buttons: React.ReactNode[]) => buttons,
mouseEnterDelay,
mouseLeaveDelay,
overlayClassName,
overlayStyle,
destroyPopupOnHide,
dropdownRender,
...restProps
} = props;
const prefixCls = getPrefixCls('dropdown', customizePrefixCls);
const buttonPrefixCls = `${prefixCls}-button`;
const dropdownProps: DropdownProps = {
menu,
arrow,
autoFocus,
align,
disabled,
trigger: disabled ? [] : trigger,
onOpenChange,
getPopupContainer: getPopupContainer || getContextPopupContainer,
mouseEnterDelay,
mouseLeaveDelay,
overlayClassName,
overlayStyle,
destroyPopupOnHide,
dropdownRender,
};
const { compactSize, compactItemClassnames } = useCompactItemContext(prefixCls, direction);
const classes = classNames(buttonPrefixCls, compactItemClassnames, className);
if ('overlay' in props) {
dropdownProps.overlay = overlay;
}
if ('open' in props) {
dropdownProps.open = open;
}
if ('placement' in props) {
dropdownProps.placement = placement;
} else {
dropdownProps.placement = direction === 'rtl' ? 'bottomLeft' : 'bottomRight';
}
const leftButton = (
<Button
type={type}
danger={danger}
disabled={disabled}
loading={loading}
onClick={onClick}
htmlType={htmlType}
href={href}
title={title}
>
{children}
</Button>
);
const rightButton = <Button type={type} danger={danger} icon={icon} />;
const [leftButtonToRender, rightButtonToRender] = buttonsRender([leftButton, rightButton]);
return (
<Space.Compact className={classes} size={compactSize} block {...restProps}>
{leftButtonToRender}
<Dropdown {...dropdownProps}>{rightButtonToRender}</Dropdown>
</Space.Compact>
);
};
DropdownButton.__ANT_BUTTON = true;
export default DropdownButton;