Drawer 抽屉

用于在屏幕边缘显示应用导航等内容的面板。

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

interface showArgs {
  title?: string
  placement?: PlacementType
  showClose?: boolean
  visibleEvent?: boolean
  cancelEvent?: boolean
}

export default function ExpDrawer() {
  const [visible, setVisible] = useState(false)
  const [title, setTitle] = useState('')
  const [placement, setPlacement] = useState<PlacementType>()
  const [showClose, setShowClose] = useState(false)

  const visibleEvent = useRef(false)
  const cancelEvent = useRef(false)

  function onShow(args: showArgs) {
    setTitle(args.title ?? '')
    setPlacement(args.placement)
    setShowClose(!!args.showClose)

    visibleEvent.current = !!args.visibleEvent
    cancelEvent.current = !!args.cancelEvent

    setVisible(true)
  }

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

  const onCancel: PopupOnCancel = res => {
    if (cancelEvent.current) {
      console.log('cancel', res)
      showToast('取消')
    }
  }

  return (
    <>
      <TaGroup title="基础用法">
        <TaCell
          label="顶部弹出"
          isLink
          onClick={() => onShow({ title: '顶部弹出', placement: 'top' })}
        ></TaCell>
        <TaCell
          label="底部弹出"
          isLink
          onClick={() => onShow({ title: '底部弹出', placement: 'bottom' })}
        ></TaCell>
        <TaCell
          label="左侧弹出"
          isLink
          onClick={() => onShow({ title: '左侧弹出', placement: 'left' })}
        ></TaCell>
        <TaCell
          label="右侧弹出"
          isLink
          onClick={() => onShow({ title: '右侧弹出', placement: 'right' })}
        ></TaCell>
      </TaGroup>
      <TaGroup title="无标题">
        <TaCell
          label="底部弹出"
          isLink
          onClick={() => onShow({ placement: 'bottom' })}
        ></TaCell>
        <TaCell
          label="右侧弹出"
          isLink
          onClick={() => onShow({ placement: 'right' })}
        ></TaCell>
      </TaGroup>
      <TaGroup title="展示关闭按钮">
        <TaCell
          label="有标题-底部"
          isLink
          onClick={() =>
            onShow({ title: '标题', placement: 'bottom', showClose: true })
          }
        ></TaCell>
        <TaCell
          label="有标题-右侧"
          isLink
          onClick={() =>
            onShow({ title: '标题', placement: 'right', showClose: true })
          }
        ></TaCell>
        <TaCell
          label="无标题"
          isLink
          onClick={() => onShow({ placement: 'bottom', showClose: true })}
        ></TaCell>
      </TaGroup>
      <TaGroup title="事件监听">
        <TaCell
          label="onVisibleStateChange"
          isLink
          onClick={() =>
            onShow({
              title: '标题',
              placement: 'bottom',
              showClose: true,
              visibleEvent: true
            })
          }
        ></TaCell>
        <TaCell
          label="cancel"
          isLink
          onClick={() =>
            onShow({
              title: '标题',
              placement: 'bottom',
              showClose: true,
              cancelEvent: true
            })
          }
        ></TaCell>
      </TaGroup>
      <TaDrawer
        visible={visible}
        title={title}
        placement={placement}
        showClose={showClose}
        onUpdateVisible={v => setVisible(v)}
        onVisibleStateChange={onVisibleStateChange}
        onCancel={onCancel}
      />
    </>
  )
}

Import

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

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

Import Type

组件导出的类型定义:

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

Props

属性类型默认值必填说明
visiblebooleanfalse是否显示
titlestring标题,设置标题后展示头部栏
placementPlacementType'bottom'从哪展开,可选值:'bottom', 'top', 'left', 'right'
showClosebooleanfalse是否显示关闭按钮,显示按钮后展示头部栏

Events

事件描述回调函数参数TypeScript 函数
onVisibleStateChange展示隐藏时触发payload: { state: VisibleState }PopupOnVisibleStateChange
onCancel关闭按钮或者点击蒙层关闭时触发payload: { source: string }PopupOnCancel
onUpdateVisible展示隐藏时触发visible: boolean 是否显示

VisibleState 值说明

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

Slots

children

<TaDrawer title="菜单" placement="right">
  内容
</TaDrawer>