Skip to content

DropdownMenu 下拉菜单

介绍

向下弹出的菜单列表。

引入

ts
import { IBestDropdownMenu, IBestDropdownItem, IBestDropdownMenuOption, IBestDropdownMenuController } from "@ibestservices/ibest-ui-v2";

代码演示

基础用法

基础用法

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  groupId: string = 'dropdown_menu'
  @Local value: number = 1
  @Local options: IBestDropdownMenuOption[] = [
    { text: '全部商品', value: 1 },
    { text: '第一个新款商品', value: 2 },
    { text: '活动商品', value: 3 },
    { text: '优惠商品', value: 4 },
    { text: '价格商品', value: 5 },
    { text: '折扣商品', value: 6 },
    { text: '精选商品', value: 7 },
    { text: '推荐商品', value: 8 },
    { text: '精选商品', value: 9 },
    { text: '推荐商品', value: 10 },
    { text: '新品商品', value: 11 },
    { text: '优惠商品', value: 12 },
    { text: '价格商品', value: 13 },
    { text: '折扣商品', value: 14 },
    { text: '精选商品', value: 15 },
    { text: '推荐商品', value: 16 },
    { text: '新品商品', value: 17 },
    { text: '优惠商品', value: 18 },
    { text: '价格商品', value: 19 },
    { text: '折扣商品', value: 20 },
    { text: '精选商品', value: 21 },
    { text: '推荐商品', value: 22 },
    { text: '新品商品', value: 23 },
    { text: '优惠商品', value: 24 },
    { text: '价格商品', value: 25 },
    { text: '折扣商品', value: 26 },
    { text: '精选商品', value: 27 },
    { text: '推荐商品', value: 28 },
    { text: '新品商品', value: 29 },
    { text: '优惠商品', value: 30 },
    { text: '最后一个价格商品', value: 31 }
  ]
  @Local value1: string = ''
  @Local options1: IBestDropdownMenuOption[] = [
    { text: '默认排序', value: '' },
    { text: '好评排序', value: 'a' },
    { text: '销量排序', value: 'b' },
  ]
  build() {
    Column(){
      IBestDropdownMenu({ groupId: this.groupId }){
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value!!,
          options: this.options,
          maxHeight: 500
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
      }
    }
  }
}

自定义菜单内容

自定义菜单内容

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  groupId: string = 'dropdown_menu'
  @Local value: number = 1
  @Local options: IBestDropdownMenuOption[] = [
    { text: '全部商品', value: 1 },
    { text: '新款商品', value: 2 },
    { text: '活动商品', value: 3 }
  ]
  @Local switchValue: boolean = true
  @Local switchValue1: boolean = true
  private controller: IBestDropdownMenuController = new IBestDropdownMenuController()
  @Builder switchBuilder(){
    IBestSwitch({
      value: $switchValue
    })
  }
  @Builder switchBuilder1(){
    IBestSwitch({
      value: $switchValue1
    })
  }
  build() {
    Column(){
      IBestDropdownMenu({ groupId: this.groupId, controller: this.controller }){
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value!!,
          options: this.options
        })
        IBestDropdownItem({ groupId: this.groupId, title: "筛选" }){
          IBestCell({
            title: "轻量",
            center: true,
            rightIconBuilder: (): void => this.switchBuilder()
          })
          IBestCell({
            title: "简单",
            center: true,
            rightIconBuilder: (): void => this.switchBuilder1()
          })
          Row(){
            IBestButton({
              type: "primary",
              text: "确定",
              btnWidth: "100%",
              btnHeight: 50,
              round: true,
              onBtnClick: () => {
                this.controller.close()
              }
            })
          }.padding({top: 5, bottom: 5, left: 10, right: 10})
        }
      }
    }
  }
}

自定义样式

自定义样式

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  groupId: string = 'dropdown_menu'
  @Local value: number = 1
  @Local options: IBestDropdownMenuOption[] = [
    { text: '全部商品', icon: "star", value: 1 },
    { text: '新款商品', icon: "fire", iconPosition: 'right', value: 2 },
    { text: '活动商品', icon: "gem", value: 3, disabled: true }
  ]
  @Local value1: string = ''
  @Local options1: IBestDropdownMenuOption[] = [
    { text: '默认排序', value: '' },
    { text: '好评排序', value: 'a' },
    { text: '销量排序', value: 'b' }
  ]
  build() {
    Column(){
      IBestDropdownMenu({
        groupId: this.groupId,
        activeColor: "#ee0a24",
        radius: 8,
        menuHeight: 36,
        menuFontSize: 12,
        menuShadow: { color: 'rgba(100, 101, 102, 0.2)', radius: 12, offsetY: 2 },
        menuIcon: $r("app.media.icon_arrow_down_full")
      }){
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value!!,
          options: this.options
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
      }
    }
  }
}

横向滚动

