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

属性类型默认值必填说明
visiblebooleanfalse是否显示
titlestring提示内容
closablebooleanfalse是否显示关闭按钮
iconstring图标,使用 Icon 组件,图标优先级高于 type
typeStateType'default'提示类型
durationnumber0visible=true 展示后,duration 毫秒后消失,0 为不消失,在 visible 下生效
colorstring自定义色彩,支持 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

属性类型默认值必填说明
titlestring提示内容
typeStateType'default'提示类型
iconstring图标,使用 Icon 组件,图标优先级高于 type
durationnumber1500展示时长(单位 ms),值为 0 时,notify 不会消失
colorstring自定义色彩,支持 hex rgb hsl 等写法,详细效果查看
closablebooleanfalse是否显示关闭按钮
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()