Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageViewer preview component supports self-defined preview content #6825

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/components/image-viewer/demos/demo1.less
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@

.footerButton {
font-size: var(--adm-font-size-4);
color: #ffffff;
color: #fff;
line-height: 1;
padding: 10px 16px;
background-color: rgba(153, 153, 153, 0.4);
border-radius: 100px;
display: inline-block;
cursor: pointer;
}
.image-render {
width: 100%;
height: 100%;
display: flex;
align-items: center;
}
42 changes: 39 additions & 3 deletions src/components/image-viewer/demos/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useState } from 'react'
import { ImageViewer, Button } from 'antd-mobile'
import { Button, ImageViewer } from 'antd-mobile'
import { DemoBlock } from 'demos'
import { demoImage, demoImages } from './images'
import React, { useState } from 'react'
import styles from './demo1.less'
import { demoImage, demoImages } from './images'

// 单张图片预览
const Single = () => {
Expand Down Expand Up @@ -95,6 +95,38 @@ const ViewWithFooter = () => {
)
}

const ViewImageRender = () => {
const [visible, setVisible] = useState(false)

return (
<>
<Button
onClick={() => {
setVisible(true)
}}
>
显示内容
</Button>
<ImageViewer
visible={visible}
onClose={() => {
setVisible(false)
}}
imageRender={() => (
<div className={styles['image-render']}>
<video
muted
width='100%'
controls
src='https://mdn.alipayobjects.com/huamei_iwk9zp/afts/file/A*uYT7SZwhJnUAAAAAAAAAAAAADgCCAQ'
/>
</div>
)}
/>
</>
)
}

export default () => {
return (
<>
Expand Down Expand Up @@ -134,6 +166,10 @@ export default () => {
<DemoBlock title='自定义底部额外内容'>
<ViewWithFooter />
</DemoBlock>

<DemoBlock title='自定义预览内容'>
<ViewImageRender />
</DemoBlock>
</>
)
}
4 changes: 4 additions & 0 deletions src/components/image-viewer/image-viewer.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
touch-action: none;
user-select: none;
}
&-image-render {
width: 100%;
height: 100%;
}
&-footer {
position: absolute;
width: 100%;
Expand Down
18 changes: 14 additions & 4 deletions src/components/image-viewer/image-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import classNames from 'classnames'
import type { FC, ReactNode } from 'react'
import React, {
forwardRef,
useCallback,
useImperativeHandle,
useRef,
useState,
useCallback,
} from 'react'
import type { FC, ReactNode } from 'react'
import { mergeProps } from '../../utils/with-default-props'
import {
GetContainer,
renderToContainer,
} from '../../utils/render-to-container'
import { mergeProps } from '../../utils/with-default-props'
import Mask from '../mask'
import SafeArea from '../safe-area'
import { Slide } from './slide'
import { Slides, SlidesRef } from './slides'
import classNames from 'classnames'

const classPrefix = `adm-image-viewer`

Expand All @@ -27,6 +27,8 @@
onClose?: () => void
afterClose?: () => void
renderFooter?: (image: string) => ReactNode
imageRender?: () => ReactNode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

接受一下当前 image 的 url ,否则不知道是哪一个~

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

接受一下当前 image 的 url ,否则不知道是哪一个~

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个定义看起来还是没有变哈?


classNames?: {
mask?: string
body?: string
Expand Down Expand Up @@ -64,6 +66,14 @@
maxZoom={props.maxZoom}
/>
)}
{!props.image && (
<div

Check warning on line 70 in src/components/image-viewer/image-viewer.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/image-viewer/image-viewer.tsx#L70

Added line #L70 was not covered by tests
className={`${classPrefix}-image-render`}
onClick={props.onClose}
>
{props.imageRender?.()}
</div>
)}
</div>
{props.image && (
<div className={`${classPrefix}-footer`}>
Expand Down
1 change: 1 addition & 0 deletions src/components/image-viewer/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
| maxZoom | 最大缩放比例 | `number \| 'auto'` | `3` |
| onClose | 关闭时触发 | `() => void` | - |
| renderFooter | 渲染底部额外内容 | `(image: string) => ReactNode` | - |
| imageRender | 自定义渲染内容 | `() => ReactNode` | - |
| visible | 是否显示 | `boolean` | `false` |

## ImageViewer.Multi
Expand Down
Loading