Skip to content

Commit 1cfdaff

Browse files
authored
perf(image): use composition api (#3635)
* perf(image): use composition api * chore: withInstall
1 parent 2c030d7 commit 1cfdaff

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/image/image.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import {
2-
computed, defineComponent, ref, toRefs, watch,
2+
computed, defineComponent, ref, toRefs, watch, onMounted, onUnmounted,
33
} from '@vue/composition-api';
44
import { isFunction, omit } from 'lodash-es';
55
import { ImageErrorIcon, ImageIcon } from 'tdesign-icons-vue';
66
import observe from '../_common/js/utils/observe';
77
import { useConfig } from '../config-provider/useConfig';
8-
import { TdImageProps } from './type';
98
import props from './props';
109
import { renderTNodeJSX } from '../utils/render-tnode';
1110
import Space from '../space';
@@ -15,7 +14,7 @@ export default defineComponent({
1514
name: 'TImage',
1615
components: { Space },
1716
props,
18-
setup(props: TdImageProps, { emit }) {
17+
setup(props, { emit }) {
1918
const { onLoad, onError } = props;
2019

2120
const {
@@ -104,6 +103,17 @@ export default defineComponent({
104103
`${classPrefix.value}-image--position-${props.position}`,
105104
]);
106105

106+
const io = ref(null);
107+
108+
onMounted(() => {
109+
if (!lazy.value || !imageRef.value) return;
110+
io.value = observe(imageRef.value as HTMLElement, null, handleLoadImage as Function, 0);
111+
});
112+
113+
onUnmounted(() => {
114+
imageRef.value && io && (io as IntersectionObserver).unobserve(imageRef.value as Element);
115+
});
116+
107117
return {
108118
imageRef,
109119
imageClasses,
@@ -120,18 +130,11 @@ export default defineComponent({
120130
handleError,
121131
handleLoad,
122132
isLoaded,
133+
io,
123134
rest,
124135
};
125136
},
126-
mounted(this) {
127-
if (!this.lazy || !this.imageRef) return;
128137

129-
const io = observe(this.imageRef as HTMLElement, null, this.handleLoadImage as Function, 0);
130-
this.io = io;
131-
},
132-
destroyed(this) {
133-
this.imageRef && this.io && (this.io as IntersectionObserver).unobserve(this.imageRef as Element);
134-
},
135138
methods: {
136139
renderPlaceholder() {
137140
const placeholder = renderTNodeJSX(this, 'placeholder');
@@ -193,7 +196,6 @@ export default defineComponent({
193196
`${this.classPrefix}-image__wrapper--shape-${this.shape}`,
194197
this.gallery && `${this.classPrefix}-image__wrapper--gallery`,
195198
this.hasMouseEvent && `${this.classPrefix}-image__wrapper--need-hover`,
196-
this.className,
197199
]}
198200
onMouseenter={this.handleToggleOverlay}
199201
onMouseleave={this.handleToggleOverlay}

src/image/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _Image from './image';
2-
import mapProps from '../utils/map-props';
32
import withInstall from '../utils/withInstall';
43
import { TdImageProps } from './type';
54

@@ -8,5 +7,5 @@ import './style';
87
export type ImageProps = TdImageProps;
98
export * from './type';
109

11-
export const Image = withInstall(mapProps(['value'])(_Image));
10+
export const Image = withInstall(_Image);
1211
export default Image;

0 commit comments

Comments
 (0)