Dropdown下拉菜单

向下弹出的列表。

何时使用

当页面上的操作命令过多时,用此组件可以收纳操作元素。点击或移入触点,会出现一个下拉菜单。可在列表中进行选择,并执行相应的命令。

  • 用于收罗一组命令操作。

  • Select 用于选择,而 Dropdown 是命令集合。

代码演示

Dropdown下拉菜单 - 图1

基本

最简单的下拉菜单。

  1. import { Menu, Dropdown } from 'antd';
  2. import { DownOutlined } from '@ant-design/icons';
  3. const menu = (
  4. <Menu>
  5. <Menu.Item>
  6. <a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
  7. 1st menu item
  8. </a>
  9. </Menu.Item>
  10. <Menu.Item icon={<DownOutlined />} disabled>
  11. <a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
  12. 2nd menu item
  13. </a>
  14. </Menu.Item>
  15. <Menu.Item disabled>
  16. <a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
  17. 3rd menu item
  18. </a>
  19. </Menu.Item>
  20. <Menu.Item danger>a danger item</Menu.Item>
  21. </Menu>
  22. );
  23. ReactDOM.render(
  24. <Dropdown overlay={menu}>
  25. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  26. Hover me <DownOutlined />
  27. </a>
  28. </Dropdown>,
  29. mountNode,
  30. );

Dropdown下拉菜单 - 图2

箭头

可以展示一个箭头。

  1. import { Menu, Dropdown, Button } from 'antd';
  2. const menu = (
  3. <Menu>
  4. <Menu.Item>
  5. <a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
  6. 1st menu item
  7. </a>
  8. </Menu.Item>
  9. <Menu.Item>
  10. <a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
  11. 2nd menu item
  12. </a>
  13. </Menu.Item>
  14. <Menu.Item>
  15. <a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
  16. 3rd menu item
  17. </a>
  18. </Menu.Item>
  19. </Menu>
  20. );
  21. ReactDOM.render(
  22. <>
  23. <Dropdown overlay={menu} placement="bottomLeft" arrow>
  24. <Button>bottomLeft</Button>
  25. </Dropdown>
  26. <Dropdown overlay={menu} placement="bottomCenter" arrow>
  27. <Button>bottomCenter</Button>
  28. </Dropdown>
  29. <Dropdown overlay={menu} placement="bottomRight" arrow>
  30. <Button>bottomRight</Button>
  31. </Dropdown>
  32. <br />
  33. <Dropdown overlay={menu} placement="topLeft" arrow>
  34. <Button>topLeft</Button>
  35. </Dropdown>
  36. <Dropdown overlay={menu} placement="topCenter" arrow>
  37. <Button>topCenter</Button>
  38. </Dropdown>
  39. <Dropdown overlay={menu} placement="topRight" arrow>
  40. <Button>topRight</Button>
  41. </Dropdown>
  42. </>,
  43. mountNode,
  44. );
  1. #components-dropdown-demo-arrow .ant-btn {
  2. margin-right: 8px;
  3. margin-bottom: 8px;
  4. }
  5. .ant-row-rtl #components-dropdown-demo-arrow .ant-btn {
  6. margin-right: 0;
  7. margin-bottom: 8px;
  8. margin-left: 8px;
  9. }

Dropdown下拉菜单 - 图3

触发方式

默认是移入触发菜单,可以点击触发。

  1. import { Menu, Dropdown } from 'antd';
  2. import { DownOutlined } from '@ant-design/icons';
  3. const menu = (
  4. <Menu>
  5. <Menu.Item key="0">
  6. <a href="https://www.antgroup.com">1st menu item</a>
  7. </Menu.Item>
  8. <Menu.Item key="1">
  9. <a href="https://www.aliyun.com">2nd menu item</a>
  10. </Menu.Item>
  11. <Menu.Divider />
  12. <Menu.Item key="3">3rd menu item</Menu.Item>
  13. </Menu>
  14. );
  15. ReactDOM.render(
  16. <Dropdown overlay={menu} trigger={['click']}>
  17. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  18. Click me <DownOutlined />
  19. </a>
  20. </Dropdown>,
  21. mountNode,
  22. );

Dropdown下拉菜单 - 图4

带下拉框的按钮

