diff --git a/docs/demo/previewgroup-items.md b/docs/demo/previewgroup-items.md
new file mode 100644
index 00000000..47dbaae8
--- /dev/null
+++ b/docs/demo/previewgroup-items.md
@@ -0,0 +1,8 @@
+---
+title: previewGroupItems
+nav:
+ title: Demo
+ path: /demo
+---
+
+
diff --git a/docs/examples/controlledWithGroup.tsx b/docs/examples/controlledWithGroup.tsx
index 2a653949..0d205f60 100644
--- a/docs/examples/controlledWithGroup.tsx
+++ b/docs/examples/controlledWithGroup.tsx
@@ -6,6 +6,7 @@ import { defaultIcons } from './common';
export default function Base() {
const [visible, setVisible] = React.useState(false);
+ const [current, setCurrent] = React.useState(1);
return (
@@ -25,7 +26,8 @@ export default function Base() {
onVisibleChange: value => {
setVisible(value);
},
- current: 1,
+ current,
+ onChange: c => setCurrent(c),
}}
>
+
+
+ );
+}
diff --git a/src/Image.tsx b/src/Image.tsx
index 39dc1853..56674f4f 100644
--- a/src/Image.tsx
+++ b/src/Image.tsx
@@ -168,7 +168,7 @@ const ImageInternal: CompoundedComponent = props => {
const onPreview: React.MouseEventHandler = e => {
const { left, top } = getOffset(e.target);
if (groupContext) {
- groupContext.onPreview(imageId, left, top);
+ groupContext.onPreview(imageId, src, left, top);
} else {
setMousePosition({
x: left,
diff --git a/src/PreviewGroup.tsx b/src/PreviewGroup.tsx
index 22fd7ae8..d6f647e9 100644
--- a/src/PreviewGroup.tsx
+++ b/src/PreviewGroup.tsx
@@ -67,7 +67,7 @@ const Group: React.FC = ({
} = typeof preview === 'object' ? preview : ({} as PreviewGroupPreview);
// ========================== Items ===========================
- const [mergedItems, register] = usePreviewItems(items);
+ const [mergedItems, register, fromItems] = usePreviewItems(items);
// ========================= Preview ==========================
// >>> Index
@@ -91,15 +91,19 @@ const Group: React.FC = ({
const [mousePosition, setMousePosition] = useState(null);
const onPreviewFromImage = React.useCallback(
- (id, mouseX, mouseY) => {
- const index = mergedItems.findIndex(item => item.id === id);
+ (id, imageSrc, mouseX, mouseY) => {
+ const index = fromItems
+ ? mergedItems.findIndex(item => item.data.src === imageSrc)
+ : mergedItems.findIndex(item => item.id === id);
+
+ setCurrent(index < 0 ? 0 : index);
setShowPreview(true);
setMousePosition({ x: mouseX, y: mouseY });
- setCurrent(index < 0 ? 0 : index);
+
setKeepOpenIndex(true);
},
- [mergedItems],
+ [mergedItems, fromItems],
);
// Reset current when reopen
diff --git a/src/hooks/usePreviewItems.ts b/src/hooks/usePreviewItems.ts
index ac652589..a335eb78 100644
--- a/src/hooks/usePreviewItems.ts
+++ b/src/hooks/usePreviewItems.ts
@@ -15,7 +15,7 @@ export type Items = Omit[];
*/
export default function usePreviewItems(
items?: GroupConsumerProps['items'],
-): [items: Items, registerImage: RegisterImage] {
+): [items: Items, registerImage: RegisterImage, fromItems: boolean] {
// Context collection image data
const [images, setImages] = React.useState>({});
@@ -36,6 +36,7 @@ export default function usePreviewItems(
// items
const mergedItems = React.useMemo(() => {
+ // use `items` first
if (items) {
return items.map(item => {
if (typeof item === 'string') {
@@ -51,6 +52,7 @@ export default function usePreviewItems(
});
}
+ // use registered images secondly
return Object.keys(images).reduce((total: Items, id) => {
const { canPreview, data } = images[id];
if (canPreview) {
@@ -60,5 +62,5 @@ export default function usePreviewItems(
}, []);
}, [items, images]);
- return [mergedItems, registerImage];
+ return [mergedItems, registerImage, !!items];
}
diff --git a/src/interface.ts b/src/interface.ts
index d5ff43d4..42120a73 100644
--- a/src/interface.ts
+++ b/src/interface.ts
@@ -26,4 +26,4 @@ export type InternalItem = PreviewImageElementProps & {
export type RegisterImage = (id: string, data: PreviewImageElementProps) => VoidFunction;
-export type OnGroupPreview = (id: string, mouseX: number, mouseY: number) => void;
+export type OnGroupPreview = (id: string, imageSrc: string, mouseX: number, mouseY: number) => void;
diff --git a/tests/previewGroup.test.tsx b/tests/previewGroup.test.tsx
index 38226802..883d4c6a 100644
--- a/tests/previewGroup.test.tsx
+++ b/tests/previewGroup.test.tsx
@@ -44,6 +44,28 @@ describe('PreviewGroup', () => {
expect(onChange).toHaveBeenCalledWith(2, 1);
});
+ it('items should works', () => {
+ const { rerender } = render(
+
+
+ ,
+ );
+
+ fireEvent.click(document.querySelector('.first-img'));
+
+ expect(document.querySelector('.rc-image-preview-img')).toHaveAttribute('src', 'src2');
+
+ rerender(
+
+
+ ,
+ );
+
+ fireEvent.click(document.querySelector('.first-img'));
+
+ expect(document.querySelector('.rc-image-preview-img')).toHaveAttribute('src', 'src3');
+ });
+
it('Mount and UnMount', () => {
const { container, unmount } = render(