Dropdown下拉菜单

向下弹出的列表。

何时使用#

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

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

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

代码演示

Hover me

最简单的下拉菜单。

expand codeexpand code
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;

可以展示一个箭头。

expand codeexpand code
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;
}

设置 arrow{ pointAtCenter: true } 后,箭头将指向目标元素的中心。

expand codeexpand code
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

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

expand codeexpand code
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;
Cascading menu

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

expand codeexpand code
import { IArrowDown } from 'infra-design-icons';
import { Dropdown, Menu, Space } from 'infrad';
import React from 'react';

const menu = (
  <Menu
    items={[
      {
        key: '1',
        type: 'group',
        label: 'Group title',
        children: [
          {
            key: '1-1',
            label: '1st menu item',
          },
          {
            key: '1-2',
            label: '2nd menu item',
          },
        ],
      },
      {
        key: '2',
        label: 'sub menu',
        children: [
          {
            key: '2-1',
            label: '3rd menu item',
          },
          {
            key: '2-2',
            label: '4th menu item',
          },
        ],
      },
      {
        key: '3',
        label: 'disabled sub menu',
        disabled: true,
        children: [
          {
            key: '3-1',
            label: '5d menu item',
          },
          {
            key: '3-2',
            label: '6th menu item',
          },
        ],
      },
    ]}
  />
);

const App: React.FC = () => (
  <Dropdown overlay={menu}>
    <a onClick={e => e.preventDefault()}>
      <Space>
        Cascading menu
        <IArrowDown />
      </Space>
    </a>
  </Dropdown>
);

export default App;

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

expand codeexpand code
import { Dropdown, Menu } from 'infrad';
import React from 'react';

const menu = (
  <Menu
    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} trigger={['contextMenu']}>
    <div
      className="site-dropdown-context-menu"
      style={{
        textAlign: 'center',
        height: 200,
        lineHeight: '200px',
      }}
    >
      Right Click on here
    </div>
  </Dropdown>
);

export default App;
.site-dropdown-context-menu {
  color: #777;
  background: #f7f7f7;
}
Selectable

为 Menu 添加 selectable 属性可以开启选择能力。

expand codeexpand code
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;

支持 6 个弹出位置。

expand codeexpand code
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

分割线和不可用菜单项。

expand codeexpand code
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

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

expand codeexpand code
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;

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

expand codeexpand code
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

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

expand codeexpand code
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;

添加 loading 属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。

expand codeexpand code
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#

属性如下

参数说明类型默认值版本
arrow下拉框箭头是否显示boolean | { pointAtCenter: boolean }false
autoFocus打开后自动聚焦下拉框booleanfalse4.21.0
disabled菜单是否禁用boolean-
destroyPopupOnHide关闭后是否销毁 Dropdownbooleanfalse
getPopupContainer菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。示例(triggerNode: HTMLElement) => HTMLElement() => document.body
overlay菜单Menu | () => Menu-
overlayClassName下拉根元素的类名称string-
overlayStyle下拉根元素的样式CSSProperties-
placement菜单弹出位置:bottom bottomLeft bottomRight top topLeft 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>

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