Dropdown 下拉框

import {
  showToast,
  PopupOnVisibleStateChange,
  TaCell,
  TaGroup,
  TaDropdown
} from '@/index'
import { useRef, useState } from 'react'

interface showArgs {
  selector?: string
  visibleEvent?: boolean
}

export default function ExpDropdown() {
  const [visible, setVisible] = useState(false)
  const [selector, setSelector] = useState('')

  const visibleEvent = useRef(false)

  function onShow(args: showArgs = {}) {
    setSelector(args.selector ?? '')

    visibleEvent.current = !!args.visibleEvent

    setVisible(true)
  }

  const onVisibleStateChange: PopupOnVisibleStateChange = res => {
    if (visibleEvent.current) {
      console.log('onVisibleStateChange', res)
      showToast(`${res.state} 事件触发`)
    }

    if (res.state === 'hidden') {
      visibleEvent.current = false
    }
  }

  return (
    <>
      <TaGroup title="基础用法">
        <TaCell
          label="基础"
          isLink
          id="dropdownCell"
          onClick={() => onShow({ selector: '#dropdownCell' })}
        />
      </TaGroup>
      <TaGroup title="事件监听">
        <TaCell
          label="onVisibleStateChange"
          id="dropdownCellEvent"
          onClick={() =>
            onShow({ selector: '#dropdownCellEvent', visibleEvent: true })
          }
        />
      </TaGroup>
      <TaDropdown
        selector={selector}
        visible={visible}
        onUpdateVisible={v => setVisible(v)}
        onVisibleStateChange={onVisibleStateChange}
      ></TaDropdown>
    </>
  )
}

Import

import { TaDropdown } from 'tantalum-ui-mobile-react'

具体的引入方式可以参考引入组件

Import Type

组件导出的类型定义:

import type {
  VisibleState,
  PopupOnVisibleStateChange
} from 'tantalum-ui-mobile-react'

Props

属性类型默认值必填说明
visiblebooleanfalse是否显示
selectorstring | HTMLElement从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到

Events

事件描述回调函数参数TypeScript 函数
onVisibleStateChange展示隐藏时触发payload: { state: VisibleState }PopupOnVisibleStateChange
onUpdateVisible展示隐藏时触发visible: boolean 是否显示

VisibleState 值说明

说明备注
show展示时触发
shown展示且动画结束后触发
hide隐藏时触发可能携带其他参数 cancel, maskClick, closeClick 等
hidden隐藏且动画结束后触发可能携带其他参数 cancel, maskClick, closeClick 等

Slots

<TaDropdown>展开的内容</TaDropdown>