横向滚动

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  groupId: string = 'dropdown_menu'
  @Local value: number = 1
  @Local options: IBestDropdownMenuOption[] = [
    { text: '全部商品', value: 1 },
    { text: '新款商品', value: 2 },
    { text: '活动商品', value: 3 }
  ]
  @Local value1: string = ''
  @Local options1: IBestDropdownMenuOption[] = [
    { text: '默认排序', value: '' },
    { text: '好评排序', value: 'a' },
    { text: '销量排序', value: 'b' },
  ]
  build() {
    Column(){
      IBestDropdownMenu({ groupId: this.groupId, menuWidthType: "auto" }){
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value!!,
          options: this.options
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1
        })
      }
    }
  }
}

异步打开

异步打开

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  groupId: string = 'dropdown_menu'
  @Local value: number = 1
  @Local options: IBestDropdownMenuOption[] = []
  @Local value1: string = ''
  @Local options1: IBestDropdownMenuOption[] = []
  build() {
    Column(){
      IBestDropdownMenu({ groupId: this.groupId }){
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value!!,
          options: this.options,
          beforeOpen: () => {
            if(this.options.length){
              return true
            }
            return new Promise((resolve, reject) => {
              setTimeout(() => {
                this.options = [
                  { text: '打折商品', value: '1' },
                  { text: '新款商品', value: '2' },
                  { text: '活动商品', value: '3' }
                ]
                resolve(true)
              }, 1500)
            })
          }
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1,
          onOpen: () => {
            setTimeout(() => {
              this.options1 = [
                { text: '默认排序', value: 'a' },
                { text: '好评排序', value: 'b' },
                { text: '销量排序', value: 'c' },
              ]
            }, 1500)
          }
        })
      }
    }
  }
}

禁用菜单

禁用菜单

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  groupId: string = 'dropdown_menu'
  @Local value: number = 1
  @Local options: IBestDropdownMenuOption[] = [
    { text: '全部商品', value: 1 },
    { text: '新款商品', value: 2 },
    { text: '活动商品', value: 3 }
  ]
  @Local value1: string = ''
  @Local options1: IBestDropdownMenuOption[] = [
    { text: '默认排序', value: '' },
    { text: '好评排序', value: 'a' },
    { text: '销量排序', value: 'b' },
  ]
  build() {
    Column(){
      IBestDropdownMenu({ groupId: this.groupId }){
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value!!,
          options: this.options,
          disabled: true
        })
        IBestDropdownItem({
          groupId: this.groupId,
          value: this.value1!!,
          options: this.options1,
          disabled: true
        })
      }
    }
  }
}

API

IBestDropdownMenu @Props

参数说明类型默认值
groupId分组id, 需保证全局唯一性string | number''
menuHeight菜单高度string | number48
bgColor菜单背景颜色ResourceColor#fff
closeOnClickOverlay点击遮罩层是否关闭菜单booleantrue
menuWidthType菜单项宽度类型, 可选值 autofixedstringflex
menuIcon菜单项标题右侧图标Resourcearrow-down
menuShadow菜单项阴影ShadowOptions | ShadowStyle-
menuIconSize菜单项图标大小string | number16
menuFontSize菜单项文字大小string | number16
menuFontColor菜单项文字颜色ResourceColor#323232
activeColor菜单标题和下拉选项的选中态颜色ResourceColor#1989fa
dropDownFontSize菜单项下拉选项文字大小string | number14
radius下拉菜单圆角string | number0
selectedIcon菜单项选中图标ResourceStrsuccess
controller菜单项控制器IBestDropdownMenuController-
dropDownIconSize 1.0.3下拉选项图标大小string | number14

IBestDropdownMenu 插槽

插槽名说明类型
defaultBuilder默认内容的插槽CustomBuilder

IBestDropdownItem @Props

参数说明类型默认值
groupId分组id, 需保证全局唯一性string | number''
title菜单默认标题string''
options菜单选项列表IBestDropdownMenuOption[][]
value当前选中项对应的value,支持双向绑定string | number''
disabled菜单是否禁用booleanfalse
maxHeight 1.0.3最大高度string | number''

IBestDropdownMenuOption 数据结构

参数说明类型
text选项文字ResourceStr
value选项标识符string | number
disabled选项是否禁用boolean
icon选项左侧的图标ResourceStr
iconPosition 1.0.3选项图标位置, 可选值 leftright, 默认'left'string

IBestDropdownItem Events

事件名说明事件类型
onOpen菜单打开时触发() => void
onClose菜单关闭时触发() => void
onChange点击选项时触发(value: string | number) => void
beforeOpen 1.0.3菜单打开前触发,返回false可阻止菜单打开() => Promise<boolean> | boolean

IBestDropdownItem 插槽

插槽名说明类型
defaultBuilder默认内容的插槽CustomBuilder

IBestDropdownMenuController API

方法名说明参数
close关闭所有下拉菜单-

主题定制

组件提供了下列颜色变量,可用于自定义深色/浅色模式样式,使用方法请参考 颜色模式 章节,如需要其它颜色变量可提 issue

名称描述默认值
ibest_dropdown_menu_bg_color菜单背景色#fff
ibest_dropdown_menu_text_color菜单文字颜色#323233
ibest_dropdown_menu_border_color菜单边框颜色#ebedf0
ibest_dropdown_menu_disabled_color菜单禁用时颜色#969799