2023-09-07 10:15:58 +08:00
|
|
|
import * as React from 'react';
|
2020-03-02 12:09:38 +08:00
|
|
|
import RightOutlined from '@ant-design/icons/RightOutlined';
|
2023-09-07 10:15:58 +08:00
|
|
|
import type { AlignType } from '@rc-component/trigger';
|
2022-06-11 15:10:03 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import RcDropdown from 'rc-dropdown';
|
2023-08-08 16:48:26 +08:00
|
|
|
import { useEvent } from 'rc-util';
|
2022-06-21 15:48:29 +08:00
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2023-01-20 11:03:50 +08:00
|
|
|
import omit from 'rc-util/lib/omit';
|
2023-09-07 10:15:58 +08:00
|
|
|
|
2023-10-23 19:46:08 +08:00
|
|
|
import { useZIndex } from '../_util/hooks/useZIndex';
|
2023-03-30 14:55:34 +08:00
|
|
|
import type { AdjustOverflow } from '../_util/placements';
|
|
|
|
import getPlacements from '../_util/placements';
|
2023-09-07 10:15:58 +08:00
|
|
|
import genPurePanel from '../_util/PurePanel';
|
2023-03-30 14:55:34 +08:00
|
|
|
import { cloneElement } from '../_util/reactNode';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-10-23 19:46:08 +08:00
|
|
|
import zIndexContext from '../_util/zindexContext';
|
2020-05-17 00:51:05 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2023-01-20 11:03:50 +08:00
|
|
|
import type { MenuProps } from '../menu';
|
|
|
|
import Menu from '../menu';
|
2022-06-17 14:37:18 +08:00
|
|
|
import { OverrideProvider } from '../menu/OverrideContext';
|
2023-09-07 10:15:58 +08:00
|
|
|
import { useToken } from '../theme/internal';
|
2022-04-25 10:54:00 +08:00
|
|
|
import useStyle from './style';
|
2015-12-02 15:18:15 +08:00
|
|
|
|
2022-12-13 14:57:40 +08:00
|
|
|
const Placements = [
|
2018-12-22 21:21:42 +08:00
|
|
|
'topLeft',
|
|
|
|
'topCenter',
|
|
|
|
'topRight',
|
|
|
|
'bottomLeft',
|
|
|
|
'bottomCenter',
|
|
|
|
'bottomRight',
|
2022-01-13 13:34:34 +08:00
|
|
|
'top',
|
|
|
|
'bottom',
|
2022-12-13 14:57:40 +08:00
|
|
|
] as const;
|
2021-02-17 17:09:13 +08:00
|
|
|
|
2023-08-08 16:48:26 +08:00
|
|
|
type Placement = typeof Placements[number];
|
2023-04-29 11:13:53 +08:00
|
|
|
type DropdownPlacement = Exclude<Placement, 'topCenter' | 'bottomCenter'>;
|
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
|
|
|
|
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-10-23 00:33:45 +08:00
|
|
|
menu?: MenuProps;
|
2022-06-11 15:10:03 +08:00
|
|
|
autoFocus?: boolean;
|
2022-01-13 13:34:34 +08:00
|
|
|
arrow?: boolean | DropdownArrowOptions;
|
2018-08-24 11:51:52 +08:00
|
|
|
trigger?: ('click' | 'hover' | 'contextMenu')[];
|
2022-10-23 00:33:45 +08:00
|
|
|
dropdownRender?: (originNode: React.ReactNode) => React.ReactNode;
|
2023-10-17 14:50:54 +08:00
|
|
|
onOpenChange?: (open: boolean, info: { source: 'trigger' | 'menu' }) => void;
|
2022-08-24 17:46:44 +08:00
|
|
|
open?: boolean;
|
2017-08-11 23:55:36 +08:00
|
|
|
disabled?: boolean;
|
2021-08-08 14:49:22 +08:00
|
|
|
destroyPopupOnHide?: boolean;
|
2023-08-26 23:51:07 +08:00
|
|
|
align?: AlignType;
|
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;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: 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;
|
2022-12-22 17:56:02 +08:00
|
|
|
autoAdjustOverflow?: boolean | AdjustOverflow;
|
2022-09-06 21:46:49 +08:00
|
|
|
|
|
|
|
// Deprecated
|
2022-10-23 00:33:45 +08:00
|
|
|
/** @deprecated Please use `menu` instead */
|
|
|
|
overlay?: React.ReactElement | OverlayFunc;
|
2022-09-06 21:46:49 +08:00
|
|
|
/** @deprecated Please use `open` instead */
|
|
|
|
visible?: boolean;
|
|
|
|
/** @deprecated Please use `onOpenChange` instead */
|
|
|
|
onVisibleChange?: (open: boolean) => void;
|
2016-08-11 15:38:03 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 14:33:51 +08:00
|
|
|
type CompoundedComponent = React.FC<DropdownProps> & {
|
2022-07-11 15:57:05 +08:00
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof WrapPurePanel;
|
2022-12-01 14:33:51 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2022-12-01 14:33:51 +08:00
|
|
|
const Dropdown: CompoundedComponent = (props) => {
|
2023-03-30 14:55:34 +08:00
|
|
|
const {
|
|
|
|
menu,
|
|
|
|
arrow,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
trigger,
|
|
|
|
disabled,
|
|
|
|
dropdownRender,
|
|
|
|
getPopupContainer,
|
|
|
|
overlayClassName,
|
|
|
|
rootClassName,
|
2023-11-02 00:48:26 +08:00
|
|
|
overlayStyle,
|
2023-03-30 14:55:34 +08:00
|
|
|
open,
|
|
|
|
onOpenChange,
|
|
|
|
// Deprecated
|
|
|
|
visible,
|
|
|
|
onVisibleChange,
|
|
|
|
mouseEnterDelay = 0.15,
|
|
|
|
mouseLeaveDelay = 0.1,
|
|
|
|
autoAdjustOverflow = true,
|
|
|
|
placement = '',
|
|
|
|
overlay,
|
|
|
|
transitionName,
|
|
|
|
} = props;
|
2021-08-08 14:49:22 +08:00
|
|
|
const {
|
|
|
|
getPopupContainer: getContextPopupContainer,
|
|
|
|
getPrefixCls,
|
|
|
|
direction,
|
2023-11-02 00:48:26 +08:00
|
|
|
dropdown,
|
2021-08-08 14:49:22 +08:00
|
|
|
} = React.useContext(ConfigContext);
|
2016-03-29 14:01:10 +08:00
|
|
|
|
2022-09-02 17:22:23 +08:00
|
|
|
// Warning for deprecated usage
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Dropdown');
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2022-09-02 17:22:23 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
[
|
|
|
|
['visible', 'open'],
|
|
|
|
['onVisibleChange', 'onOpenChange'],
|
|
|
|
].forEach(([deprecatedName, newName]) => {
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
|
2022-09-02 17:22:23 +08:00
|
|
|
});
|
2022-10-23 00:33:45 +08:00
|
|
|
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!('overlay' in props), 'overlay', 'menu');
|
2022-09-02 17:22:23 +08:00
|
|
|
}
|
|
|
|
|
2023-03-30 14:55:34 +08:00
|
|
|
const memoTransitionName = React.useMemo<string>(() => {
|
2021-02-08 17:09:13 +08:00
|
|
|
const rootPrefixCls = getPrefixCls();
|
2023-03-30 14:55:34 +08:00
|
|
|
|
2018-01-13 17:25:08 +08:00
|
|
|
if (transitionName !== undefined) {
|
|
|
|
return transitionName;
|
|
|
|
}
|
2022-09-30 09:49:28 +08:00
|
|
|
if (placement.includes('top')) {
|
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`;
|
2023-03-30 14:55:34 +08:00
|
|
|
}, [getPrefixCls, placement, transitionName]);
|
2017-02-09 15:44:33 +08:00
|
|
|
|
2023-04-29 11:13:53 +08:00
|
|
|
const memoPlacement = React.useMemo<DropdownPlacement>(() => {
|
2022-01-13 13:34:34 +08:00
|
|
|
if (!placement) {
|
2022-09-30 09:49:28 +08:00
|
|
|
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
|
2022-01-13 13:34:34 +08:00
|
|
|
}
|
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (placement.includes('Center')) {
|
|
|
|
return placement.slice(0, placement.indexOf('Center')) as DropdownPlacement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return placement as DropdownPlacement;
|
|
|
|
}, [placement, direction]);
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2022-01-13 13:34:34 +08:00
|
|
|
if (placement.includes('Center')) {
|
2023-04-29 11:13:53 +08:00
|
|
|
const newPlacement = placement.slice(0, placement.indexOf('Center')) as DropdownPlacement;
|
2022-05-10 15:43:29 +08:00
|
|
|
warning(
|
2022-01-13 13:34:34 +08:00
|
|
|
!placement.includes('Center'),
|
2023-09-11 17:28:04 +08:00
|
|
|
'deprecated',
|
2022-01-13 13:34:34 +08:00
|
|
|
`You are using '${placement}' placement in Dropdown, which is deprecated. Try to use '${newPlacement}' instead.`,
|
|
|
|
);
|
2020-01-02 19:10:16 +08:00
|
|
|
}
|
2022-01-13 13:34:34 +08:00
|
|
|
|
2022-09-06 21:46:49 +08:00
|
|
|
[
|
|
|
|
['visible', 'open'],
|
|
|
|
['onVisibleChange', 'onOpenChange'],
|
|
|
|
].forEach(([deprecatedName, newName]) => {
|
2023-09-13 22:07:33 +08:00
|
|
|
warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
|
2022-09-06 21:46:49 +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);
|
|
|
|
|
2023-09-07 10:15:58 +08:00
|
|
|
const [, token] = useToken();
|
2023-01-19 11:21:05 +08:00
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
const triggerActions = disabled ? [] : trigger;
|
2022-09-30 09:49:28 +08:00
|
|
|
let alignPoint: boolean;
|
|
|
|
if (triggerActions && triggerActions.includes('contextMenu')) {
|
2020-05-17 00:51:05 +08:00
|
|
|
alignPoint = true;
|
|
|
|
}
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2022-08-24 17:46:44 +08:00
|
|
|
// =========================== Open ============================
|
|
|
|
const [mergedOpen, setOpen] = useMergedState(false, {
|
2022-09-06 21:46:49 +08:00
|
|
|
value: open ?? visible,
|
2022-06-21 15:48:29 +08:00
|
|
|
});
|
|
|
|
|
2022-08-24 17:46:44 +08:00
|
|
|
const onInnerOpenChange = useEvent((nextOpen: boolean) => {
|
2023-10-17 14:50:54 +08:00
|
|
|
onOpenChange?.(nextOpen, { source: 'trigger' });
|
2022-09-06 21:46:49 +08:00
|
|
|
onVisibleChange?.(nextOpen);
|
2022-08-24 17:46:44 +08:00
|
|
|
setOpen(nextOpen);
|
2022-06-21 15:48:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// =========================== Overlay ============================
|
2023-11-02 00:48:26 +08:00
|
|
|
const overlayClassNameCustomized = classNames(
|
|
|
|
overlayClassName,
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
|
|
|
dropdown?.className,
|
|
|
|
{ [`${prefixCls}-rtl`]: direction === 'rtl' },
|
|
|
|
);
|
2022-06-21 15:48:29 +08:00
|
|
|
|
2022-01-13 13:34:34 +08:00
|
|
|
const builtinPlacements = getPlacements({
|
|
|
|
arrowPointAtCenter: typeof arrow === 'object' && arrow.pointAtCenter,
|
2022-12-22 17:56:02 +08:00
|
|
|
autoAdjustOverflow,
|
2023-01-19 11:21:05 +08:00
|
|
|
offset: token.marginXXS,
|
|
|
|
arrowWidth: arrow ? token.sizePopupArrow : 0,
|
2023-02-15 10:21:28 +08:00
|
|
|
borderRadius: token.borderRadius,
|
2022-01-13 13:34:34 +08:00
|
|
|
});
|
|
|
|
|
2022-06-21 15:48:29 +08:00
|
|
|
const onMenuClick = React.useCallback(() => {
|
2023-10-26 10:21:54 +08:00
|
|
|
if (menu?.selectable && menu?.multiple) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-17 14:50:54 +08:00
|
|
|
onOpenChange?.(false, { source: 'menu' });
|
2022-08-24 17:46:44 +08:00
|
|
|
setOpen(false);
|
2023-10-26 10:21:54 +08:00
|
|
|
}, [menu?.selectable, menu?.multiple]);
|
2022-05-30 19:06:51 +08:00
|
|
|
|
|
|
|
const renderOverlay = () => {
|
|
|
|
// 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.
|
|
|
|
|
2022-09-30 09:49:28 +08:00
|
|
|
let overlayNode: React.ReactNode;
|
2022-10-23 00:33:45 +08:00
|
|
|
if (menu?.items) {
|
|
|
|
overlayNode = <Menu {...menu} />;
|
|
|
|
} else if (typeof overlay === 'function') {
|
2022-11-09 17:36:49 +08:00
|
|
|
overlayNode = overlay();
|
2022-05-30 19:06:51 +08:00
|
|
|
} else {
|
|
|
|
overlayNode = overlay;
|
|
|
|
}
|
2022-10-23 00:33:45 +08:00
|
|
|
if (dropdownRender) {
|
|
|
|
overlayNode = dropdownRender(overlayNode);
|
|
|
|
}
|
2022-05-30 19:06:51 +08:00
|
|
|
overlayNode = React.Children.only(
|
|
|
|
typeof overlayNode === 'string' ? <span>{overlayNode}</span> : overlayNode,
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-06-17 14:37:18 +08:00
|
|
|
<OverrideProvider
|
|
|
|
prefixCls={`${prefixCls}-menu`}
|
|
|
|
expandIcon={
|
|
|
|
<span className={`${prefixCls}-menu-submenu-arrow`}>
|
|
|
|
<RightOutlined className={`${prefixCls}-menu-submenu-arrow-icon`} />
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
mode="vertical"
|
|
|
|
selectable={false}
|
2022-06-21 15:48:29 +08:00
|
|
|
onClick={onMenuClick}
|
2022-06-17 14:37:18 +08:00
|
|
|
validator={({ mode }) => {
|
|
|
|
// Warning if use other mode
|
|
|
|
warning(
|
|
|
|
!mode || mode === 'vertical',
|
2023-09-11 17:28:04 +08:00
|
|
|
'usage',
|
2022-06-17 14:37:18 +08:00
|
|
|
`mode="${mode}" is not supported for Dropdown's Menu.`,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
>
|
2023-06-18 13:21:39 +08:00
|
|
|
{overlayNode}
|
2022-06-17 14:37:18 +08:00
|
|
|
</OverrideProvider>
|
2022-05-30 19:06:51 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-10-23 19:46:08 +08:00
|
|
|
// =========================== zIndex ============================
|
2023-11-02 00:48:26 +08:00
|
|
|
const [zIndex, contextZIndex] = useZIndex('Dropdown', overlayStyle?.zIndex as number);
|
2023-10-23 19:46:08 +08:00
|
|
|
|
2022-06-21 15:48:29 +08:00
|
|
|
// ============================ Render ============================
|
2022-04-25 10:54:00 +08:00
|
|
|
return wrapSSR(
|
2023-10-23 19:46:08 +08:00
|
|
|
<zIndexContext.Provider value={contextZIndex}>
|
|
|
|
<RcDropdown
|
|
|
|
alignPoint={alignPoint!}
|
|
|
|
{...omit(props, ['rootClassName'])}
|
|
|
|
mouseEnterDelay={mouseEnterDelay}
|
|
|
|
mouseLeaveDelay={mouseLeaveDelay}
|
|
|
|
visible={mergedOpen}
|
|
|
|
builtinPlacements={builtinPlacements}
|
|
|
|
arrow={!!arrow}
|
|
|
|
overlayClassName={overlayClassNameCustomized}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
transitionName={memoTransitionName}
|
|
|
|
trigger={triggerActions}
|
|
|
|
overlay={renderOverlay}
|
|
|
|
placement={memoPlacement}
|
|
|
|
onVisibleChange={onInnerOpenChange}
|
2023-11-02 00:48:26 +08:00
|
|
|
overlayStyle={{ ...dropdown?.style, ...overlayStyle, zIndex }}
|
2023-10-23 19:46:08 +08:00
|
|
|
>
|
|
|
|
{dropdownTrigger}
|
|
|
|
</RcDropdown>
|
|
|
|
</zIndexContext.Provider>,
|
2020-05-17 00:51:05 +08:00
|
|
|
);
|
|
|
|
};
|
2018-06-12 22:07:02 +08:00
|
|
|
|
2023-04-29 11:13:53 +08:00
|
|
|
function postPureProps(props: DropdownProps) {
|
|
|
|
return {
|
|
|
|
...props,
|
|
|
|
align: {
|
|
|
|
overflow: {
|
|
|
|
adjustX: false,
|
|
|
|
adjustY: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-11 15:57:05 +08:00
|
|
|
// We don't care debug panel
|
2023-04-29 11:13:53 +08:00
|
|
|
const PurePanel = genPurePanel(Dropdown, 'dropdown', (prefixCls) => prefixCls, postPureProps);
|
2022-07-11 15:57:05 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
/* istanbul ignore next */
|
2023-07-11 23:01:17 +08:00
|
|
|
const WrapPurePanel: React.FC<DropdownProps> = (props) => (
|
2022-07-11 15:57:05 +08:00
|
|
|
<PurePanel {...props}>
|
|
|
|
<span />
|
|
|
|
</PurePanel>
|
|
|
|
);
|
|
|
|
|
|
|
|
Dropdown._InternalPanelDoNotUseOrYouWillBeFired = WrapPurePanel;
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Dropdown.displayName = 'Dropdown';
|
|
|
|
}
|
|
|
|
|
2020-05-17 00:51:05 +08:00
|
|
|
export default Dropdown;
|