Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(design): reset gallery selection between preview sessions (#8112)

白熱433e416e871d
diff --git a/packages/design/src/components/gallery/Gallery.tsx b/packages/design/src/components/gallery/Gallery.tsx
index d146818a3e..6a2cf77ca4 100644
--- a/packages/design/src/components/gallery/Gallery.tsx
+++ b/packages/design/src/components/gallery/Gallery.tsx
@@ -44,6 +44,7 @@ export function Gallery(props: IGalleryProps) {
const { className, images, open, onOpenChange } = props;
const [isVisible, setIsVisible] = useState(Boolean(open));
const [activeImageIndex, setActiveImageIndex] = useState(0);
+ const [previousOpen, setPreviousOpen] = useState(open);
const [zoomLevel, setZoomLevel] = useState(1);
const { direction, locale } = useContext(ConfigContext);
@@ -51,6 +52,16 @@ export function Gallery(props: IGalleryProps) {
const closeButtonRef = useRef<HTMLButtonElement>(null);
const previouslyFocusedElementRef = useRef<HTMLElement | null>(null);
+ if (open !== previousOpen) {
+ setPreviousOpen(open);
+ if (open) {
+ setActiveImageIndex(0);
+ }
+ }
+ if (activeImageIndex > 0 && activeImageIndex >= images.length) {
+ setActiveImageIndex(0);
+ }
+
const activeImage = images[activeImageIndex];
const hasPagination = images.length > 1;
const imageLabel = locale?.Accessibility.image
@@ -73,7 +84,9 @@ export function Gallery(props: IGalleryProps) {
}, [open]);
useEffect(() => {
- if (!open) return;
+ if (!open) {
+ return;
+ }
previouslyFocusedElementRef.current = document.activeElement instanceof HTMLElement
? document.activeElement
@@ -82,7 +95,9 @@ export function Gallery(props: IGalleryProps) {
}, [open]);
useEffect(() => {
- if (!open && !isVisible) return;
+ if (!open && !isVisible) {
+ return;
+ }
const handler = (event: KeyboardEvent) => {
if (event.key === 'Escape' && open) {
@@ -133,7 +148,9 @@ export function Gallery(props: IGalleryProps) {
// wheel
useEffect(() => {
- if (!open) return;
+ if (!open) {
+ return;
+ }
const handleWheel = (e: WheelEvent) => {
e.preventDefault();
@@ -147,7 +164,9 @@ export function Gallery(props: IGalleryProps) {
};
}, [open]);
- if (!open && !isVisible) return null;
+ if (!open && !isVisible) {
+ return null;
+ }
function handleToggleZoom(ratio: number | 'reset') {
if (ratio === 'reset') {
diff --git a/packages/design/src/components/gallery/__tests__/Gallery.spec.tsx b/packages/design/src/components/gallery/__tests__/Gallery.spec.tsx
index aa09849a8e..9e4eddd4b1 100644
--- a/packages/design/src/components/gallery/__tests__/Gallery.spec.tsx
+++ b/packages/design/src/components/gallery/__tests__/Gallery.spec.tsx
@@ -16,8 +16,7 @@
import type { ComponentProps, PropsWithChildren } from 'react';
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-
+import { afterEach, describe, expect, it, vi } from 'vitest';
import enUS from '../../../locale/en-US';
import { ConfigProvider } from '../../config-provider/ConfigProvider';
import { Gallery } from '../Gallery';
@@ -52,9 +51,6 @@ afterEach(() => {
});
describe('Gallery', () => {
- beforeEach(() => {
- vi.useRealTimers();
- });
it('does not render when open is false', () => {
renderGallery({ images, open: false });
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
@@ -75,10 +71,9 @@ describe('Gallery', () => {
// The overlay is the first child of the dialog
const dialog = screen.getByRole('dialog');
const overlay = dialog.querySelector('div');
- if (overlay) {
- fireEvent.click(overlay);
- expect(onOpenChange).toHaveBeenCalledWith(false);
- }
+ expect(overlay).not.toBeNull();
+ fireEvent.click(overlay!);
+ expect(onOpenChange).toHaveBeenCalledWith(false);
});
it('calls onOpenChange(false) when pressing ESC', () => {
@@ -130,33 +125,25 @@ describe('Gallery', () => {
expect(trigger).toHaveFocus();
});
- it('keeps the toolbar controls in logical order in RTL', () => {
- render(
- <ConfigProvider locale={enUS.design} direction="rtl" mountContainer={document.body}>
- <Gallery images={images} open />
- </ConfigProvider>
- );
-
- expect(screen.getByRole('dialog').querySelector('footer')).toHaveClass('rtl:univer-flex-row-reverse');
- });
-
it('zoom in/out/reset buttons adjust the image scale', () => {
renderGallery({ images, open: true });
const img = screen.getByRole('img');
const zoomInBtn = screen.getByRole('button', { name: /zoom in/i });
const zoomOutBtn = screen.getByRole('button', { name: /zoom out/i });
const resetBtn = screen.getByRole('button', { name: /reset zoom/i });
-
- expect(img).toHaveStyle('transform: scale(1)');
+ const getScale = () => Number.parseFloat(img.style.transform.match(/scale\(([^)]+)\)/)?.[1] ?? '');
+ const initialScale = getScale();
fireEvent.click(zoomInBtn);
- expect(img).toHaveStyle('transform: scale(1.25)');
+ const zoomedScale = getScale();
+ expect(zoomedScale).toBeGreaterThan(initialScale);
fireEvent.click(zoomOutBtn);
- expect(img).toHaveStyle('transform: scale(1)');
+ expect(getScale()).toBeLessThan(zoomedScale);
+ fireEvent.click(zoomInBtn);
fireEvent.click(resetBtn);
- expect(img).toHaveStyle('transform: scale(1)');
+ expect(getScale()).toBe(initialScale);
});
it('can switch images using the pager', () => {
@@ -168,6 +155,30 @@ describe('Gallery', () => {
expect(img).toHaveAttribute('alt', 'Image 2 of 3');
});
+ it('opens the requested first image in each new preview session', () => {
+ const { rerender } = renderGallery({ images, open: true });
+ fireEvent.click(screen.getByRole('button', { name: 'Next' }));
+ rerender(<Gallery images={images} open={false} />);
+ const reorderedImages = [images[2], images[0], images[1]];
+ rerender(<Gallery images={reorderedImages} open />);
+ expect(screen.getByRole('img')).toHaveAttribute('src', images[2]);
+ expect(screen.getByRole('img')).toHaveAccessibleName('Image 1 of 3');
+
+ fireEvent.click(screen.getByRole('button', { name: 'Next' }));
+ rerender(<Gallery images={reorderedImages} open={false} />);
+ rerender(<Gallery images={[images[0]]} open />);
+ expect(screen.getByRole('img')).toHaveAttribute('src', images[0]);
+ });
+
+ it('keeps a valid preview when images are removed while open', () => {
+ const { rerender } = renderGallery({ images, open: true });
+ fireEvent.click(screen.getByRole('button', { name: 'Next' }));
+ fireEvent.click(screen.getByRole('button', { name: 'Next' }));
+ rerender(<Gallery images={[images[0]]} open />);
+ expect(screen.getByRole('img')).toHaveAttribute('src', images[0]);
+ expect(screen.getByRole('img')).toHaveAccessibleName('Image 1 of 1');
+ });
+
it('does not render pagination for a single image', () => {
renderGallery({ images: [images[0]], open: true });
@@ -178,10 +189,14 @@ describe('Gallery', () => {
it('should zoom with wheel event and keep value in range', () => {
renderGallery({ images, open: true });
const img = screen.getByRole('img');
- const getScale = () => Number.parseFloat((img.style.transform.match(/scale\(([^)]+)\)/)?.[1] ?? '1'));
+ const getScale = () => Number.parseFloat(img.style.transform.match(/scale\(([^)]+)\)/)?.[1] ?? '');
+ const initialScale = getScale();
fireEvent.wheel(window, { deltaY: -300 });
- expect(img.style.transform).toContain('scale(');
+ const zoomedScale = getScale();
+ expect(zoomedScale).toBeGreaterThan(initialScale);
+ fireEvent.wheel(window, { deltaY: 300 });
+ expect(getScale()).toBeLessThan(zoomedScale);
const zoomInBtn = screen.getByRole('button', { name: /zoom in/i });
const zoomOutBtn = screen.getByRole('button', { name: /zoom out/i });
@@ -191,7 +206,7 @@ describe('Gallery', () => {
const atUpperBound = getScale();
fireEvent.click(zoomInBtn);
expect(getScale()).toBe(atUpperBound);
- expect(getScale()).toBeLessThanOrEqual(2);
+ expect(zoomInBtn).toBeDisabled();
for (let i = 0; i < 12; i++) {
fireEvent.click(zoomOutBtn);
@@ -199,18 +214,16 @@ describe('Gallery', () => {
const atLowerBound = getScale();
fireEvent.click(zoomOutBtn);
expect(getScale()).toBe(atLowerBound);
- expect(getScale()).toBeGreaterThanOrEqual(0.5);
+ expect(zoomOutBtn).toBeDisabled();
});
- it('should schedule close animation timer when closing', () => {
- const timeoutSpy = vi.spyOn(globalThis, 'setTimeout');
+ it('keeps the preview during exit and removes it after the animation', () => {
+ vi.useFakeTimers();
const { rerender } = renderGallery({ images, open: true });
- expect(screen.getByRole('dialog')).toBeInTheDocument();
-
+ act(() => vi.runAllTimers());
rerender(<Gallery images={images} open={false} />);
expect(screen.getByRole('dialog')).toBeInTheDocument();
-
- expect(timeoutSpy).toHaveBeenCalled();
- expect(timeoutSpy.mock.calls.some(([, delay]) => delay === 150)).toBe(true);
+ act(() => vi.runAllTimers());
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
});