PopDialog 气泡对话框

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

interface showArgs {
  selector?: string
  content?: string
  placement?: PlacementType
  showCancel?: boolean
  cancelText?: string
  confirmText?: string
  visibleEvent?: boolean
  confirmEvent?: boolean
}

const defaultContent = '确定要删除该条数据?'

export default function ExpPopDialog() {
  const [selector, setSelector] = useState<string>()
  const [content, setContent] = useState(defaultContent)
  const [placement, setPlacement] = useState<PlacementType>()
  const [showCancel, setShowCancel] = useState(false)
  const [cancelText, setCancelText] = useState<string>()
  const [confirmText, setConfirmText] = useState<string>()

  const [visible, setVisible] = useState(false)
  const [visible2, setVisible2] = useState(false)

  const confirmEvent = useRef(false)
  const visibleEvent = useRef(false)

  function onShow(args: showArgs = {}) {
    setSelector(args.selector)
    setPlacement(args.placement)
    setContent(args.content || defaultContent)
    setShowCancel(args.showCancel !== false)
    setCancelText(args.cancelText)
    setConfirmText(args.confirmText)

    visibleEvent.current = !!args.visibleEvent
    confirmEvent.current = !!args.confirmEvent

    visibleEvent.current = !!args.visibleEvent

    setVisible(true)
  }

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

  const onConfirm = () => {
    if (confirmEvent.current) {
      console.log('confirm')
      showToast('点击确定按钮')
    }
  }

  const onCancel: PopupOnCancel = res => {
    if (confirmEvent.current) {
      console.log('cancel', res)
      showToast('点击取消按钮')
    }
  }

  function onCallApi(selector: string) {
    showPopDialog({
      selector,
      placement: 'top',
      content: defaultContent,
      success: res => {
        console.log('success', res)
        showToast(res.confirm ? `点击了确定` : `点击了取消`)
      }
    })
  }

  return (
    <>
      <TaGroup title="基础用法">
        <TaCell label="基础">
          <TaButton
            size="small"
            id="popDialog"
            shape="circle"
            icon="DeleteOutlined"
            onClick={() => onShow({ selector: '#popDialog' })}
          ></TaButton>
        </TaCell>
        <TaCell label="不展示取消按钮">
          <TaButton
            size="small"
            id="popDialogNoCancel"
            shape="circle"
            icon="DeleteOutlined"
            onClick={() =>
              onShow({ selector: '#popDialogNoCancel', showCancel: false })
            }
          ></TaButton>
        </TaCell>
        <TaCell label="设置按钮文案">
          <TaButton
            size="small"
            id="popDialogButtonText"
            shape="circle"
            icon="PlusOutlined"
            onClick={() =>
              onShow({
                selector: '#popDialogNoCancel',
                content: '这有一份关爱保险待你查收',
                confirmText: '接受',
                cancelText: '拒绝'
              })
            }
          ></TaButton>
        </TaCell>
        <TaCell label="不展示蒙层">
          <TaButton
            size="small"
            id="popDialogNoMask"
            shape="circle"
            icon="DeleteOutlined"
            onClick={() => setVisible2(true)}
          ></TaButton>
        </TaCell>
      </TaGroup>
      <TaGroup title="方向 placement=top/bottom/left/right">
        <div className="exp-popover-box2">
          <div>
            <TaButton
              size="small"
              id="popDialogTop2"
              shape="circle"
              icon="UpOutlined"
              onClick={() =>
                onShow({ placement: 'top', selector: '#popDialogTop2' })
              }
            ></TaButton>
          </div>
          <div>
            <TaButton
              size="small"
              id="popDialogLeft2"
              shape="circle"
              icon="LeftOutlined"
              onClick={() =>
                onShow({ placement: 'left', selector: '#popDialogLeft2' })
              }
            ></TaButton>
            <TaButton
              className="exp-popover-box2-ml"
              size="small"
              id="popDialogRight2"
              shape="circle"
              icon="RightOutlined"
              onClick={() =>
                onShow({ placement: 'right', selector: '#popDialogRight2' })
              }
            ></TaButton>
          </div>
          <div>
            <TaButton
              size="small"
              id="popDialogBottom2"
              shape="circle"
              icon="DownOutlined"
              onClick={() =>
                onShow({ placement: 'bottom', selector: '#popDialogBottom2' })
              }
            ></TaButton>
          </div>
        </div>
      </TaGroup>
      <TaGroup title="事件监听">
        <TaCell label="confirm/cancel">
          <TaButton
            size="small"
            id="popDialogEvent"
            shape="circle"
            icon="DeleteOutlined"
            onClick={() =>
              onShow({ confirmEvent: true, selector: '#popDialogEvent' })
            }
          ></TaButton>
        </TaCell>
        <TaCell label="onVisibleStateChange">
          <TaButton
            size="small"
            id="popDialogEvent2"
            shape="circle"
            icon="DeleteOutlined"
            onClick={() =>
              onShow({ visibleEvent: true, selector: '#popDialogEvent2' })
            }
          ></TaButton>
        </TaCell>
      </TaGroup>
      <TaGroup title="API">
        <TaCell label="showPopDialog">
          <TaButton
            size="small"
            id="popDialogApi"
            shape="circle"
            icon="DeleteOutlined"
            onClick={() => onCallApi('#popDialogApi')}
          ></TaButton>
        </TaCell>
      </TaGroup>
      <TaPopDialog
        placement={placement}
        content={content}
        selector={selector}
        visible={visible}
        showCancel={showCancel}
        confirmText={confirmText}
        cancelText={cancelText}
        onUpdateVisible={v => setVisible(v)}
        onVisibleStateChange={onVisibleStateChange}
        onCancel={onCancel}
        onConfirm={onConfirm}
      />
      <TaPopDialog
        visible={visible2}
        selector="#popDialogNoMask"
        showMask={false}
        content="无蒙层气泡内容"
        onUpdateVisible={v => setVisible2(v)}
      />
    </>
  )
}

