From 700780f02063a4fd39d8ce6bcfaf38d2c99e493a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E8=B4=A7=E7=88=B1=E5=90=83=E7=99=BD=E8=90=9D?= =?UTF-8?q?=E5=8D=9C?= Date: Thu, 6 Jun 2024 22:25:18 +0800 Subject: [PATCH] fix: transform support one is 0 (#434) --- src/Dialog/Content/index.tsx | 2 +- tests/index.spec.tsx | 86 ++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/Dialog/Content/index.tsx b/src/Dialog/Content/index.tsx index ea85a556..af966a6e 100644 --- a/src/Dialog/Content/index.tsx +++ b/src/Dialog/Content/index.tsx @@ -41,7 +41,7 @@ const Content = React.forwardRef((props, ref) => { const elementOffset = offset(dialogRef.current); setTransformOrigin( - (mousePosition?.x && mousePosition?.y) + mousePosition && (mousePosition.x || mousePosition.y) ? `${mousePosition.x - elementOffset.left}px ${mousePosition.y - elementOffset.top}px` : '', ); diff --git a/tests/index.spec.tsx b/tests/index.spec.tsx index cf58155d..26ac8301 100644 --- a/tests/index.spec.tsx +++ b/tests/index.spec.tsx @@ -280,24 +280,48 @@ describe('dialog', () => { }); }); - it('sets transform-origin when property mousePosition is set', () => { - const wrapper = mount( - -

the dialog

-
, - ); + describe('mousePosition', () => { + function prepareModal(mousePosition: { x: number; y: number }) { + const wrapper = mount( + +

the dialog

+
, + ); + + // Trigger position align + act(() => { + wrapper + .find('Content CSSMotion' as any) + .props() + .onAppearPrepare(); + }); + + return wrapper; + } + + it('sets transform-origin when property mousePosition is set', () => { + const wrapper = prepareModal({ x: 100, y: 100 }); + + expect( + (wrapper.find('.rc-dialog').at(0).getDOMNode() as HTMLDivElement).style['transform-origin'], + ).toBeTruthy(); + }); - // Trigger position align - act(() => { - wrapper - .find('Content CSSMotion' as any) - .props() - .onAppearPrepare(); + it('both undefined', () => { + const wrapper = prepareModal({ x: undefined, y: undefined }); + + expect( + (wrapper.find('.rc-dialog').at(0).getDOMNode() as HTMLDivElement).style['transform-origin'], + ).toBeFalsy(); }); - expect( - (wrapper.find('.rc-dialog').at(0).getDOMNode() as HTMLDivElement).style['transform-origin'], - ).toBeTruthy(); + it('one valid', () => { + const wrapper = prepareModal({ x: 10, y: 0 }); + + expect( + (wrapper.find('.rc-dialog').at(0).getDOMNode() as HTMLDivElement).style['transform-origin'], + ).toBeTruthy(); + }); }); it('can get dom element before dialog first show when forceRender is set true ', () => { @@ -644,14 +668,14 @@ describe('dialog', () => { it('support aria-* in closable', () => { const onClose = jest.fn(); const wrapper = mount( - + }} + visible + onClose={onClose} + />, ); jest.runAllTimers(); wrapper.update(); @@ -669,14 +693,14 @@ describe('dialog', () => { it('support disable button in closable', () => { const onClose = jest.fn(); const wrapper = mount( - + }} + visible + onClose={onClose} + />, ); jest.runAllTimers(); wrapper.update(); @@ -697,13 +721,7 @@ describe('dialog', () => { it('should not display closeIcon when closable is false', () => { const onClose = jest.fn(); - const wrapper = mount( - - ); + const wrapper = mount(); jest.runAllTimers(); wrapper.update();