Skip to content

Fuse.Design Dropdown 下拉菜单

下拉菜单是一种向下弹出的列表组件,适用于页面操作命令过多时,收纳操作元素。用户通过点击或移入触点,可展开下拉菜单进行选择并执行相应命令,与用于选择的 Select 组件不同,Dropdown 是命令集合。

一、代码演示

  1. 基本:最简单的下拉菜单示例,通过 a-dropdown 组件,在触发元素内设置 @click.prevent 阻止默认点击行为,使用 #overlay 插槽定义菜单内容。
vue
<template>
  <a-dropdown>
    <a class="ant-dropdown-link" @click.prevent>
      Hover me
      <DownOutlined />
    </a>
    <template #overlay>
      <a-menu>
        <a-menu-item>
          <a href="javascript:;">1st menu item</a>
        </a-menu-item>
        <a-menu-item>
          <a href="javascript:;">2nd menu item</a>
        </a-menu-item>
        <a-menu-item>
          <a href="javascript:;">3rd menu item</a>
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
</template>
<script lang="ts" setup>
import { DownOutlined } from '@ant-design/icons-vue';
</script>
  1. 右键菜单:默认移入触发菜单,通过设置 :trigger="['contextmenu']" 可改为右键点击触发。
Right Click on here
vue
<template>
  <a-dropdown :trigger="['contextmenu']">
    <div
      :style="{
        textAlign: 'center',
        background: '#f7f7f7',
        height: '200px',
        lineHeight: '200px',
        color: '#777',
      }"
    >
      Right Click on here
    </div>
    <template #overlay>
      <a-menu>
        <a-menu-item key="1">1st menu item</a-menu-item>
        <a-menu-item key="2">2nd menu item</a-menu-item>
        <a-menu-item key="3">3rd menu item</a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
</template>
  1. 带下拉框的按钮:包含按钮与右侧功能菜单,可设置 icon 属性修改图标,同时展示了按钮禁用等状态。
vue
<template>
  <div class="demo-dropdown-wrap">
    <a-dropdown-button @click="handleButtonClick">
      Dropdown
      <template #overlay>
        <a-menu @click="handleMenuClick">
          <a-menu-item key="1">
            <UserOutlined />
            1st menu item
          </a-menu-item>
          <a-menu-item key="2">
            <UserOutlined />
            2nd menu item
          </a-menu-item>
          <a-menu-item key="3">
            <UserOutlined />
            3rd item
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown-button>
    <a-dropdown-button>
      Dropdown
      <template #overlay>
        <a-menu @click="handleMenuClick">
          <a-menu-item key="1">
            <UserOutlined />
            1st menu item
          </a-menu-item>
          <a-menu-item key="2">
            <UserOutlined />
            2nd menu item
          </a-menu-item>
          <a-menu-item key="3">
            <UserOutlined />
            3rd item
          </a-menu-item>
        </a-menu>
      </template>
      <template #icon><UserOutlined /></template>
    </a-dropdown-button>
    <a-dropdown-button disabled @click="handleButtonClick">
      Dropdown
      <template #overlay>
        <a-menu @click="handleMenuClick">
          <a-menu-item key="1">
            <UserOutlined />
            1st menu item
          </a-menu-item>
          <a-menu-item key="2">
            <UserOutlined />
            2nd menu item
          </a-menu-item>
          <a-menu-item key="3">
            <UserOutlined />
            3rd item
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown-button>
    <a-dropdown>
      <template #overlay>
        <a-menu @click="handleMenuClick">
          <a-menu-item key="1">
            <UserOutlined />
            1st menu item
          </a-menu-item>
          <a-menu-item key="2">
            <UserOutlined />
            2nd menu item
          </a-menu-item>
          <a-menu-item key="3">
            <UserOutlined />
            3rd item
          </a-menu-item>
        </a-menu>
      </template>
      <a-button>
        Button
        <DownOutlined />
      </a-button>
    </a-dropdown>
  </div>
</template>
<script lang="ts" setup>
import { UserOutlined, DownOutlined } from '@ant-design/icons-vue'
import type { MenuProps } from '@design-middle-center/fuse-design-vue'
const handleButtonClick = (e: Event) => {
  console.log('click left button', e)
}
const handleMenuClick: MenuProps['onClick'] = (e) => {
  console.log('click', e)
}
</script>
<style scoped>
.demo-dropdown-wrap :deep(.ant-dropdown-button) {
  margin-right: 8px;
  margin-bottom: 8px;
}
</style>
  1. 加载中状态:通过设置 loading 属性让按钮处于加载状态。