Import

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

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

Import Type

组件导出的类型定义:

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

Props

属性类型默认值必填说明
visiblebooleanfalse是否显示
selectorstring | HTMLElement从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到
placementPlacementType'bottom'展开位置,可选 'bottom', 'top', 'left', 'right'
showMaskbooleantrue是否展示蒙层,如果设置不展示,气泡则是跟随 selector 对应的元素
contentstring文本内容
showCancelbooleantrue是否显示取消按钮
cancelTextstring'取消'取消按钮的文本
confirmTextstring'确定'确定按钮的文本

Events

事件描述回调函数参数TypeScript 函数
onConfirm确认按钮点击时触发
onCancel取消按钮点击时触发payload: { source: string }PopupOnCancel
onVisibleStateChange展示隐藏时触发payload: { state: VisibleState }PopupOnVisibleStateChange
onUpdateVisible展示隐藏时触发visible: boolean 是否显示

VisibleState 值说明

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

showPopDialog(object)

显示气泡对话框。

object

属性类型默认值必填说明
selectorstring | HTMLElement从哪个元素展开,如果是 string,则为可以被 document.querySelector(selector) 获取到
placementPlacementType'bottom'展开位置,可选 'bottom', 'top', 'left', 'right'
contentstring文本内容
showCancelbooleantrue是否显示取消按钮
cancelTextstring'取消'取消按钮的文本
confirmTextstring'确定'确定按钮的文本
success(payload: SuccessPayload) => void接口调用成功(在用户做出选择后,如取消,选择选项)的回调函数
fail(e: Error) => void接口调用失败(如传入错误的参数)的回调函数(不传入 fail 遇错误直接抛出)
complete() => void弹窗关闭或调用失败的回调函数

SuccessPayload

属性类型说明
cancel?boolean为 true 时,表示取消
confirm?boolean为 true 时,表示点击了确定

Usage

具体调用方式可以参考API 调用

showPopDialog({
  selector: '#popDialogTarget',
  content: this.content,
  success: res => {
    console.log('success', res)
  }
})