diff --git a/docs/en-US/component/image.md b/docs/en-US/component/image.md index c10e985084..d57d4d3926 100644 --- a/docs/en-US/component/image.md +++ b/docs/en-US/component/image.md @@ -124,6 +124,7 @@ image/image-preview | alt | Native alt | string | - | - | | fit | Indicate how the image should be resized to fit its container, same as [object-fit](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) | string | fill / contain / cover / none / scale-down | - | | hide-on-click-modal | When enabling preview, use this flag to control whether clicking on backdrop can exit preview mode | boolean | true / false | false | +| initial-index | The initial preview image index, less than the length of `url-list` | number | int | 0 | | lazy | Whether to use lazy load | boolean | — | false | | preview-src-list | allow big image preview | Array | — | - | | referrer-policy | Native referrerPolicy | string | - | - | diff --git a/packages/components/image/__tests__/image.spec.ts b/packages/components/image/__tests__/image.spec.ts index 3ef337bc18..a72ad35202 100644 --- a/packages/components/image/__tests__/image.spec.ts +++ b/packages/components/image/__tests__/image.spec.ts @@ -76,6 +76,21 @@ describe('Image.vue', () => { expect(wrapper.find('img').classes()).toContain('el-image__preview') }) + test('preview initial index test', async () => { + const wrapper = mount(Image, { + props: { + src: IMAGE_SUCCESS, + previewSrcList: new Array(3).fill(IMAGE_FAIL), + initialIndex: 1, + }, + }) + await doubleWait() + await wrapper.find('.el-image__inner').trigger('click') + expect( + wrapper.findAll('.el-image-viewer__img')[1].attributes('style') + ).not.toContain('display: none') + }) + test('$attrs', async () => { const alt = 'this ia alt' const wrapper = mount(Image, { diff --git a/packages/components/image/src/image.ts b/packages/components/image/src/image.ts index 574e35b434..f32fcab63b 100644 --- a/packages/components/image/src/image.ts +++ b/packages/components/image/src/image.ts @@ -36,6 +36,10 @@ export const imageProps = buildProps({ type: Number, default: 2000, }, + initialIndex: { + type: Number, + default: 0, + }, } as const) export type ImageProps = ExtractPropTypes diff --git a/packages/components/image/src/image.vue b/packages/components/image/src/image.vue index 7e57f4beb7..8c99a03e39 100644 --- a/packages/components/image/src/image.vue +++ b/packages/components/image/src/image.vue @@ -99,8 +99,8 @@ export default defineComponent({ }) const imageIndex = computed(() => { - const { src, previewSrcList } = props - let previewIndex = 0 + const { src, previewSrcList, initialIndex } = props + let previewIndex = initialIndex const srcIndex = previewSrcList.indexOf(src) if (srcIndex >= 0) { previewIndex = srcIndex