左边是按钮,右边是额外的相关功能菜单。可设置 icon 属性来修改右边的图标。

  1. import { Menu, Dropdown, Button, message, Space, Tooltip } from 'antd';
  2. import { DownOutlined, UserOutlined } from '@ant-design/icons';
  3. function handleButtonClick(e) {
  4. message.info('Click on left button.');
  5. console.log('click left button', e);
  6. }
  7. function handleMenuClick(e) {
  8. message.info('Click on menu item.');
  9. console.log('click', e);
  10. }
  11. const menu = (
  12. <Menu onClick={handleMenuClick}>
  13. <Menu.Item key="1" icon={<UserOutlined />}>
  14. 1st menu item
  15. </Menu.Item>
  16. <Menu.Item key="2" icon={<UserOutlined />}>
  17. 2nd menu item
  18. </Menu.Item>
  19. <Menu.Item key="3" icon={<UserOutlined />}>
  20. 3rd menu item
  21. </Menu.Item>
  22. </Menu>
  23. );
  24. ReactDOM.render(
  25. <Space wrap>
  26. <Dropdown.Button onClick={handleButtonClick} overlay={menu}>
  27. Dropdown
  28. </Dropdown.Button>
  29. <Dropdown.Button overlay={menu} placement="bottomCenter" icon={<UserOutlined />}>
  30. Dropdown
  31. </Dropdown.Button>
  32. <Dropdown.Button onClick={handleButtonClick} overlay={menu} disabled>
  33. Dropdown
  34. </Dropdown.Button>
  35. <Dropdown.Button
  36. overlay={menu}
  37. buttonsRender={([leftButton, rightButton]) => [
  38. <Tooltip title="tooltip" key="leftButton">
  39. {leftButton}
  40. </Tooltip>,
  41. React.cloneElement(rightButton, { loading: true }),
  42. ]}
  43. >
  44. With Tooltip
  45. </Dropdown.Button>
  46. <Dropdown overlay={menu}>
  47. <Button>
  48. Button <DownOutlined />
  49. </Button>
  50. </Dropdown>
  51. </Space>,
  52. mountNode,
  53. );

Dropdown下拉菜单 - 图5

菜单隐藏方式

默认是点击关闭菜单,可以关闭此功能。

  1. import { Menu, Dropdown } from 'antd';
  2. import { DownOutlined } from '@ant-design/icons';
  3. class OverlayVisible extends React.Component {
  4. state = {
  5. visible: false,
  6. };
  7. handleMenuClick = e => {
  8. if (e.key === '3') {
  9. this.setState({ visible: false });
  10. }
  11. };
  12. handleVisibleChange = flag => {
  13. this.setState({ visible: flag });
  14. };
  15. render() {
  16. const menu = (
  17. <Menu onClick={this.handleMenuClick}>
  18. <Menu.Item key="1">Clicking me will not close the menu.</Menu.Item>
  19. <Menu.Item key="2">Clicking me will not close the menu also.</Menu.Item>
  20. <Menu.Item key="3">Clicking me will close the menu.</Menu.Item>
  21. </Menu>
  22. );
  23. return (
  24. <Dropdown
  25. overlay={menu}
  26. onVisibleChange={this.handleVisibleChange}
  27. visible={this.state.visible}
  28. >
  29. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  30. Hover me <DownOutlined />
  31. </a>
  32. </Dropdown>
  33. );
  34. }
  35. }
  36. ReactDOM.render(<OverlayVisible />, mountNode);

Dropdown下拉菜单 - 图6

弹出位置

支持 6 个弹出位置。

  1. import { Menu, Dropdown, Button, Space } from 'antd';
  2. const menu = (
  3. <Menu>
  4. <Menu.Item>
  5. <a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
  6. 1st menu item
  7. </a>
  8. </Menu.Item>
  9. <Menu.Item>
  10. <a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
  11. 2nd menu item
  12. </a>
  13. </Menu.Item>
  14. <Menu.Item>
  15. <a target="_blank" rel="noopener noreferrer" href="https://www.luohanacademy.com">
  16. 3rd menu item
  17. </a>
  18. </Menu.Item>
  19. </Menu>
  20. );
  21. ReactDOM.render(
  22. <Space direction="vertical">
  23. <Space wrap>
  24. <Dropdown overlay={menu} placement="bottomLeft">
  25. <Button>bottomLeft</Button>
  26. </Dropdown>
  27. <Dropdown overlay={menu} placement="bottomCenter">
  28. <Button>bottomCenter</Button>
  29. </Dropdown>
  30. <Dropdown overlay={menu} placement="bottomRight">
  31. <Button>bottomRight</Button>
  32. </Dropdown>
  33. </Space>
  34. <Space wrap>
  35. <Dropdown overlay={menu} placement="topLeft">
  36. <Button>topLeft</Button>
  37. </Dropdown>
  38. <Dropdown overlay={menu} placement="topCenter">
  39. <Button>topCenter</Button>
  40. </Dropdown>
  41. <Dropdown overlay={menu} placement="topRight">
  42. <Button>topRight</Button>
  43. </Dropdown>
  44. </Space>
  45. </Space>,
  46. mountNode,
  47. );

Dropdown下拉菜单 - 图7

其他元素

分割线和不可用菜单项。

  1. import { Menu, Dropdown } from 'antd';
  2. import { DownOutlined } from '@ant-design/icons';
  3. const menu = (
  4. <Menu>
  5. <Menu.Item key="0">
  6. <a target="_blank" rel="noopener noreferrer" href="https://www.antgroup.com">
  7. 1st menu item
  8. </a>
  9. </Menu.Item>
  10. <Menu.Item key="1">
  11. <a target="_blank" rel="noopener noreferrer" href="https://www.aliyun.com">
  12. 2nd menu item
  13. </a>
  14. </Menu.Item>
  15. <Menu.Divider />
  16. <Menu.Item key="3" disabled>
  17. 3rd menu itemdisabled
  18. </Menu.Item>
  19. </Menu>
  20. );
  21. ReactDOM.render(
  22. <Dropdown overlay={menu}>
  23. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  24. Hover me <DownOutlined />
  25. </a>
  26. </Dropdown>,
  27. mountNode,
  28. );

