Notify 消息提示
import {
TaCell,
TaGroup,
TaNotify,
PopupOnVisibleStateChange,
PopupOnCancel,
showNotify,
hideNotify,
StateType
} from '@/index'
import { useState } from 'react'
interface showArgs {
icon?: string
title?: string
color?: string
type?: StateType
duration?: number
closable?: boolean
}
export default function ExpNotify() {
const [icon, setIcon] = useState<string>()
const [title, setTitle] = useState('')
const [color, setColor] = useState<string>()
const [type, setType] = useState<StateType>()
const [duration, setDuration] = useState(0)
const [closable, setClosable] = useState(false)
const [visible, setVisible] = useState(false)
function onShow(args: showArgs) {
setIcon(args.icon)
setTitle(args.title ?? '')
setColor(args.color)
setType(args.type)
setDuration(args.duration ?? 1500)
setClosable(!!args.closable)
setVisible(true)
}
const onVisibleStateChange: PopupOnVisibleStateChange = res => {
console.log('onVisibleStateChange', res)
}
const onCancel: PopupOnCancel = res => {
console.log('onCancel', res)
}
return (
<>
<TaGroup title="基础用法">
<TaCell
label="主要"
isLink
onClick={() => onShow({ title: '通知文本' })}
/>
<TaCell
label="成功"
isLink
onClick={() => onShow({ title: '成功文本', type: 'success' })}
/>
<TaCell
label="警告"
isLink
onClick={() => onShow({ title: '警告文本', type: 'warning' })}
/>
<TaCell
label="危险"
isLink
onClick={() => onShow({ title: '危险文本', type: 'danger' })}
/>
</TaGroup>
<TaGroup title="自定义图标">
<TaCell
label="成功"
isLink
onClick={() =>
onShow({
title: '成功文本',
type: 'success',
icon: 'CheckCircleOutlined'
})
}
/>
<TaCell
label="警告"
isLink
onClick={() =>
onShow({
title: '警告文本',
type: 'warning',
icon: 'ExclamationCircleOutlined'
})
}
/>
<TaCell
label="危险"
isLink
onClick={() =>
onShow({
title: '危险文本',
type: 'danger',
icon: 'CloseCircleOutlined'
})
}
/>
</TaGroup>
<TaGroup title="其他">
<TaCell
label="自定义时长"
isLink
onClick={() => onShow({ title: '5秒后消失', duration: 5000 })}
/>
<TaCell
label="自定义颜色"
isLink
onClick={() =>
onShow({
title: '深色调',
icon: 'InfoCircleOutlined',
color: '#ff4d4f'
})
}
/>
<TaCell
label="手动关闭"
isLink
onClick={() =>
onShow({ title: '常驻可手动关闭', duration: 0, closable: true })
}
/>
</TaGroup>
<TaGroup title="API">
<TaCell
label="showNotify"
isLink
onClick={() =>
showNotify({
title: '通知文本',
duration: 5000,
closable: true
})
}
/>
<TaCell label="hideNotify" isLink onClick={() => hideNotify()} />
</TaGroup>
<TaNotify
visible={visible}
title={title}
type={type}
color={color}
icon={icon}
duration={duration}
closable={closable}
onVisibleStateChange={onVisibleStateChange}
onCancel={onCancel}
onUpdateVisible={v => setVisible(v)}
/>
</>
)
}
Import
import { TaNotify } from 'tantalum-ui-mobile-react'
具体的引入方式可以参考引入组件。
Import Type
组件导出的类型定义:
import type {
VisibleState,
PopupOnVisibleStateChange,
StateType
} from 'tantalum-ui-mobile-react'
Props
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
visible | boolean | false | 否 | 是否显示 |
title | string | 否 | 提示内容 | |
closable | boolean | false | 否 | 是否显示关闭按钮 |
icon | string | 否 | 图标,使用 Icon 组件,图标优先级高于 type | |
type | StateType | 'default' | 否 | 提示类型 |
duration | number | 0 | 否 | visible=true 展示后,duration 毫秒后消失,0 为不消失,在 visible 下生效 |
color | string | 否 | 自定义色彩,支持 hex rgb hsl 等写法,详细效果查看 |
StateType 的合法值
值 | 说明 |
---|---|
default | 警告,同 warning |
primary | 提示 |
success | 成功 |
warning | 警告 |
danger | 强烈警告 |
Events
事件 | 描述 | 回调函数参数 | TypeScript 函数 |
---|---|---|---|
onVisibleStateChange | 展示隐藏时触发 | payload: { state: VisibleState } | PopupOnVisibleStateChange |
onUpdateVisible | 展示隐藏时触发 | visible: boolean 是否显示 |
VisibleState 值说明
值 | 说明 | 备注 |
---|---|---|
show | 展示时触发 | |
shown | 展示且动画结束后触发 | |
hide | 隐藏时触发 | 可能携带其他参数 cancel, maskClick, closeClick 等 |
hidden | 隐藏且动画结束后触发 | 可能携带其他参数 cancel, maskClick, closeClick 等 |
showNotify(object)
显示消息提示。
注:Notify 接口目前仅支持单例模式。
object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
title | string | 否 | 提示内容 | |
type | StateType | 'default' | 否 | 提示类型 |
icon | string | 否 | 图标,使用 Icon 组件,图标优先级高于 type | |
duration | number | 1500 | 否 | 展示时长(单位 ms),值为 0 时,notify 不会消失 |
color | string | 否 | 自定义色彩,支持 hex rgb hsl 等写法,详细效果查看 | |
closable | boolean | false | 否 | 是否显示关闭按钮 |
success | () => void | 否 | 接口调用成功的回调函数 | |
fail | (e: Error) => void | 否 | 接口调用失败的回调函数(不传入 fail 遇错误直接抛出) | |
complete | () => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Usage
具体调用方式可以参考API 调用。
showNotify({
title: '提示内容',
duration: 2000
})
hideNotify([object])
隐藏消息提示框。
object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
success | () => void | 否 | 接口调用成功的回调函数 | |
fail | (e: Error) => void | 否 | 接口调用失败的回调函数(不传入 fail 遇错误直接抛出) | |
complete | () => void | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
Usage
具体调用方式可以参考API 调用。
hideNotify()