Skip to content

Commit 91af3c4

Browse files
authored
refactor: adjust popupAlign after placement to align with origin logic (#330)
* fix: align order of it * test: test case
1 parent 4cd443f commit 91af3c4

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
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-
popupAlign || builtinPlacements[placement] || {};
207+
builtinPlacements[placement] || popupAlign || {};
208208

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

src/index.tsx

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

361+
// When no builtinPlacements and popupAlign changed
361362
useLayoutEffect(() => {
362-
if(!mergedOpen) return;
363-
triggerAlign();
363+
if (mergedOpen && !builtinPlacements?.[popupPlacement]) {
364+
triggerAlign();
365+
}
364366
}, [JSON.stringify(popupAlign)]);
365367

366368
const alignedClassName = React.useMemo(() => {

tests/align.test.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +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";
6+
import { awaitFakeTimer } from './util';
77

88
import { _rs } from 'rc-resize-observer';
99

@@ -108,4 +108,26 @@ describe('Trigger.Align', () => {
108108
await awaitFakeTimer();
109109
expect(onAlign).toHaveBeenCalled();
110110
});
111+
112+
it('placement is higher than popupAlign', async () => {
113+
render(
114+
<Trigger
115+
popupVisible
116+
popup={<span className="bamboo" />}
117+
builtinPlacements={{
118+
top: {},
119+
}}
120+
popupPlacement="top"
121+
popupAlign={{}}
122+
>
123+
<span />
124+
</Trigger>,
125+
);
126+
127+
await awaitFakeTimer();
128+
129+
expect(
130+
document.querySelector('.rc-trigger-popup-placement-top'),
131+
).toBeTruthy();
132+
});
111133
});

0 commit comments

Comments
 (0)