ant-design/components/dropdown/dropdown-button.tsx
二货机器人 b4730281a9
refactor: Dropdown cssinjs support (#35214)
* chore: init

* chore: dropdown style

* chore: more style

* chore: icon style

* chore: more style

* chore: diff shows the cache path

* chore: motion support

* chore: more style

* feat: status

* chore: sub menu style

* chore: all dropdown style

* fix: ts

* chore: update snapshot

* Update components/dropdown/style/index.tsx

Co-authored-by: MadCcc <1075746765@qq.com>

* chore: update style

Co-authored-by: MadCcc <1075746765@qq.com>
2022-04-25 10:54:00 +08:00

124 lines
3.1 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import EllipsisOutlined from '@ant-design/icons/EllipsisOutlined';
import Button, { ButtonProps } from '../button';
import { ButtonHTMLType } from '../button/button';
import { ButtonGroupProps } from '../button/button-group';
import { ConfigContext } from '../config-provider';
import Dropdown, { DropdownProps } from './dropdown';
import useStyle from './style';
const ButtonGroup = Button.Group;
export type DropdownButtonType = 'default' | 'primary' | 'ghost' | 'dashed' | 'link' | 'text';
export interface DropdownButtonProps extends ButtonGroupProps, DropdownProps {
type?: DropdownButtonType;
htmlType?: ButtonHTMLType;
disabled?: boolean;
loading?: ButtonProps['loading'];
onClick?: React.MouseEventHandler<HTMLButtonElement>;
icon?: React.ReactNode;
href?: string;
children?: React.ReactNode;
title?: string;
buttonsRender?: (buttons: React.ReactNode[]) => React.ReactNode[];
}
interface DropdownButtonInterface extends React.FC<DropdownButtonProps> {
__ANT_BUTTON: boolean;
}
const DropdownButton: DropdownButtonInterface = props => {
const {
getPopupContainer: getContextPopupContainer,
getPrefixCls,
direction,
} = React.useContext(ConfigContext);
const {
prefixCls: customizePrefixCls,
type = 'default',
disabled,
loading,
onClick,
htmlType,
children,
className,
overlay,
trigger,
align,
visible,
onVisibleChange,
placement,
getPopupContainer,
href,
icon = <EllipsisOutlined />,
title,
buttonsRender = (buttons: React.ReactNode[]) => buttons,
mouseEnterDelay,
mouseLeaveDelay,
overlayClassName,
overlayStyle,
destroyPopupOnHide,
...restProps
} = props;
const prefixCls = getPrefixCls('dropdown', customizePrefixCls);
const buttonPrefixCls = `${prefixCls}-button`;
const [wrapSSR, hashId] = useStyle(prefixCls);
const dropdownProps = {
align,
overlay,
disabled,
trigger: disabled ? [] : trigger,
onVisibleChange,
getPopupContainer: getPopupContainer || getContextPopupContainer,
mouseEnterDelay,
mouseLeaveDelay,
overlayClassName,
overlayStyle,
destroyPopupOnHide,
} 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}
loading={loading}
onClick={onClick}
htmlType={htmlType}
href={href}
title={title}
>
{children}
</Button>
);
const rightButton = <Button type={type} icon={icon} />;
const [leftButtonToRender, rightButtonToRender] = buttonsRender!([leftButton, rightButton]);
return wrapSSR(
<ButtonGroup {...restProps} className={classNames(buttonPrefixCls, className, hashId)}>
{leftButtonToRender}
<Dropdown {...dropdownProps}>{rightButtonToRender}</Dropdown>
</ButtonGroup>,
);
};
DropdownButton.__ANT_BUTTON = true;
export default DropdownButton;