Skip to content

Commit bc3843b

Browse files
authored
feat(Image): add previewMask prop (#5531)
* fix(Image): can not cancel previewMask * remove redundant code * remove redundant code * update code
1 parent 9b6e742 commit bc3843b

File tree

5 files changed

+27
-39
lines changed

5 files changed

+27
-39
lines changed

components/image/index.en-US.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Previewable image.
2222
| placeholder | Load placeholder, use default placeholder when set `true` | boolean \| slot | - | 2.0.0 |
2323
| preview | preview config, disabled when `false` | boolean \| [previewType](#previewType) | true | 2.0.0 |
2424
| src | Image path | string | - | 2.0.0 |
25-
| previewMask | custom mask | slot | - | 3.2.0 |
25+
| previewMask | custom mask | boolean \| function \| slot | - | 3.2.0 |
2626
| width | Image width | string \| number | - | 2.0.0 |
2727

2828
### events

components/image/index.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ const Image = defineComponent<ImageProps>({
4949
v-slots={{
5050
...slots,
5151
previewMask:
52-
slots.previewMask ??
53-
(() => (
54-
<div class={`${prefixCls.value}-mask-info`}>
55-
<EyeOutlined />
56-
{imageLocale?.preview}
57-
</div>
58-
)),
52+
props.previewMask === false
53+
? null
54+
: props.previewMask ??
55+
slots.previewMask ??
56+
(() => (
57+
<div class={`${prefixCls.value}-mask-info`}>
58+
<EyeOutlined />
59+
{imageLocale?.preview}
60+
</div>
61+
)),
5962
}}
6063
></ImageInternal>
6164
);

components/image/index.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cover: https://gw.alipayobjects.com/zos/antfincdn/D1dXz9PZqa/image.svg
2323
| placeholder | 加载占位, 为 `true` 时使用默认占位 | boolean \| slot | - | 2.0.0 |
2424
| preview | 预览参数,为 `false` 时禁用 | boolean \| [previewType](#previewType) | true | 2.0.0 |
2525
| src | 图片地址 | string | - | 2.0.0 |
26-
| previewMask | 自定义 mask | slot | - | 3.2.0 |
26+
| previewMask | 自定义 mask | boolean \| function \| slot | - | 3.2.0 |
2727
| width | 图像宽度 | string \| number | - | 2.0.0 |
2828

2929
### 事件

components/vc-image/src/Image.tsx

+5-16
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,17 @@ export type ImagePreviewType = Omit<
2525
icons?: PreviewProps['icons'];
2626
};
2727

28-
export interface ImagePropsType extends Omit<ImgHTMLAttributes, 'placeholder' | 'onClick'> {
29-
// Original
30-
src?: string;
31-
wrapperClassName?: string;
32-
wrapperStyle?: CSSProperties;
33-
prefixCls?: string;
34-
previewPrefixCls?: string;
35-
placeholder?: boolean;
36-
fallback?: string;
37-
preview?: boolean | ImagePreviewType;
38-
onClick?: MouseEventHandler;
39-
onError?: HTMLImageElement['onerror'];
40-
}
4128
export const imageProps = () => ({
4229
src: String,
4330
wrapperClassName: String,
4431
wrapperStyle: { type: Object as PropType<CSSProperties>, default: undefined as CSSProperties },
4532
rootClassName: String,
4633
prefixCls: String,
4734
previewPrefixCls: String,
35+
previewMask: {
36+
type: [Boolean, Function] as PropType<boolean | (() => any)>,
37+
default: undefined,
38+
},
4839
placeholder: PropTypes.any,
4940
fallback: String,
5041
preview: {
@@ -103,9 +94,7 @@ const ImageInternal = defineComponent({
10394
value: previewVisible,
10495
onChange: onPreviewVisibleChange,
10596
});
106-
watch(previewVisible, val => {
107-
setShowPreview(Boolean(val));
108-
});
97+
10998
watch(isShowPreview, (val, preVal) => {
11099
onPreviewVisibleChange(val, preVal);
111100
});

components/vc-image/src/PreviewGroup.tsx

+10-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ref, provide, defineComponent, inject, watch, reactive, computed, watch
33
import { type ImagePreviewType, mergeDefaultValue } from './Image';
44
import Preview from './Preview';
55
import type { PreviewProps } from './Preview';
6+
import useMergedState from '../../_util/hooks/useMergedState';
67

78
export interface PreviewGroupPreview
89
extends Omit<ImagePreviewType, 'icons' | 'mask' | 'maskClassName'> {
@@ -85,10 +86,14 @@ const Group = defineComponent({
8586
const current = ref<number>();
8687

8788
const previewVisible = computed(() => preview.value.visible);
88-
const onPreviewVisibleChange = computed(() => preview.value.onVisibleChange);
8989
const getPreviewContainer = computed(() => preview.value.getContainer);
90-
91-
const isShowPreview = ref(!!previewVisible.value);
90+
const onPreviewVisibleChange = (val, preval) => {
91+
preview.value.onVisibleChange?.(val, preval);
92+
};
93+
const [isShowPreview, setShowPreview] = useMergedState(!!previewVisible.value, {
94+
value: previewVisible,
95+
onChange: onPreviewVisibleChange,
96+
});
9297

9398
const mousePosition = ref<{ x: number; y: number }>(null);
9499
const isControlled = computed(() => previewVisible.value !== undefined);
@@ -115,9 +120,6 @@ const Group = defineComponent({
115120
const setMousePosition = (val: null | { x: number; y: number }) => {
116121
mousePosition.value = val;
117122
};
118-
const setShowPreview = (val: boolean) => {
119-
isShowPreview.value = val;
120-
};
121123

122124
const registerImage = (id: number, url: string, canPreview = true) => {
123125
const unRegister = () => {
@@ -132,16 +134,10 @@ const Group = defineComponent({
132134

133135
const onPreviewClose = (e: any) => {
134136
e?.stopPropagation();
135-
isShowPreview.value = false;
136-
mousePosition.value = null;
137+
setShowPreview(false);
138+
setMousePosition(null);
137139
};
138140

139-
watch(previewVisible, () => {
140-
isShowPreview.value = !!previewVisible.value;
141-
});
142-
watch(isShowPreview, (val, preVal) => {
143-
onPreviewVisibleChange.value(val, preVal);
144-
});
145141
watch(
146142
currentControlledKey,
147143
val => {

0 commit comments

Comments
 (0)