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

feat(image-preview): export onLoad and style for image slot #12740

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vant/src/image-preview/ImagePreviewItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ export default defineComponent({
>
{slots.image ? (
<div class={bem('image-wrap')}>
{slots.image({ src: props.src })}
{slots.image({
src: props.src,
onLoad,
style: imageStyle.value,
})}
</div>
) : (
<Image
Expand Down
2 changes: 1 addition & 1 deletion packages/vant/src/image-preview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ imagePreviewRef.value?.swipeTo(1);
| --- | --- | --- |
| index | Custom index | _{ index: index of current image }_ |
| cover | Custom content that covers the image preview | - |
| image | Custom image content | _{ src: current image src }_ |
| image | Custom image content | _{ src: current image src, onLoad: load image, style: current image style }_ |
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can add an example to tell user when should they use the style and onLoad

Copy link
Contributor Author

Choose a reason for hiding this comment

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

get

Copy link
Member

Choose a reason for hiding this comment

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

I will merge this PR first, and feel free to open another PR for the example~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I've been feeling a bit unwell lately. It would be great if anyone is willing to complete this task.

Copy link
Member

Choose a reason for hiding this comment

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

Take care of yourself ❤️

The documentation has been added: #12751


### onClose Parameters

Expand Down
10 changes: 5 additions & 5 deletions packages/vant/src/image-preview/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ imagePreviewRef.value?.swipeTo(1);

通过组件调用 `ImagePreview` 时,支持以下插槽:

| 名称 | 说明 | 参数 |
| ----- | ------------------------------ | --------------------------- |
| index | 自定义页码内容 | _{ index: 当前图片的索引 }_ |
| cover | 自定义覆盖在图片预览上方的内容 | - |
| image | 自定义图片内容 | _{ src: 当前资源地址 }_ |
| 名称 | 说明 | 参数 |
| --- | --- | --- |
| index | 自定义页码内容 | _{ index: 当前图片的索引 }_ |
| cover | 自定义覆盖在图片预览上方的内容 | - |
| image | 自定义图片内容 | _{ src: 当前资源地址, onLoad: 加载图片函数, style: 当前图片样式 }_ |

### onClose 回调参数

Expand Down
17 changes: 17 additions & 0 deletions packages/vant/src/image-preview/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ test('should render image slot correctly 2', async () => {
expect(wrapper.html().includes('video')).toBeTruthy();
});

test('should render image slot correctly 3', async () => {
const wrapper = mount(ImagePreview, {
props: {
show: true,
images,
},
slots: {
image: ({ src, style }) =>
`<img class="test-img" src="${src}" style="${Object.assign({ width: '100px' }, style)}" />`,
},
});

await later();

expect(wrapper.html().includes('width: 100px')).toBeTruthy();
});

test('should emit long-press event after long press', async () => {
const onLongPress = vi.fn();
const wrapper = mount(ImagePreview, {
Expand Down