Skip to content

Commit e30f3c5

Browse files
consistent-kzombieJ
authored andcommitted
fix: DOMRect value issues (#481)
1 parent 241fa36 commit e30f3c5

7 files changed

+117
-1
lines changed

src/hooks/useAlign.ts

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ export default function useAlign(
212212
};
213213
} else {
214214
const rect = target.getBoundingClientRect();
215+
rect.x = rect.x ?? rect.left;
216+
rect.y = rect.y ?? rect.top
215217
targetRect = {
216218
x: rect.x,
217219
y: rect.y,
@@ -220,6 +222,8 @@ export default function useAlign(
220222
};
221223
}
222224
const popupRect = popupElement.getBoundingClientRect();
225+
popupRect.x = popupRect.x ?? popupRect.left;
226+
popupRect.y = popupRect.y ?? popupRect.top;
223227
const {
224228
clientWidth,
225229
clientHeight,

tests/align.test.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ describe('Trigger.Align', () => {
2626
getBoundingClientRect: () => ({
2727
x: rectX,
2828
y: rectY,
29+
left: rectX,
30+
top: rectY,
2931
width: rectWidth,
3032
height: rectHeight,
3133
right: 200,

tests/arrow.test.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ describe('Trigger.Arrow', () => {
5353
() => ({
5454
x: 200,
5555
y: 200,
56+
left: 200,
57+
top: 200,
5658
width: 100,
5759
height: 50,
5860
}),

tests/flip-visibleFirst.test.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,26 @@ describe('Trigger.VisibleFirst', () => {
5656
let containerRect = {
5757
x: 0,
5858
y: 0,
59+
left: 0,
60+
top: 0,
5961
width: 100,
6062
height: 100,
6163
};
6264

6365
let targetRect = {
6466
x: 0,
6567
y: 0,
68+
left: 0,
69+
top: 0,
6670
width: 1,
6771
height: 1,
6872
};
6973

7074
let popupRect = {
7175
x: 0,
7276
y: 0,
77+
left: 0,
78+
top: 0,
7379
width: 100,
7480
height: 100,
7581
};
@@ -84,18 +90,24 @@ describe('Trigger.VisibleFirst', () => {
8490
containerRect = {
8591
x: 0,
8692
y: 0,
93+
left: 0,
94+
top: 0,
8795
width: 100,
8896
height: 100,
8997
};
9098
targetRect = {
9199
x: 250,
92100
y: 250,
101+
left: 250,
102+
top: 250,
93103
width: 1,
94104
height: 1,
95105
};
96106
popupRect = {
97107
x: 0,
98108
y: 0,
109+
left: 0,
110+
top: 0,
99111
width: 100,
100112
height: 100,
101113
};

tests/flip.test.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ describe('Trigger.Align', () => {
5858
let spanRect = {
5959
x: 0,
6060
y: 0,
61+
left: 0,
62+
top: 0,
6163
width: 1,
6264
height: 1,
6365
};
6466

6567
let popupRect = {
6668
x: 0,
6769
y: 0,
70+
left: 0,
71+
top: 0,
6872
width: 100,
6973
height: 100,
7074
};
@@ -112,12 +116,16 @@ describe('Trigger.Align', () => {
112116
spanRect = {
113117
x: 0,
114118
y: 0,
119+
left: 0,
120+
top: 0,
115121
width: 1,
116122
height: 1,
117123
};
118124
popupRect = {
119125
x: 0,
120126
y: 0,
127+
left: 0,
128+
top: 0,
121129
width: 100,
122130
height: 100,
123131
};
@@ -253,6 +261,8 @@ describe('Trigger.Align', () => {
253261
popupRect = {
254262
x: 100,
255263
y: 1,
264+
left: 100,
265+
top: 1,
256266
width: 100,
257267
height: 100,
258268
};
@@ -339,6 +349,8 @@ describe('Trigger.Align', () => {
339349
({
340350
x: 100,
341351
y: 100,
352+
left: 100,
353+
top: 100,
342354
width: 300,
343355
height: 300,
344356
} as any);
@@ -384,6 +396,8 @@ describe('Trigger.Align', () => {
384396
popupRect = {
385397
x: 0,
386398
y: 0,
399+
left: 0,
400+
top: 0,
387401
width: 200,
388402
height: 200,
389403
};

tests/flipShift.test.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,14 @@ const builtinPlacements = {
7979
};
8080

8181
describe('Trigger.Flip+Shift', () => {
82-
let spanRect = { x: 0, y: 0, width: 0, height: 0 };
82+
let spanRect = { x: 0, y: 0, left: 0, top: 0, width: 0, height: 0 };
8383

8484
beforeEach(() => {
8585
spanRect = {
8686
x: 0,
8787
y: 100,
88+
left: 0,
89+
top: 100,
8890
width: 100,
8991
height: 100,
9092
};
@@ -113,6 +115,8 @@ describe('Trigger.Flip+Shift', () => {
113115
return {
114116
x: 0,
115117
y: 0,
118+
left: 0,
119+
top: 0,
116120
width: 100,
117121
height: 300,
118122
};

tests/rect.test.tsx

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { cleanup, render } from '@testing-library/react';
2+
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
3+
import React from 'react';
4+
import Trigger from '../src';
5+
import { awaitFakeTimer } from './util';
6+
7+
describe('Trigger.Rect', () => {
8+
let targetVisible = true;
9+
10+
let rectX = 100;
11+
let rectY = 100;
12+
let rectWidth = 100;
13+
let rectHeight = 100;
14+
15+
beforeAll(() => {
16+
spyElementPrototypes(HTMLDivElement, {
17+
getBoundingClientRect: () => ({
18+
left: rectX,
19+
top: rectY,
20+
width: rectWidth,
21+
height: rectHeight,
22+
right: 200,
23+
bottom: 200,
24+
}),
25+
});
26+
27+
spyElementPrototypes(HTMLElement, {
28+
offsetParent: {
29+
get: () => (targetVisible ? document.body : null),
30+
},
31+
});
32+
spyElementPrototypes(SVGElement, {
33+
offsetParent: {
34+
get: () => (targetVisible ? document.body : null),
35+
},
36+
});
37+
});
38+
39+
beforeEach(() => {
40+
targetVisible = true;
41+
42+
rectX = 100;
43+
rectY = 100;
44+
rectWidth = 100;
45+
rectHeight = 100;
46+
47+
jest.useFakeTimers();
48+
});
49+
50+
afterEach(() => {
51+
cleanup();
52+
jest.useRealTimers();
53+
});
54+
55+
it('getBoundingClientRect top and left', async () => {
56+
render(
57+
<Trigger
58+
popupVisible
59+
popup={<span className="bamboo" />}
60+
popupAlign={{
61+
points: ['bc', 'tc'],
62+
_experimental: {
63+
dynamicInset: true,
64+
},
65+
}}
66+
>
67+
<div />
68+
</Trigger>,
69+
);
70+
71+
await awaitFakeTimer();
72+
73+
expect(document.querySelector('.rc-trigger-popup')).toHaveStyle({
74+
bottom: `100px`,
75+
});
76+
});
77+
78+
});

0 commit comments

Comments
 (0)