vue
<template>
  <a-space direction="vertical">
    <a-dropdown-button type="primary" loading>
      <template #overlay>
        <a-menu>
          <a-menu-item key="1">Submit and continue</a-menu-item>
        </a-menu>
      </template>
      Submit
    </a-dropdown-button>
    <a-dropdown-button type="primary" size="small" loading>
      <template #overlay>
        <a-menu>
          <a-menu-item key="1">Submit and continue</a-menu-item>
        </a-menu>
      </template>
      Submit
    </a-dropdown-button>
    <a-dropdown-button type="primary" :loading="loading1" @click="enterLoading1">
      <template #overlay>
        <a-menu>
          <a-menu-item key="1">Submit and continue</a-menu-item>
        </a-menu>
      </template>
      Submit
    </a-dropdown-button>
    <a-dropdown-button :loading="loading2" @click="enterLoading2">
      Submit
      <template #overlay>
        <a-menu>
          <a-menu-item key="1">Submit and continue</a-menu-item>
        </a-menu>
      </template>
      <template #icon><DownOutlined /></template>
    </a-dropdown-button>
  </a-space>
</template>
<script lang="ts" setup>
import { Ref, ref } from 'vue';
import { DownOutlined } from '@ant-design/icons-vue';
const loading1 = ref(false);
const loading2 = ref(false);
const enterLoading = (loading: Ref<boolean>) => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 6000);
};
function enterLoading1() {
  enterLoading(loading1);
}
function enterLoading2() {
  enterLoading(loading2);
}
</script>

API

属性如下

参数说明类型默认值版本
align该值将合并到placement的配置中,设置参考dom-alignObject--
arrow下拉框箭头是否显示。取值可以是布尔值,当为true时显示箭头;也可以是一个对象{ pointAtCenter: boolean }pointAtCenter设为true时箭头指向目标元素中心booleanfalse
destroyPopupOnHide关闭后是否销毁Dropdownbooleanfalse3.0
disabled菜单是否禁用boolean--
getPopupContainer菜单渲染父节点。默认渲染到body上,如果遇到菜单滚动定位问题,可修改为滚动的区域,并相对其定位。该函数接收触发节点triggerNode作为参数Function(triggerNode)() => document.body-
overlay(v-slot)菜单内容,需使用Menu组件,还可包含菜单项Menu.Item、分割线Menu.Divider 。注意Menu.Item必须设置唯一的key属性Menu--
overlayClassName下拉根元素的类名称string--
overlayStyle下拉根元素的样式object--
placement菜单弹出位置,可选值为bottomLeftbottombottomRighttopLefttoptopRightbottomLeftbottombottomRight
trigger触发下拉的行为,移动端不支持hover。取值为数组,可选值为clickhovercontextmenuArray<clickhovercontextmenu>
open(v-model)菜单是否显示boolean--

overlay菜单使用Menu,还包括菜单项Menu.Item,分割线Menu.Divider。注意:Menu.Item必须设置唯一的key属性。Dropdown下的Menu默认不可选中。如果需要菜单可选中,可以指定<Menu selectable>

事件

事件名称说明回调参数版本
openChange菜单显示状态改变时调用,参数为visible。点击菜单按钮导致的消失不会触发function(open)4.0
参数说明类型默认值版本
disabled菜单是否禁用boolean--
icon右侧的icon,可以是VNode或通过插槽slot传入VNodeslot-
loading设置按钮载入状态,可以是布尔值,也可以是一个包含delay属性(延迟显示加载状态的时长,单位为毫秒)的对象booleanfalse
overlay(v-slot)菜单内容,需使用Menu组件Menu--
placement菜单弹出位置,可选值为bottomLeftbottombottomRighttopLefttoptopRightbottomLeftbottombottomRight
size按钮大小,和Button组件一致string'default'-
trigger触发下拉的行为,取值为数组,可选值为clickhovercontextmenuArray<clickhovercontextmenu>
type按钮类型,和Button组件一致string'default'-
open(v-model)菜单是否显示boolean--
事件名称说明回调参数版本
click点击左侧按钮的回调,和Button组件一致Function-
openChange菜单显示状态改变时调用,参数为visible。点击菜单按钮导致的消失不会触发function(open)4.0