Skip to content

Commit 9d55c8c

Browse files
authored
feat: should trigger align when popupAlign had updated (#329)
* feat: should trigger align when popupAlign had updated * feat: optimize code
1 parent 422d698 commit 9d55c8c

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

src/hooks/useAlign.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export default function useAlign(
204204

205205
// Placement
206206
const placementInfo: AlignType =
207-
builtinPlacements[placement] || popupAlign || {};
207+
popupAlign || builtinPlacements[placement] || {};
208208

209209
// Offset
210210
const { offset, targetOffset } = placementInfo;

src/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ export function generateTrigger(
358358
triggerAlign();
359359
}, [mousePos]);
360360

361+
useLayoutEffect(() => {
362+
if(!mergedOpen) return;
363+
triggerAlign();
364+
}, [JSON.stringify(popupAlign)]);
365+
361366
const alignedClassName = React.useMemo(() => {
362367
const baseClassName = getAlignPopupClassName(
363368
builtinPlacements,

tests/align.test.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
33
import React from 'react';
44
import type { TriggerProps } from '../src';
55
import Trigger from '../src';
6+
import { awaitFakeTimer } from "./util";
67

78
import { _rs } from 'rc-resize-observer';
89

@@ -33,15 +34,6 @@ describe('Trigger.Align', () => {
3334
jest.useRealTimers();
3435
});
3536

36-
async function awaitFakeTimer() {
37-
for (let i = 0; i < 10; i += 1) {
38-
await act(async () => {
39-
jest.advanceTimersByTime(100);
40-
await Promise.resolve();
41-
});
42-
}
43-
}
44-
4537
it('not show', async () => {
4638
const onAlign = jest.fn();
4739

tests/basic.test.jsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
/* eslint-disable max-classes-per-file */
22

3-
import { act, cleanup, fireEvent, render } from '@testing-library/react';
3+
import {
4+
act,
5+
cleanup,
6+
fireEvent,
7+
render,
8+
} from '@testing-library/react';
49
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
510
import React, { createRef, StrictMode } from 'react';
611
import ReactDOM from 'react-dom';
712
import Trigger from '../src';
8-
import { placementAlignMap } from './util';
13+
import { placementAlignMap, awaitFakeTimer } from './util';
914

1015
describe('Trigger.Basic', () => {
1116
beforeEach(() => {
@@ -842,4 +847,48 @@ describe('Trigger.Basic', () => {
842847
expect(errorSpy).not.toHaveBeenCalled();
843848
errorSpy.mockRestore();
844849
});
850+
it('should trigger align when popupAlign had updated', async () => {
851+
const onPopupAlign = jest.fn();
852+
const App = () => {
853+
const [placementAlign, setPlacementAlign] = React.useState(
854+
placementAlignMap.leftTop,
855+
);
856+
const [open, setOpen] = React.useState(true);
857+
return (
858+
<>
859+
<Trigger
860+
popupVisible={open}
861+
popupAlign={placementAlign}
862+
onPopupAlign={onPopupAlign}
863+
popup={<strong className="x-content">tooltip2</strong>}
864+
>
865+
<div>
866+
<div
867+
id="btn"
868+
onClick={() => {
869+
setPlacementAlign(prev => prev === placementAlignMap.left ? placementAlignMap.leftTop: placementAlignMap.left);
870+
}}
871+
>
872+
click
873+
</div>
874+
<div id="close" onClick={() => {
875+
setOpen(false);
876+
}}>close</div>
877+
</div>
878+
</Trigger>
879+
</>
880+
);
881+
};
882+
render(<App />);
883+
await awaitFakeTimer();
884+
expect(onPopupAlign).toHaveBeenCalledTimes(1);
885+
fireEvent.click(document.querySelector('#btn'));
886+
await awaitFakeTimer();
887+
expect(onPopupAlign).toHaveBeenCalledTimes(2);
888+
fireEvent.click(document.querySelector('#close'));
889+
await awaitFakeTimer();
890+
fireEvent.click(document.querySelector('#btn'));
891+
await awaitFakeTimer();
892+
expect(onPopupAlign).toHaveBeenCalledTimes(2);
893+
});
845894
});

tests/util.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { act } from "@testing-library/react";
2+
13
const autoAdjustOverflow = {
24
adjustX: 1,
35
adjustY: 1,
@@ -91,3 +93,13 @@ export function getMouseEvent(type: string, values = {}): FakeMouseEvent {
9193
};
9294
return new FakeMouseEvent(type, values);
9395
}
96+
97+
98+
export async function awaitFakeTimer() {
99+
for (let i = 0; i < 10; i += 1) {
100+
await act(async () => {
101+
jest.advanceTimersByTime(100);
102+
await Promise.resolve();
103+
});
104+
}
105+
}

0 commit comments

Comments
 (0)