Skip to content

Commit 84b7973

Browse files
authored
fix: support PreviewGroup#items locating when clicking image (#344)
* fix: support PreviewGroup#items locating when clicking image * fix: use fromItems boolean * fix: types * fix: boolean * fix: update
1 parent 66b57c2 commit 84b7973

File tree

8 files changed

+68
-10
lines changed

8 files changed

+68
-10
lines changed

docs/demo/previewgroup-items.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: previewGroupItems
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/previewgroup-items.tsx"></code>

docs/examples/controlledWithGroup.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { defaultIcons } from './common';
66

77
export default function Base() {
88
const [visible, setVisible] = React.useState(false);
9+
const [current, setCurrent] = React.useState(1);
910
return (
1011
<div>
1112
<div>
@@ -25,7 +26,8 @@ export default function Base() {
2526
onVisibleChange: value => {
2627
setVisible(value);
2728
},
28-
current: 1,
29+
current,
30+
onChange: c => setCurrent(c),
2931
}}
3032
>
3133
<Image
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Image from 'rc-image';
2+
import * as React from 'react';
3+
import '../../assets/index.less';
4+
5+
export default function Base() {
6+
return (
7+
<Image.PreviewGroup
8+
items={[
9+
'https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg',
10+
'https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg',
11+
'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
12+
]}
13+
>
14+
<Image
15+
width={200}
16+
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
17+
/>
18+
</Image.PreviewGroup>
19+
);
20+
}

src/Image.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
168168
const onPreview: React.MouseEventHandler<HTMLDivElement> = e => {
169169
const { left, top } = getOffset(e.target);
170170
if (groupContext) {
171-
groupContext.onPreview(imageId, left, top);
171+
groupContext.onPreview(imageId, src, left, top);
172172
} else {
173173
setMousePosition({
174174
x: left,

src/PreviewGroup.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const Group: React.FC<GroupConsumerProps> = ({
6767
} = typeof preview === 'object' ? preview : ({} as PreviewGroupPreview);
6868

6969
// ========================== Items ===========================
70-
const [mergedItems, register] = usePreviewItems(items);
70+
const [mergedItems, register, fromItems] = usePreviewItems(items);
7171

7272
// ========================= Preview ==========================
7373
// >>> Index
@@ -91,15 +91,19 @@ const Group: React.FC<GroupConsumerProps> = ({
9191
const [mousePosition, setMousePosition] = useState<null | { x: number; y: number }>(null);
9292

9393
const onPreviewFromImage = React.useCallback<OnGroupPreview>(
94-
(id, mouseX, mouseY) => {
95-
const index = mergedItems.findIndex(item => item.id === id);
94+
(id, imageSrc, mouseX, mouseY) => {
95+
const index = fromItems
96+
? mergedItems.findIndex(item => item.data.src === imageSrc)
97+
: mergedItems.findIndex(item => item.id === id);
98+
99+
setCurrent(index < 0 ? 0 : index);
96100

97101
setShowPreview(true);
98102
setMousePosition({ x: mouseX, y: mouseY });
99-
setCurrent(index < 0 ? 0 : index);
103+
100104
setKeepOpenIndex(true);
101105
},
102-
[mergedItems],
106+
[mergedItems, fromItems],
103107
);
104108

105109
// Reset current when reopen

src/hooks/usePreviewItems.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type Items = Omit<InternalItem, 'canPreview'>[];
1515
*/
1616
export default function usePreviewItems(
1717
items?: GroupConsumerProps['items'],
18-
): [items: Items, registerImage: RegisterImage] {
18+
): [items: Items, registerImage: RegisterImage, fromItems: boolean] {
1919
// Context collection image data
2020
const [images, setImages] = React.useState<Record<number, PreviewImageElementProps>>({});
2121

@@ -36,6 +36,7 @@ export default function usePreviewItems(
3636

3737
// items
3838
const mergedItems = React.useMemo<Items>(() => {
39+
// use `items` first
3940
if (items) {
4041
return items.map(item => {
4142
if (typeof item === 'string') {
@@ -51,6 +52,7 @@ export default function usePreviewItems(
5152
});
5253
}
5354

55+
// use registered images secondly
5456
return Object.keys(images).reduce((total: Items, id) => {
5557
const { canPreview, data } = images[id];
5658
if (canPreview) {
@@ -60,5 +62,5 @@ export default function usePreviewItems(
6062
}, []);
6163
}, [items, images]);
6264

63-
return [mergedItems, registerImage];
65+
return [mergedItems, registerImage, !!items];
6466
}

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ export type InternalItem = PreviewImageElementProps & {
2626

2727
export type RegisterImage = (id: string, data: PreviewImageElementProps) => VoidFunction;
2828

29-
export type OnGroupPreview = (id: string, mouseX: number, mouseY: number) => void;
29+
export type OnGroupPreview = (id: string, imageSrc: string, mouseX: number, mouseY: number) => void;

tests/previewGroup.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ describe('PreviewGroup', () => {
4444
expect(onChange).toHaveBeenCalledWith(2, 1);
4545
});
4646

47+
it('items should works', () => {
48+
const { rerender } = render(
49+
<Image.PreviewGroup items={['src1', 'src2', 'src3']}>
50+
<Image src="src2" className="first-img" />
51+
</Image.PreviewGroup>,
52+
);
53+
54+
fireEvent.click(document.querySelector('.first-img'));
55+
56+
expect(document.querySelector('.rc-image-preview-img')).toHaveAttribute('src', 'src2');
57+
58+
rerender(
59+
<Image.PreviewGroup items={['src1', 'src2', 'src3']}>
60+
<Image src="src3" className="first-img" />
61+
</Image.PreviewGroup>,
62+
);
63+
64+
fireEvent.click(document.querySelector('.first-img'));
65+
66+
expect(document.querySelector('.rc-image-preview-img')).toHaveAttribute('src', 'src3');
67+
});
68+
4769
it('Mount and UnMount', () => {
4870
const { container, unmount } = render(
4971
<Image.PreviewGroup>

0 commit comments

Comments
 (0)