ActionSheet 动作面板
import {
ActionSheetOption,
ActionSheetOnConfirm,
showToast,
showDialog,
PopupOnCancel,
showActionSheet,
PopupOnVisibleStateChange,
TaCell,
TaGroup,
TaActionSheet
} from '@/index'
import { useRef, useState } from 'react'
interface showArgs {
options?: ActionSheetOption[]
title?: string
showCancel?: boolean
cancelText?: string
visibleEvent?: boolean
confirmEvent?: boolean
}
const defaultOptions: ActionSheetOption[] = [
{
name: '选项1'
},
{
name: '选项2'
},
{
name: '选项3'
}
]
export default function ExpActionSheet() {
const [options, setOptions] = useState(defaultOptions)
const [visible, setVisible] = useState(false)
const [title, setTitle] = useState('')
const [showCancel, setShowCancel] = useState(false)
const [cancelText, setCancelText] = useState<string>()
const visibleEvent = useRef(false)
const confirmEvent = useRef(false)
function onShow(args: showArgs = {}) {
setTitle(args.title ?? '')
setShowCancel(!!args.showCancel)
setCancelText(args.cancelText)
setOptions(args.options ?? defaultOptions)
visibleEvent.current = !!args.visibleEvent
confirmEvent.current = !!args.confirmEvent
setVisible(true)
}
const onVisibleStateChange: PopupOnVisibleStateChange = res => {
if (visibleEvent.current) {
console.log('onVisibleStateChange', res)
showToast(`${res.state} 事件触发`)
}
if (res.state === 'hidden') {
visibleEvent.current = false
confirmEvent.current = false
}
}
const onConfirm: ActionSheetOnConfirm = res => {
if (confirmEvent.current) {
console.log('confirm', res)
// showDialog({
// title: '选择了',
// showCancel: false,
// content: `item.name: '${res.item.name}'\nindex: ${res.index}`
// })
}
}
const onCancel: PopupOnCancel = res => {
if (confirmEvent.current) {
console.log('cancel', res)
showToast('取消了')
}
}
function onCallApi() {
showActionSheet({
title: '标题',
options: defaultOptions,
showCancel: true,
success: res => {
console.log('success', res)
const { confirm, detail } = res
if (confirm) {
showDialog({
title: '选择了',
showCancel: false,
content: `item.name: '${detail.item.name}'\nindex: ${detail.index}`
})
} else {
showToast('取消了')
}
}
})
}
return (
<>
<TaGroup title="基础用法">
<TaCell label="默认" isLink onClick={() => onShow()} />
<TaCell
label="展示标题"
isLink
onClick={() => onShow({ title: '标题' })}
/>
<TaCell
label="展示取消按钮"
isLink
onClick={() => onShow({ showCancel: true })}
/>
<TaCell
label="设置取消按钮文案"
isLink
onClick={() =>
onShow({ showCancel: true, cancelText: '自定义取消按钮文案' })
}
/>
</TaGroup>
<TaGroup title="options 扩展">
<TaCell
label="选项描述"
isLink
onClick={() =>
onShow({
options: [
{
name: '选项1',
description: '选项1的描述文案'
},
{
name: '选项2'
},
{
name: '选项3'
}
]
})
}
/>
<TaCell
label="选项高亮"
isLink
onClick={() =>
onShow({
options: [
{
name: '选项1',
highlight: true
},
{
name: '选项2'
},
{
name: '选项3'
}
]
})
}
/>
</TaGroup>
<TaGroup title="事件监听">
<TaCell
label="confirm/cancel"
isLink
onClick={() => onShow({ showCancel: true, confirmEvent: true })}
/>
<TaCell
label="onVisibleStateChange"
isLink
onClick={() => onShow({ visibleEvent: true })}
/>
</TaGroup>
<TaGroup title="API">
<TaCell label="showActionSheet" isLink onClick={() => onCallApi()} />
</TaGroup>
<TaActionSheet
visible={visible}
title={title}
options={options}
showCancel={showCancel}
cancelText={cancelText}
onUpdateVisible={v => setVisible(v)}
onVisibleStateChange={onVisibleStateChange}
onCancel={onCancel}
onConfirm={onConfirm}
/>
</>
)
}
Import
import { TaActionSheet } from 'tantalum-ui-mobile-react'
具体的引入方式可以参考引入组件。
Import Type
组件导出的类型定义:
import type {
ActionSheetOption,
ActionSheetOnConfirm,
VisibleState,
PopupOnVisibleStateChange,
PopupOnCancel
} from 'tantalum-ui-mobile-react'
Props
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
visible | boolean | false | 否 | 是否显示 |
title | string | 否 | 标题,不设置则不展示标题栏 | |
options | ActionSheetOption[] | 是 | 选项列表 | |
maskClosable | boolean | true | 否 | 点击蒙层是否触发关闭操作 |
showCancel | boolean | false | 否 | 是否显示取消按钮 |
cancelText | string | '取消' | 否 | 取消按钮的文本 |
ActionSheetOption 的结构
key | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
name | string | 是 | 展示名称 | |
description | string | 否 | 附加描述 | |
disabled | string | false | 否 | 是否禁用 |
highlight | string | false | 否 | 是否高亮显示 |
interface ActionSheetOption {
name: string
highlight?: boolean
description?: string
disabled?: boolean
}
const options: ActionSheetOption[] = [
{
name: '选项1',
disabled: false,
description: '选项描述',
highlight: false
}
]
Events
事件 | 描述 | 回调函数参数 | 函数 TypeScript |
---|---|---|---|
onConfirm | 点击选项时触发 | payload: { item: { name: string }, index: number } | ActionSheetOnConfirm |
onCancel | 取消按钮或者点击蒙层关闭时触发 | payload: { source: string } | PopupOnCancel |
onVisibleStateChange | 展示隐藏时触发 | payload: { state: VisibleState } | PopupOnVisibleStateChange |
onUpdateVisible | 展示隐藏时触发 | visible: boolean 是否显示 |
VisibleState 值说明
值 | 说明 | 备注 |
---|---|---|
show | 展示时触发 | |
shown | 展示且动画结束后触发 | |
hide | 隐藏时触发 | 可能携带其他参数 cancel, maskClick, closeClick 等 |
hidden | 隐藏且动画结束后触发 | 可能携带其他参数 cancel, maskClick, closeClick 等 |
showActionSheet(object)
显示动作面板。
object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
title | string | 否 | 提示的标题,不设置则不展示 | |
options | ActionSheetOption[] | 是 | 选项列表 | |
showCancel | boolean | false | 否 | 是否显示取消按钮 |
cancelText | string | '取消' | 否 | 取消按钮的文字 |
success | (payload: SuccessPayload) => void | 否 | 接口调用成功(在用户做出选择后,如取消,选择选项)的回调函数 | |
fail | (e: Error) => void | 否 | 接口调用失败(如传入错误的参数)的回调函数(不传入 fail 遇错误直接抛出) | |
complete | () => void | 否 | 弹窗关闭或调用失败的回调函数 |
SuccessPayload
属性 | 类型 | 说明 |
---|---|---|
confirm? | boolean | 为 true 时,表示用户点击了选项,此时返回 detail |
cancel? | boolean | 为 true 时,表示用户点击了取消 |
detail?.item.name | string | confirm=true 时点击 item 的选项名 |
detail?.index | number | confirm=true 时点击 item 的索引 |
Usage
具体调用方式可以参考API 调用。
showActionSheet({
title: '标题',
options: [
{
name: '选项1',
description: '选项1的描述文案'
},
{
name: '选项2'
},
{
name: '选项3'
}
],
showCancel: true,
success: ({ confirm, cancel, detail }) => {
...
}
})