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
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
visible | boolean | false | 否 | 是否显示 |
title | string | 否 | 标题,设置标题后展示头部栏 | |
placement | PlacementType | 'bottom' | 否 | 从哪展开,可选值:'bottom', 'top', 'left', 'right' |
showClose | boolean | false | 否 | 是否显示关闭按钮,显示按钮后展示头部栏 |
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>