Skip to content

Commit

Permalink
fix: use fromItems boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
vagusX committed May 15, 2024
1 parent d2fabfe commit 124181d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/PreviewGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const Group: React.FC<GroupConsumerProps> = ({
} = typeof preview === 'object' ? preview : ({} as PreviewGroupPreview);

// ========================== Items ===========================
const [mergedItems, register, from] = usePreviewItems(items);
const [mergedItems, register, fromItems] = usePreviewItems(items);

// ========================= Preview ==========================
// >>> Index
Expand All @@ -92,7 +92,7 @@ const Group: React.FC<GroupConsumerProps> = ({

const onPreviewFromImage = React.useCallback<OnGroupPreview>(
(id, imageSrc, mouseX, mouseY) => {
const index = from === 'context'
const index = fromItems
? mergedItems.findIndex(item => item.id === id)
: mergedItems.findIndex(item => typeof item === 'string' ? item === imageSrc : item.data.src === imageSrc);

Expand Down
11 changes: 5 additions & 6 deletions src/hooks/usePreviewItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
ImageElementProps,
InternalItem,
PreviewImageElementProps,
PreviewItemsFrom,
RegisterImage,
} from '../interface';

Expand All @@ -16,7 +15,7 @@ export type Items = Omit<InternalItem, 'canPreview'>[];
*/
export default function usePreviewItems(
items?: GroupConsumerProps['items'],
): [items: Items, registerImage: RegisterImage, from: PreviewItemsFrom] {
): [items: Items, registerImage: RegisterImage, fromItems: boolean] {
// Context collection image data
const [images, setImages] = React.useState<Record<number, PreviewImageElementProps>>({});

Expand Down Expand Up @@ -63,10 +62,10 @@ export default function usePreviewItems(
}, []);
}, [items, images]);

// from
const from = React.useMemo<PreviewItemsFrom>(() => {
return items ? 'items' : 'context';
// fromItems
const fromItems = React.useMemo<PreviewItemsFrom>(() => {
return !!items;
}, [items]);

return [mergedItems, registerImage, from];
return [mergedItems, registerImage, fromItems];
}
2 changes: 0 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ export type InternalItem = PreviewImageElementProps & {
export type RegisterImage = (id: string, data: PreviewImageElementProps) => VoidFunction;

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

export type PreviewItemsFrom = 'items' | 'context';

0 comments on commit 124181d

Please sign in to comment.