diff --git a/CHANGELOG.en-US.md b/CHANGELOG.en-US.md index 4a312530206..1a3dc1a1909 100644 --- a/CHANGELOG.en-US.md +++ b/CHANGELOG.en-US.md @@ -5,6 +5,7 @@ ### Feats - `n-loading-bar` export `LoadingBarApi` type +- `n-image` add `imgProps` prop ## 2.15.2 (2021-07-02) diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md index 0f1150d98dc..dc4e496f60b 100644 --- a/CHANGELOG.zh-CN.md +++ b/CHANGELOG.zh-CN.md @@ -5,6 +5,7 @@ ### Feats - `n-loading-bar` 导出 `LoadingBarApi` 类型 +- `n-image` 增加 `imgProps` 属性 ## 2.15.2 (2021-07-02) diff --git a/src/image/src/Image.tsx b/src/image/src/Image.tsx index 56e9d34dcdf..9927d853a3a 100644 --- a/src/image/src/Image.tsx +++ b/src/image/src/Image.tsx @@ -1,14 +1,27 @@ -import { defineComponent, h, inject, ref, PropType } from 'vue' +import { defineComponent, h, inject, ref, PropType, toRef } from 'vue' import NImagePreview from './ImagePreview' import type { ImagePreviewInst } from './ImagePreview' import { imageGroupInjectionKey } from './ImageGroup' import { ExtractPublicPropTypes } from '../../_utils' import { useConfig } from '../../_mixins' +interface imgProps { + alt?: string + crossorigin?: 'anonymous' | 'use-credentials' | '' + decoding?: 'async' | 'auto' | 'sync' + height?: number + sizes?: string + src?: string + srcset?: string + usemap?: string + width?: number +} + const imageProps = { alt: String, - width: [String, Number] as PropType, height: [String, Number] as PropType, + imgProps: Object as PropType, + width: [String, Number] as PropType, src: String, showToolbar: { type: Boolean, default: true } } @@ -20,6 +33,7 @@ export default defineComponent({ props: imageProps, setup (props) { const imageRef = ref(null) + const imgPropsRef = toRef(props, 'imgProps') const previewInstRef = ref(null) const imageGroupHandle = inject(imageGroupInjectionKey, null) const { mergedClsPrefixRef } = imageGroupHandle || useConfig(props) @@ -28,6 +42,7 @@ export default defineComponent({ groupId: imageGroupHandle?.groupId, previewInstRef, imageRef, + imgProps: imgPropsRef, handleClick: () => { if (imageGroupHandle) { imageGroupHandle.setPreviewSrc(props.src) @@ -44,19 +59,25 @@ export default defineComponent({ } }, render () { - const { mergedClsPrefix } = this + const { mergedClsPrefix, imgProps = {} } = this + + const imgNode = ( + {this.alt + ) + return this.groupId ? (
- {this.alt} + {imgNode}
) : ( { return (
- {this.alt} + {imgNode}
) } diff --git a/src/image/tests/Image.spec.tsx b/src/image/tests/Image.spec.tsx new file mode 100644 index 00000000000..9173f109c35 --- /dev/null +++ b/src/image/tests/Image.spec.tsx @@ -0,0 +1,67 @@ +import { mount } from '@vue/test-utils' +import { NImage } from '../index' + +describe('n-image', () => { + it('should work with import on demand', () => { + mount(NImage) + }) + + it('should work with `alt` prop', async () => { + const wrapper = mount(NImage) + + await wrapper.setProps({ alt: 'This is just a picture' }) + + expect(wrapper.find('img').attributes('alt')).toBe('This is just a picture') + expect(wrapper.find('img').attributes('aria-label')).toBe( + 'This is just a picture' + ) + }) + + it('should work with `width` prop', async () => { + const wrapper = mount(NImage) + + await wrapper.setProps({ width: '200' }) + + expect(wrapper.find('img').attributes('width')).toBe('200') + + await wrapper.setProps({ width: 200 }) + + expect(wrapper.find('img').attributes('width')).toBe('200') + }) + + it('should work with `height` prop', async () => { + const wrapper = mount(NImage) + + await wrapper.setProps({ height: '300' }) + + expect(wrapper.find('img').attributes('height')).toBe('300') + + await wrapper.setProps({ height: 300 }) + + expect(wrapper.find('img').attributes('height')).toBe('300') + }) + + it('should work with `src` prop', async () => { + const wrapper = mount(NImage) + + await wrapper.setProps({ + src: 'https://www.naiveui.com/assets/naivelogo.93278402.svg' + }) + + expect(wrapper.find('img').attributes('src')).toBe( + 'https://www.naiveui.com/assets/naivelogo.93278402.svg' + ) + }) + + it('should work with `showToolbar` prop', async () => { + const wrapper = mount(NImage) + + await wrapper.setProps({ + showToolbar: true + }) + + await wrapper.find('img').trigger('click') + + expect(document.querySelector('.n-image-preview-toolbar')).not.toEqual(null) + }) +})