Skip to content

Commit

Permalink
chore: reset when not from img
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Jun 17, 2023
1 parent 9f8468f commit 2e752f6
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/PreviewGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const Group: React.FC<GroupConsumerProps> = ({
value: currentIndex,
});

const [keepOpenIndex, setKeepOpenIndex] = useState(false);

// >>> Image
const { src, ...imgCommonProps } = mergedItems[current]?.data || {};
// >>> Visible
Expand All @@ -88,15 +90,23 @@ const Group: React.FC<GroupConsumerProps> = ({

setShowPreview(true);
setMousePosition({ x: mouseX, y: mouseY });
setCurrent(
// `items` should always open the first one
// We easy replace `-1` to `0` here
index < 0 ? 0 : index,
);
setCurrent(index);
setKeepOpenIndex(index >= 0);
},
[mergedItems],
);

// Reset current when reopen
React.useEffect(() => {
if (isShowPreview) {
if (!keepOpenIndex) {
setCurrent(0);
}
} else {
setKeepOpenIndex(false);
}
}, [isShowPreview]);

// ========================== Events ==========================
const onInternalChange: PreviewGroupPreview['onChange'] = (next, prev) => {
setCurrent(next);
Expand Down

0 comments on commit 2e752f6

Please sign in to comment.