Skip to content

enhance: Trigger use round position to fix precision #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/hooks/useAlign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,22 @@ export default function useAlign(
onPopupAlign?.(popupEle, nextAlignInfo);

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

setOffsetInfo({
if (scaleX === 1) {
nextOffsetX = Math.round(nextOffsetX);
offsetX4Right = Math.round(offsetX4Right);
}

if (scaleY === 1) {
nextOffsetY = Math.round(nextOffsetY);
offsetY4Bottom = Math.round(offsetY4Bottom);
}

const nextOffsetInfo = {
ready: true,
offsetX: nextOffsetX / scaleX,
offsetY: nextOffsetY / scaleY,
Expand All @@ -686,7 +696,9 @@ export default function useAlign(
scaleX,
scaleY,
align: nextAlignInfo,
});
};

setOffsetInfo(nextOffsetInfo);
}
});

Expand Down
44 changes: 40 additions & 4 deletions tests/align.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ export const triggerResize = (target: Element) => {
describe('Trigger.Align', () => {
let targetVisible = true;

let rectX = 100;
let rectY = 100;
let rectWidth = 100;
let rectHeight = 100;

beforeAll(() => {
spyElementPrototypes(HTMLDivElement, {
getBoundingClientRect: () => ({
x: 100,
y: 100,
width: 100,
height: 100,
x: rectX,
y: rectY,
width: rectWidth,
height: rectHeight,
right: 200,
bottom: 200,
}),
Expand All @@ -42,6 +47,12 @@ describe('Trigger.Align', () => {

beforeEach(() => {
targetVisible = true;

rectX = 100;
rectY = 100;
rectWidth = 100;
rectHeight = 100;

jest.useFakeTimers();
});

Expand Down Expand Up @@ -258,4 +269,29 @@ describe('Trigger.Align', () => {
bottom: `100px`,
});
});

it('round when decimal precision', async () => {
rectX = 22.6;
rectY = 33.4;
rectWidth = 33.7;
rectHeight = 55.9;

render(
<Trigger
popupVisible
popup={<span className="bamboo" />}
popupAlign={{
points: ['tl', 'bl'],
}}
>
<div />
</Trigger>,
);

await awaitFakeTimer();

expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
top: `56px`,
});
});
});
Loading