Dropdown下拉菜单 - 图8

触发事件

点击菜单项后会触发事件,用户可以通过相应的菜单项 key 进行不同的操作。

  1. import { Menu, Dropdown, message } from 'antd';
  2. import { DownOutlined } from '@ant-design/icons';
  3. const onClick = ({ key }) => {
  4. message.info(`Click on item ${key}`);
  5. };
  6. const menu = (
  7. <Menu onClick={onClick}>
  8. <Menu.Item key="1">1st menu item</Menu.Item>
  9. <Menu.Item key="2">2nd menu item</Menu.Item>
  10. <Menu.Item key="3">3rd menu item</Menu.Item>
  11. </Menu>
  12. );
  13. ReactDOM.render(
  14. <Dropdown overlay={menu}>
  15. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  16. Hover me, Click menu item <DownOutlined />
  17. </a>
  18. </Dropdown>,
  19. mountNode,
  20. );

Dropdown下拉菜单 - 图9

多级菜单

传入的菜单里有多个层级。

  1. import { Menu, Dropdown } from 'antd';
  2. import { DownOutlined } from '@ant-design/icons';
  3. const { SubMenu } = Menu;
  4. const menu = (
  5. <Menu>
  6. <Menu.ItemGroup title="Group title">
  7. <Menu.Item>1st menu item</Menu.Item>
  8. <Menu.Item>2nd menu item</Menu.Item>
  9. </Menu.ItemGroup>
  10. <SubMenu title="sub menu">
  11. <Menu.Item>3rd menu item</Menu.Item>
  12. <Menu.Item>4th menu item</Menu.Item>
  13. </SubMenu>
  14. <SubMenu title="disabled sub menu" disabled>
  15. <Menu.Item>5d menu item</Menu.Item>
  16. <Menu.Item>6th menu item</Menu.Item>
  17. </SubMenu>
  18. </Menu>
  19. );
  20. ReactDOM.render(
  21. <Dropdown overlay={menu}>
  22. <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
  23. Cascading menu <DownOutlined />
  24. </a>
  25. </Dropdown>,
  26. mountNode,
  27. );

Dropdown下拉菜单 - 图10

右键菜单

默认是移入触发菜单,可以点击鼠标右键触发。

  1. import { Menu, Dropdown } from 'antd';
  2. const menu = (
  3. <Menu>
  4. <Menu.Item key="1">1st menu item</Menu.Item>
  5. <Menu.Item key="2">2nd menu item</Menu.Item>
  6. <Menu.Item key="3">3rd menu item</Menu.Item>
  7. </Menu>
  8. );
  9. ReactDOM.render(
  10. <Dropdown overlay={menu} trigger={['contextMenu']}>
  11. <div
  12. className="site-dropdown-context-menu"
  13. style={{
  14. textAlign: 'center',
  15. height: 200,
  16. lineHeight: '200px',
  17. }}
  18. >
  19. Right Click on here
  20. </div>
  21. </Dropdown>,
  22. mountNode,
  23. );
  1. .site-dropdown-context-menu {
  2. color: #777;
  3. background: #f7f7f7;
  4. }

API

属性如下

参数说明类型默认值版本
arrow下拉框箭头是否显示booleanfalse
disabled菜单是否禁用boolean-
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。示例(triggerNode: HTMLElement) => HTMLElement() => document.body
overlay菜单Menu | () => Menu-
overlayClassName下拉根元素的类名称string-
overlayStyle下拉根元素的样式CSSProperties-
placement菜单弹出位置:bottomLeft bottomCenter bottomRight topLeft topCenter topRightstringbottomLeft
trigger触发下拉的行为, 移动端不支持 hoverArray<click|hover|contextMenu>[hover]
visible菜单是否显示boolean-
onVisibleChange菜单显示状态改变时调用,参数为 visible(visible: boolean) => void-

overlay 菜单使用 Menu,还包括菜单项 Menu.Item,分割线 Menu.Divider

注意: Menu.Item 必须设置唯一的 key 属性。

Dropdown 下的 Menu 默认不可选中。如果需要菜单可选中,可以指定 <Menu selectable>

Dropdown.Button

参数说明类型默认值版本
buttonsRender自定义左右两个按钮(buttons: ReactNode[]) => ReactNode[]-
disabled菜单是否禁用boolean-
icon右侧的 iconReactNode-
overlay菜单Menu-
placement菜单弹出位置:bottomLeft bottomCenter bottomRight topLeft topCenter topRightstringbottomLeft
size按钮大小,和 Button 一致stringdefault
trigger触发下拉的行为Array<click|hover|contextMenu>[hover]
type按钮类型,和 Button 一致stringdefault
visible菜单是否显示boolean-
onClick点击左侧按钮的回调,和 Button 一致(event) => void-
onVisibleChange菜单显示状态改变时调用,参数为 visible(visible: boolean) => void-