Skip to content

Commit e7348d9

Browse files
authored
enhance: Trigger use round position to fix precision (#465)
* enhance: round of pix * test: add test case
1 parent 687c28f commit e7348d9

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/hooks/useAlign.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -670,12 +670,22 @@ export default function useAlign(
670670
onPopupAlign?.(popupEle, nextAlignInfo);
671671

672672
// Additional calculate right & bottom position
673-
const offsetX4Right =
673+
let offsetX4Right =
674674
popupMirrorRect.right - popupRect.x - (nextOffsetX + popupRect.width);
675-
const offsetY4Bottom =
675+
let offsetY4Bottom =
676676
popupMirrorRect.bottom - popupRect.y - (nextOffsetY + popupRect.height);
677677

678-
setOffsetInfo({
678+
if (scaleX === 1) {
679+
nextOffsetX = Math.round(nextOffsetX);
680+
offsetX4Right = Math.round(offsetX4Right);
681+
}
682+
683+
if (scaleY === 1) {
684+
nextOffsetY = Math.round(nextOffsetY);
685+
offsetY4Bottom = Math.round(offsetY4Bottom);
686+
}
687+
688+
const nextOffsetInfo = {
679689
ready: true,
680690
offsetX: nextOffsetX / scaleX,
681691
offsetY: nextOffsetY / scaleY,
@@ -686,7 +696,9 @@ export default function useAlign(
686696
scaleX,
687697
scaleY,
688698
align: nextAlignInfo,
689-
});
699+
};
700+
701+
setOffsetInfo(nextOffsetInfo);
690702
}
691703
});
692704

tests/align.test.tsx

+40-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ export const triggerResize = (target: Element) => {
1616
describe('Trigger.Align', () => {
1717
let targetVisible = true;
1818

19+
let rectX = 100;
20+
let rectY = 100;
21+
let rectWidth = 100;
22+
let rectHeight = 100;
23+
1924
beforeAll(() => {
2025
spyElementPrototypes(HTMLDivElement, {
2126
getBoundingClientRect: () => ({
22-
x: 100,
23-
y: 100,
24-
width: 100,
25-
height: 100,
27+
x: rectX,
28+
y: rectY,
29+
width: rectWidth,
30+
height: rectHeight,
2631
right: 200,
2732
bottom: 200,
2833
}),
@@ -42,6 +47,12 @@ describe('Trigger.Align', () => {
4247

4348
beforeEach(() => {
4449
targetVisible = true;
50+
51+
rectX = 100;
52+
rectY = 100;
53+
rectWidth = 100;
54+
rectHeight = 100;
55+
4556
jest.useFakeTimers();
4657
});
4758

@@ -258,4 +269,29 @@ describe('Trigger.Align', () => {
258269
bottom: `100px`,
259270
});
260271
});
272+
273+
it('round when decimal precision', async () => {
274+
rectX = 22.6;
275+
rectY = 33.4;
276+
rectWidth = 33.7;
277+
rectHeight = 55.9;
278+
279+
render(
280+
<Trigger
281+
popupVisible
282+
popup={<span className="bamboo" />}
283+
popupAlign={{
284+
points: ['tl', 'bl'],
285+
}}
286+
>
287+
<div />
288+
</Trigger>,
289+
);
290+
291+
await awaitFakeTimer();
292+
293+
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
294+
top: `56px`,
295+
});
296+
});
261297
});

0 commit comments

Comments
 (0)