Dropdown
A dropdown list.
When To Use#
When there are more than a few options to choose from, you can wrap them in a Dropdown
. By hovering or clicking on the trigger, a dropdown menu will appear, which allows you to choose an option and execute the relevant action.
Examples
Hover me
TypeScript
JavaScript
import { IArrowDown } from 'infra-design-icons';
import { Dropdown, Menu, Space } from 'infrad';
import React from 'react';
const menu = (
<Menu
items={[
{
key: '1',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
1st menu item
</a>
),
},
{
key: '2',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
2nd menu item (disabled)
</a>
),
icon: <IArrowDown />,
disabled: true,
},
{
key: '3',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
3rd menu item (disabled)
</a>
),
disabled: true,
},
{
key: '4',
danger: true,
label: 'a danger item',
},
]}
/>
);
const App: React.FC = () => (
<Dropdown overlay={menu}>
<a onClick={e => e.preventDefault()}>
<Space>
Hover me
<IArrowDown />
</Space>
</a>
</Dropdown>
);
export default App;
TypeScript
JavaScript
import { Button, Dropdown, Menu } from 'infrad';
import React from 'react';
const menu = (
<Menu
items={[
{
key: '1',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
1st menu item
</a>
),
},
{
key: '2',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
2nd menu item
</a>
),
},
{
key: '3',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
3rd menu item
</a>
),
},
]}
/>
);
const App: React.FC = () => (
<>
<Dropdown overlay={menu} placement="bottomLeft" arrow>
<Button>bottomLeft</Button>
</Dropdown>
<Dropdown overlay={menu} placement="bottom" arrow>
<Button>bottom</Button>
</Dropdown>
<Dropdown overlay={menu} placement="bottomRight" arrow>
<Button>bottomRight</Button>
</Dropdown>
<br />
<Dropdown overlay={menu} placement="topLeft" arrow>
<Button>topLeft</Button>
</Dropdown>
<Dropdown overlay={menu} placement="top" arrow>
<Button>top</Button>
</Dropdown>
<Dropdown overlay={menu} placement="topRight" arrow>
<Button>topRight</Button>
</Dropdown>
</>
);
export default App;
#components-dropdown-demo-arrow .ant-btn {
margin-right: 8px;
margin-bottom: 8px;
}
.ant-row-rtl #components-dropdown-demo-arrow .ant-btn {
margin-right: 0;
margin-bottom: 8px;
margin-left: 8px;
}
TypeScript
JavaScript
import { Button, Dropdown, Menu } from 'infrad';
import React from 'react';
const menu = (
<Menu
items={[
{
key: '1',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
1st menu item
</a>
),
},
{
key: '2',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
2nd menu item
</a>
),
},
{
key: '3',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
3rd menu item
</a>
),
},
]}
/>
);
const App: React.FC = () => (
<>
<Dropdown overlay={menu} placement="bottomLeft" arrow={{ pointAtCenter: true }}>
<Button>bottomLeft</Button>
</Dropdown>
<Dropdown overlay={menu} placement="bottom" arrow={{ pointAtCenter: true }}>
<Button>bottom</Button>
</Dropdown>
<Dropdown overlay={menu} placement="bottomRight" arrow={{ pointAtCenter: true }}>
<Button>bottomRight</Button>
</Dropdown>
<br />
<Dropdown overlay={menu} placement="topLeft" arrow={{ pointAtCenter: true }}>
<Button>topLeft</Button>
</Dropdown>
<Dropdown overlay={menu} placement="top" arrow={{ pointAtCenter: true }}>
<Button>top</Button>
</Dropdown>
<Dropdown overlay={menu} placement="topRight" arrow={{ pointAtCenter: true }}>
<Button>topRight</Button>
</Dropdown>
</>
);
export default App;
#components-dropdown-demo-arrow-center .ant-btn {
margin-right: 8px;
margin-bottom: 8px;
}
.ant-row-rtl #components-dropdown-demo-arrow-center .ant-btn {
margin-right: 0;
margin-bottom: 8px;
margin-left: 8px;
}
Hover me, Click menu item
TypeScript
JavaScript
import { IArrowDown } from 'infra-design-icons';
import type { MenuProps } from 'infrad';
import { Dropdown, Menu, message, Space } from 'infrad';
import React from 'react';
const onClick: MenuProps['onClick'] = ({ key }) => {
message.info(`Click on item ${key}`);
};
const menu = (
<Menu
onClick={onClick}
items={[
{
label: '1st menu item',
key: '1',
},
{
label: '2nd menu item',
key: '2',
},
{
label: '3rd menu item',
key: '3',
},
]}
/>
);
const App: React.FC = () => (
<Dropdown overlay={menu}>
<a onClick={e => e.preventDefault()}>
<Space>
Hover me, Click menu item
<IArrowDown />
</Space>
</a>
</Dropdown>
);
export default App;
Selectable
TypeScript
JavaScript
import { DownOutlined } from '@ant-design/icons';
import { Dropdown, Menu, Space, Typography } from 'antd';
import React from 'react';
const menu = (
<Menu
selectable
defaultSelectedKeys={['3']}
items={[
{
key: '1',
label: 'Item 1',
},
{
key: '2',
label: 'Item 2',
},
{
key: '3',
label: 'Item 3',
},
]}
/>
);
const App: React.FC = () => (
<Dropdown overlay={menu}>
<Typography.Link>
<Space>
Selectable
<DownOutlined />
</Space>
</Typography.Link>
</Dropdown>
);
export default App;
TypeScript
JavaScript
import { Button, Dropdown, Menu, Space } from 'infrad';
import React from 'react';
const menu = (
<Menu
items={[
{
key: '1',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
1st menu item
</a>
),
},
{
key: '2',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
2nd menu item
</a>
),
},
{
key: '3',
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
3rd menu item
</a>
),
},
]}
/>
);
const App: React.FC = () => (
<Space direction="vertical">
<Space wrap>
<Dropdown overlay={menu} placement="bottomLeft">
<Button>bottomLeft</Button>
</Dropdown>
<Dropdown overlay={menu} placement="bottom">
<Button>bottom</Button>
</Dropdown>
<Dropdown overlay={menu} placement="bottomRight">
<Button>bottomRight</Button>
</Dropdown>
</Space>
<Space wrap>
<Dropdown overlay={menu} placement="topLeft">
<Button>topLeft</Button>
</Dropdown>
<Dropdown overlay={menu} placement="top">
<Button>top</Button>
</Dropdown>
<Dropdown overlay={menu} placement="topRight">
<Button>topRight</Button>
</Dropdown>
</Space>
</Space>
);
export default App;
Hover me
TypeScript
JavaScript
import { IArrowDown } from 'infra-design-icons';
import { Dropdown, Menu, Space } from 'infrad';
import React from 'react';
const menu = (
<Menu
items={[
{
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
1st menu item
</a>
),
key: '0',
},
{
label: (
<a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
2nd menu item
</a>
),
key: '1',
},
{
type: 'divider',
},
{
label: '3rd menu item(disabled)',
key: '3',
disabled: true,
},
]}
/>
);
const App: React.FC = () => (
<Dropdown overlay={menu}>
<a onClick={e => e.preventDefault()}>
<Space>
Hover me
<IArrowDown />
</Space>
</a>
</Dropdown>
);
export default App;
Click me
TypeScript
JavaScript
import { IArrowDown } from 'infra-design-icons';
import { Dropdown, Menu, Space } from 'infrad';
import React from 'react';
const menu = (
<Menu
items={[
{
label: <a href="https://www.antgroup.com">1st menu item</a>,
key: '0',
},
{
label: <a href="https://www.aliyun.com">2nd menu item</a>,
key: '1',
},
{
type: 'divider',
},
{
label: '3rd menu item',
key: '3',
},
]}
/>
);
const App: React.FC = () => (
<Dropdown overlay={menu} trigger={['click']}>
<a onClick={e => e.preventDefault()}>
<Space>
Click me
<IArrowDown />
</Space>
</a>
</Dropdown>
);
export default App;
TypeScript
JavaScript
import { IArrowDown, UserOutlined } from 'infra-design-icons';
import type { MenuProps } from 'infrad';
import { Button, Dropdown, Menu, message, Space, Tooltip } from 'infrad';
import React from 'react';
const handleButtonClick = (e: React.MouseEvent<HTMLButtonElement>) => {
message.info('Click on left button.');
console.log('click left button', e);
};
const handleMenuClick: MenuProps['onClick'] = e => {
message.info('Click on menu item.');
console.log('click', e);
};
const menu = (
<Menu
onClick={handleMenuClick}
items={[
{
label: '1st menu item',
key: '1',
icon: <UserOutlined />,
},
{
label: '2nd menu item',
key: '2',
icon: <UserOutlined />,
},
{
label: '3rd menu item',
key: '3',
icon: <UserOutlined />,
},
]}
/>
);
const App: React.FC = () => (
<Space wrap>
<Dropdown.Button onClick={handleButtonClick} overlay={menu}>
Dropdown
</Dropdown.Button>
<Dropdown.Button overlay={menu} placement="bottom" icon={<UserOutlined />}>
Dropdown
</Dropdown.Button>
<Dropdown.Button onClick={handleButtonClick} overlay={menu} disabled>
Dropdown
</Dropdown.Button>
<Dropdown.Button
overlay={menu}
buttonsRender={([leftButton, rightButton]) => [
<Tooltip title="tooltip" key="leftButton">
{leftButton}
</Tooltip>,
React.cloneElement(rightButton as React.ReactElement<any, string>, { loading: true }),
]}
>
With Tooltip
</Dropdown.Button>
<Dropdown overlay={menu}>
<Button>
<Space>
Button
<IArrowDown />
</Space>
</Button>
</Dropdown>
</Space>
);
export default App;
Hover me
TypeScript
JavaScript
import { IArrowDown } from 'infra-design-icons';
import type { MenuProps } from 'infrad';
import { Dropdown, Menu, Space } from 'infrad';
import React, { useState } from 'react';
const App: React.FC = () => {
const [visible, setVisible] = useState(false);
const handleMenuClick: MenuProps['onClick'] = e => {
if (e.key === '3') {
setVisible(false);
}
};
const handleVisibleChange = (flag: boolean) => {
setVisible(flag);
};
const menu = (
<Menu
onClick={handleMenuClick}
items={[
{
label: 'Clicking me will not close the menu.',
key: '1',
},
{
label: 'Clicking me will not close the menu also.',
key: '2',
},
{
label: 'Clicking me will close the menu.',
key: '3',
},
]}
/>
);
return (
<Dropdown overlay={menu} onVisibleChange={handleVisibleChange} visible={visible}>
<a onClick={e => e.preventDefault()}>
<Space>
Hover me
<IArrowDown />
</Space>
</a>
</Dropdown>
);
};
export default App;
TypeScript
JavaScript
import { DownOutlined } from 'infra-design-icons';
import { Dropdown, Menu, Space } from 'infrad';
import React, { useState } from 'react';
const menu = (
<Menu
items={[
{
label: 'Submit and continue',
key: '1',
},
]}
/>
);
const App: React.FC = () => {
const [loadings, setLoadings] = useState<boolean[]>([]);
const enterLoading = (index: number) => {
setLoadings(state => {
const newLoadings = [...state];
newLoadings[index] = true;
return newLoadings;
});
setTimeout(() => {
setLoadings(state => {
const newLoadings = [...state];
newLoadings[index] = false;
return newLoadings;
});
}, 6000);
};
return (
<Space direction="vertical">
<Dropdown.Button type="primary" loading overlay={menu}>
Submit
</Dropdown.Button>
<Dropdown.Button type="primary" size="small" loading overlay={menu}>
Submit
</Dropdown.Button>
<Dropdown.Button
type="primary"
loading={loadings[0]}
overlay={menu}
onClick={() => enterLoading(0)}
>
Submit
</Dropdown.Button>
<Dropdown.Button
icon={<DownOutlined />}
loading={loadings[1]}
overlay={menu}
onClick={() => enterLoading(1)}
>
Submit
</Dropdown.Button>
</Space>
);
};
export default App;
API#
Dropdown#
Property | Description | Type | Default | Version |
---|---|---|---|---|
arrow | Whether the dropdown arrow should be visible | boolean | { pointAtCenter: boolean } | false | |
autoFocus | Focus element in overlay when opened | boolean | false | 4.21.0 |
disabled | Whether the dropdown menu is disabled | boolean | - | |
destroyPopupOnHide | Whether destroy dropdown when hidden | boolean | false | |
getPopupContainer | To set the container of the dropdown menu. The default is to create a div element in body, but you can reset it to the scrolling area and make a relative reposition. Example on CodePen | (triggerNode: HTMLElement) => HTMLElement | () => document.body | |
overlay | The dropdown menu | Menu | () => Menu | - | |
overlayClassName | The class name of the dropdown root element | string | - | |
overlayStyle | The style of the dropdown root element | CSSProperties | - | |
placement | Placement of popup menu: bottom bottomLeft bottomRight top topLeft topRight | string | bottomLeft | |
trigger | The trigger mode which executes the dropdown action. Note that hover can't be used on touchscreens | Array<click |hover |contextMenu > | [hover ] | |
visible | Whether the dropdown menu is currently visible | boolean | - | |
onVisibleChange | Called when the visible state is changed. Not trigger when hidden by click item | (visible: boolean) => void | - |
You should use Menu as overlay
. The menu items and dividers are also available by using Menu.Item
and Menu.Divider
.
Warning: You must set a unique
key
forMenu.Item
.Menu of Dropdown is unselectable by default, you can make it selectable via
<Menu selectable>
.
Dropdown.Button#
Property | Description | Type | Default | Version |
---|---|---|---|---|
buttonsRender | Custom buttons inside Dropdown.Button | (buttons: ReactNode[]) => ReactNode[] | - | |
loading | Set the loading status of button | boolean | { delay: number } | false | |
disabled | Whether the dropdown menu is disabled | boolean | - | |
icon | Icon (appears on the right) | ReactNode | - | |
overlay | The dropdown menu | Menu | - | |
placement | Placement of popup menu: bottom bottomLeft bottomRight top topLeft topRight | string | bottomLeft | |
size | Size of the button, the same as Button | string | default | |
trigger | The trigger mode which executes the dropdown action | Array<click |hover |contextMenu > | [hover ] | |
type | Type of the button, the same as Button | string | default | |
visible | Whether the dropdown menu is currently visible | boolean | - | |
onClick | The same as Button: called when you click the button on the left | (event) => void | - | |
onVisibleChange | Called when the visible state is changed | (visible: boolean) => void | - |