ImagePreview 图片预览

import {
  TaCell,
  TaGroup,
  TaImagePreview,
  showImagePreview,
  PopupOnVisibleStateChange,
  PopupOnCancel,
  showToast
} from '@/index'
import { useRef, useState } from 'react'

interface showArgs {
  current?: string
  showClose?: boolean
  maskClosable?: boolean
  callbackEvent?: boolean
  visibleEvent?: boolean
}

const imageUrls = [
  'https://cdn.fox2.cn/vfox/swiper/different-1.jpg',
  'https://cdn.fox2.cn/vfox/swiper/different-2.jpg',
  'https://cdn.fox2.cn/vfox/swiper/different-3.jpg'
]

export default function ExpImagePreview() {
  const [current, setCurrent] = useState('')
  const [showClose, setShowClose] = useState(true)
  const [maskClosable, setMaskClosable] = useState(false)
  const [visible, setVisible] = useState(false)

  const callbackEvent = useRef(false)
  const visibleEvent = useRef(false)

  function onShow(args: showArgs = {}) {
    setCurrent(args.current || '')
    setShowClose(!!args.showClose)
    setMaskClosable(!!args.maskClosable)
    setVisible(true)

    callbackEvent.current = !!args.callbackEvent
    visibleEvent.current = !!args.visibleEvent
  }

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

  const onChange = (url: string, index: number) => {
    if (callbackEvent.current) {
      console.log('change', index)
      showToast(`切换到第 ${index + 1}`)
    }
  }

  const onCancel: PopupOnCancel = res => {
    if (callbackEvent.current) {
      console.log('cancel', res)
      showToast(`关闭`)
    }
  }

  function onCallApi() {
    showImagePreview({
      urls: imageUrls,
      showClose: true,
      imageHighRendering: false,
      success: res => {
        console.log('success', res)
      }
    })
  }

  return (
    <>
      <TaGroup title="基础用法">
        <TaCell label="预览图片" isLink onClick={() => onShow({})}></TaCell>
        <TaCell
          label="指定初始图片"
          isLink
          onClick={() =>
            onShow({
              current: 'https://cdn.fox2.cn/vfox/swiper/different-2.jpg'
            })
          }
        ></TaCell>
        <TaCell
          label="展示关闭按钮"
          isLink
          onClick={() => onShow({ showClose: true })}
        ></TaCell>
      </TaGroup>
      <TaGroup title="事件监听">
        <TaCell
          label="change/cancel"
          isLink
          onClick={() => onShow({ showClose: true, callbackEvent: true })}
        ></TaCell>
        <TaCell
          label="onVisibleStateChange"
          isLink
          onClick={() => onShow({ visibleEvent: true })}
        ></TaCell>
      </TaGroup>
      <TaGroup title="API">
        <TaCell
          label="showImagePreview"
          isLink
          onClick={() => onCallApi()}
        ></TaCell>
      </TaGroup>
      <TaImagePreview
        urls={imageUrls}
        value={current}
        visible={visible}
        maskClosable={maskClosable}
        showClose={showClose}
        imageHighRendering={false}
        onCancel={onCancel}
        onVisibleStateChange={onVisibleStateChange}
        onUpdateVisible={v => setVisible(v)}
        onChange={onChange}
      />
    </>
  )
}

Import

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

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

Import Type

组件导出的类型定义:

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

Props

属性类型默认值必填说明
visiblebooleanfalse是否显示
valuestring指定当前显示的图片 url
urlsstring[]图片 url 数组
showClosebooleanfalse是否显示关闭按钮,显示按钮后展示头部栏
navigationButtonsbooleanfalse是否展示上一页/下一页按钮
imageHighRenderingbooleantrue高清渲染,开启后图片按物理分辨率展示

Events

事件描述回调函数参数TypeScript 函数
onChange图片切换后触发( current: string, activeIndex: number, fromIndex: number )ImagePreviewOnChange
onCancel关闭时触发( payload: { source: string } )PopupOnCancel
onVisibleStateChange展示隐藏时触发( payload: { state: VisibleState } )PopupOnVisibleStateChange
onUpdateVisible展示隐藏时触发( visible: boolean 是否显示 )

onChange 的回调参数

参数类型描述
currentstring当前图片的 url
activeIndexnumber当前图片的索引
fromIndexnumber上涨图片的索引

VisibleState 值说明

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

showImagePreview(object)

预览图片。

object

属性类型默认值必填说明
urlsstring[]图片地址数组
valuestring默认显示的图片地址
showClosebooleanfalse是否显示关闭按钮,显示按钮后展示头部栏
navigationButtonsbooleanfalse是否展示上一页/下一页按钮
imageHighRenderingbooleantrue高清渲染,开启后图片按物理分辨率展示
success(payload: { cancel: boolean }) => void接口调用成功(在用户做出选择后,如取消,选择选项)的回调函数
fail(e: Error) => void接口调用失败(如传入错误的参数)的回调函数(不传入 fail 遇错误直接抛出)
complete() => void弹窗关闭或调用失败的回调函数

Usage

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

showImagePreview({
  urls: [
    'https://cdn.fox2.cn/vfox/empty/default@2x.png',
    'https://cdn.fox2.cn/vfox/empty/network@2x.png'
  ],
  value: 'https://cdn.fox2.cn/vfox/empty/network@2x.png'
})