2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-03-18 10:31:25 +08:00
|
|
|
import RcDropdown from 'rc-dropdown';
|
2016-12-03 17:31:04 +08:00
|
|
|
import classNames from 'classnames';
|
2020-03-02 12:09:38 +08:00
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
2017-06-09 18:37:05 +08:00
|
|
|
import DropdownButton from './dropdown-button';
|
2020-05-17 00:51:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-05-10 15:43:29 +08:00
|
|
|
import warning from '../_util/warning';
|
2018-12-22 21:21:42 +08:00
|
|
|
import { tuple } from '../_util/type';
|
2020-05-14 20:54:49 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2022-01-13 13:34:34 +08:00
|
|
|
import getPlacements from '../_util/placements';
|
2022-04-25 10:54:00 +08:00
|
|
|
import useStyle from './style';
|
2015-12-02 15:18:15 +08:00
|
|
|
|
2018-12-22 21:21:42 +08:00
|
|
|
const Placements = tuple(
|
|
|
|
'topLeft',
|
|
|
|
'topCenter',
|
|
|
|
'topRight',
|
|
|
|
'bottomLeft',
|
|
|
|
'bottomCenter',
|
|
|
|
'bottomRight',
|
2022-01-13 13:34:34 +08:00
|
|
|
'top',
|
|
|
|
'bottom',
|
2018-12-22 21:21:42 +08:00
|
|
|
);
|
2021-02-17 17:09:13 +08:00
|
|
|
|
2019-11-15 14:35:25 +08:00
|
|
|
type Placement = typeof Placements[number];
|
2018-12-28 20:27:42 +08:00
|
|
|
|
2019-11-06 14:49:20 +08:00
|
|
|
type OverlayFunc = () => React.ReactElement;
|
2018-12-28 20:27:42 +08:00
|
|
|
|
2019-02-27 14:44:24 +08:00
|
|
|
type Align = {
|
|
|
|
points?: [string, string];
|
|
|
|
offset?: [number, number];
|
|
|
|
targetOffset?: [number, number];
|
|
|
|
overflow?: {
|
|
|
|
adjustX?: boolean;
|
|
|
|
adjustY?: boolean;
|
|
|
|
};
|
|
|
|
useCssRight?: boolean;
|
|
|
|
useCssBottom?: boolean;
|
|
|
|
useCssTransform?: boolean;
|
|
|
|
};
|
|
|
|
|
2022-01-13 13:34:34 +08:00
|
|
|
export type DropdownArrowOptions = {
|
|
|
|
pointAtCenter?: boolean;
|
|
|
|
};
|
|
|
|
|
2022-04-21 22:09:59 +08:00
|
|
|
export interface DropdownProps {
|
2022-01-13 13:34:34 +08:00
|
|
|
arrow?: boolean | DropdownArrowOptions;
|
2018-08-24 11:51:52 +08:00
|
|
|
trigger?: ('click' | 'hover' | 'contextMenu')[];
|
2019-11-06 14:49:20 +08:00
|
|
|
overlay: React.ReactElement | OverlayFunc;
|
2018-12-28 13:57:03 +08:00
|
|
|
onVisibleChange?: (visible: boolean) => void;
|
2016-08-24 16:09:55 +08:00
|
|
|
visible?: boolean;
|
2017-08-11 23:55:36 +08:00
|
|
|
disabled?: boolean;
|
2021-08-08 14:49:22 +08:00
|
|
|
destroyPopupOnHide?: boolean;
|
2019-02-27 14:44:24 +08:00
|
|
|
align?: Align;
|
2019-04-28 11:47:22 +08:00
|
|
|
getPopupContainer?: (triggerNode: HTMLElement) => HTMLElement;
|
2016-12-03 17:31:04 +08:00
|
|
|
prefixCls?: string;
|
2017-08-29 13:27:38 +08:00
|
|
|
className?: string;
|
2018-01-13 17:25:08 +08:00
|
|
|
transitionName?: string;
|
2018-12-22 21:21:42 +08:00
|
|
|
placement?: Placement;
|
2018-12-13 22:03:12 +08:00
|
|
|
overlayClassName?: string;
|
|
|
|
overlayStyle?: React.CSSProperties;
|
2017-12-03 19:09:39 +08:00
|
|
|
forceRender?: boolean;
|
2018-12-04 20:28:48 +08:00
|
|
|
mouseEnterDelay?: number;
|
|
|
|
mouseLeaveDelay?: number;
|
2018-12-21 20:56:36 +08:00
|
|
|
openClassName?: string;
|
2022-04-08 22:55:42 +08:00
|
|
|
children?: React.ReactNode;
|
2016-08-11 15:38:03 +08:00
|
|
|
}
|
|
|
|
|
2022-04-21 22:09:59 +08:00
|
|
|
interface DropdownInterface extends React.FC<DropdownProps> {
|
2020-05-17 00:51:05 +08:00
|
|
|
Button: typeof DropdownButton;
|
|
|
|
}
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
const Dropdown: DropdownInterface = props => {
|
2021-08-08 14:49:22 +08:00
|
|
|
const {
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
|
|
|
} = React.useContext(ConfigContext);
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
const getTransitionName = () => {
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2020-05-17 00:51:05 +08:00
|
|
|
const { placement = '', transitionName } = props;
|
2018-01-13 17:25:08 +08:00
|
|
|
if (transitionName !== undefined) {
|
|
|
|
return transitionName;
|
|
|
|
}
|
2017-02-09 15:44:33 +08:00
|
|
|
if (placement.indexOf('top') >= 0) {
|
2021-02-08 17:09:13 +08:00
|
|
|
return `${rootPrefixCls}-slide-down`;
|
2017-02-09 15:44:33 +08:00
|
|
|
}
|
2021-02-08 17:09:13 +08:00
|
|
|
return `${rootPrefixCls}-slide-up`;
|
2020-05-17 00:51:05 +08:00
|
|
|
};
|
2017-02-09 15:44:33 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
const renderOverlay = (prefixCls: string) => {
|
2018-12-28 20:27:42 +08:00
|
|
|
// rc-dropdown already can process the function of overlay, but we have check logic here.
|
|
|
|
// So we need render the element to check and pass back to rc-dropdown.
|
2020-05-17 00:51:05 +08:00
|
|
|
const { overlay } = props;
|
2018-12-28 20:27:42 +08:00
|
|
|
|
|
|
|
let overlayNode;
|
|
|
|
if (typeof overlay === 'function') {
|
|
|
|
overlayNode = (overlay as OverlayFunc)();
|
|
|
|
} else {
|
|
|
|
overlayNode = overlay;
|
2018-05-21 21:02:03 +08:00
|
|
|
}
|
2020-05-25 16:27:02 +08:00
|
|
|
overlayNode = React.Children.only(
|
2020-08-11 17:43:54 +08:00
|
|
|
typeof overlayNode === 'string' ? <span>{overlayNode}</span> : overlayNode,
|
2020-05-25 16:27:02 +08:00
|
|
|
);
|
2018-12-28 20:27:42 +08:00
|
|
|
|
|
|
|
const overlayProps = overlayNode.props;
|
|
|
|
|
|
|
|
// Warning if use other mode
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2018-12-28 20:27:42 +08:00
|
|
|
!overlayProps.mode || overlayProps.mode === 'vertical',
|
2019-02-27 15:32:29 +08:00
|
|
|
'Dropdown',
|
2019-08-05 18:38:10 +08:00
|
|
|
`mode="${overlayProps.mode}" is not supported for Dropdown's Menu.`,
|
2018-12-28 20:27:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
// menu cannot be selectable in dropdown defaultly
|
2021-05-24 16:24:00 +08:00
|
|
|
const { selectable = false, expandIcon } = overlayProps;
|
2018-12-28 20:27:42 +08:00
|
|
|
|
2021-02-17 17:09:13 +08:00
|
|
|
const overlayNodeExpandIcon =
|
|
|
|
typeof expandIcon !== 'undefined' && React.isValidElement(expandIcon) ? (
|
|
|
|
expandIcon
|
|
|
|
) : (
|
|
|
|
<span className={`${prefixCls}-menu-submenu-arrow`}>
|
|
|
|
<RightOutlined className={`${prefixCls}-menu-submenu-arrow-icon`} />
|
|
|
|
</span>
|
|
|
|
);
|
2018-12-28 20:27:42 +08:00
|
|
|
|
|
|
|
const fixedModeOverlay =
|
|
|
|
typeof overlayNode.type === 'string'
|
2020-05-25 16:27:02 +08:00
|
|
|
? overlayNode
|
2020-05-14 20:54:49 +08:00
|
|
|
: cloneElement(overlayNode, {
|
2018-12-28 20:27:42 +08:00
|
|
|
mode: 'vertical',
|
|
|
|
selectable,
|
2021-02-17 17:09:13 +08:00
|
|
|
expandIcon: overlayNodeExpandIcon,
|
2018-12-28 20:27:42 +08:00
|
|
|
});
|
|
|
|
|
2019-11-06 14:49:20 +08:00
|
|
|
return fixedModeOverlay as React.ReactElement;
|
2018-12-28 20:27:42 +08:00
|
|
|
};
|
2017-05-11 14:57:29 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
const getPlacement = () => {
|
|
|
|
const { placement } = props;
|
2022-01-13 13:34:34 +08:00
|
|
|
if (!placement) {
|
|
|
|
return direction === 'rtl' ? ('bottomRight' as Placement) : ('bottomLeft' as Placement);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (placement.includes('Center')) {
|
|
|
|
const newPlacement = placement.slice(0, placement.indexOf('Center'));
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2022-01-13 13:34:34 +08:00
|
|
|
!placement.includes('Center'),
|
|
|
|
'Dropdown',
|
|
|
|
`You are using '${placement}' placement in Dropdown, which is deprecated. Try to use '${newPlacement}' instead.`,
|
|
|
|
);
|
|
|
|
return newPlacement;
|
2020-01-02 19:10:16 +08:00
|
|
|
}
|
2022-01-13 13:34:34 +08:00
|
|
|
|
|
|
|
return placement;
|
2020-05-17 00:51:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const {
|
2020-06-28 22:41:59 +08:00
|
|
|
arrow,
|
2020-05-17 00:51:05 +08:00
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
trigger,
|
|
|
|
disabled,
|
|
|
|
getPopupContainer,
|
|
|
|
overlayClassName,
|
|
|
|
} = props;
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
const prefixCls = getPrefixCls('dropdown', customizePrefixCls);
|
2022-04-25 10:54:00 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
const child = React.Children.only(children) as React.ReactElement<any>;
|
|
|
|
|
|
|
|
const dropdownTrigger = cloneElement(child, {
|
2020-09-06 13:07:39 +08:00
|
|
|
className: classNames(
|
|
|
|
`${prefixCls}-trigger`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
child.props.className,
|
|
|
|
),
|
2020-05-17 00:51:05 +08:00
|
|
|
disabled,
|
|
|
|
});
|
|
|
|
|
2022-04-25 10:54:00 +08:00
|
|
|
const overlayClassNameCustomized = classNames(overlayClassName, hashId, {
|
2020-05-17 00:51:05 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
|
|
|
|
const triggerActions = disabled ? [] : trigger;
|
|
|
|
let alignPoint;
|
|
|
|
if (triggerActions && triggerActions.indexOf('contextMenu') !== -1) {
|
|
|
|
alignPoint = true;
|
|
|
|
}
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2022-01-13 13:34:34 +08:00
|
|
|
const builtinPlacements = getPlacements({
|
|
|
|
arrowPointAtCenter: typeof arrow === 'object' && arrow.pointAtCenter,
|
2022-03-09 19:34:46 +08:00
|
|
|
autoAdjustOverflow: true,
|
2022-01-13 13:34:34 +08:00
|
|
|
});
|
|
|
|
|
2022-04-25 10:54:00 +08:00
|
|
|
return wrapSSR(
|
2020-05-17 00:51:05 +08:00
|
|
|
<RcDropdown
|
|
|
|
alignPoint={alignPoint}
|
|
|
|
{...props}
|
2022-01-13 13:34:34 +08:00
|
|
|
builtinPlacements={builtinPlacements}
|
|
|
|
arrow={!!arrow}
|
2020-05-17 00:51:05 +08:00
|
|
|
overlayClassName={overlayClassNameCustomized}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
transitionName={getTransitionName()}
|
|
|
|
trigger={triggerActions}
|
|
|
|
overlay={() => renderOverlay(prefixCls)}
|
|
|
|
placement={getPlacement()}
|
|
|
|
>
|
|
|
|
{dropdownTrigger}
|
2022-04-25 10:54:00 +08:00
|
|
|
</RcDropdown>,
|
2020-05-17 00:51:05 +08:00
|
|
|
);
|
|
|
|
};
|
2018-06-12 22:07:02 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
Dropdown.Button = DropdownButton;
|
2018-11-26 12:06:42 +08:00
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
Dropdown.defaultProps = {
|
|
|
|
mouseEnterDelay: 0.15,
|
|
|
|
mouseLeaveDelay: 0.1,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Dropdown;
|