Skip to content

Commit 3308337

Browse files
authored
Merge 9699bcf into a31202a
2 parents a31202a + 9699bcf commit 3308337

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

src/hooks/useAlign.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ export default function useAlign(
269269

270270
// ============== Intersection ===============
271271
// Get area by position. Used for check if flip area is better
272-
function getIntersectionVisibleArea(x: number, y: number) {
272+
function getIntersectionVisibleArea(offsetX: number, offsetY: number) {
273+
const x = popupRect.x + offsetX;
274+
const y = popupRect.y + offsetY;
275+
273276
const r = x + popupWidth;
274277
const b = y + popupHeight;
275278

tests/flip.test.tsx

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,137 @@ describe('Trigger.Align', () => {
187187
});
188188
});
189189
});
190+
191+
describe('flip when scroll', () => {
192+
const popupRect = {
193+
width: 100,
194+
height: 100,
195+
};
196+
197+
/**
198+
* 模拟有滚动条时
199+
* popupRect的x,y值等于popupElement相对与target的位置减去target相对与视口的位置
200+
* 假设popupElement相对与target的位置x,y均为-1000
201+
*
202+
* 重置pupupElement位置 https://github.com/react-component/trigger/blob/e6fa971f97196ea791d0799f25c318c9d8c0ae0f/src/hooks/useAlign.ts#L137-L139
203+
* 获取popupRect https://github.com/react-component/trigger/blob/e6fa971f97196ea791d0799f25c318c9d8c0ae0f/src/hooks/useAlign.ts#L159
204+
*/
205+
Object.defineProperty(popupRect, 'x', {
206+
get() {
207+
return -1000 - spanRect.x;
208+
}
209+
});
210+
211+
Object.defineProperty(popupRect, 'y', {
212+
get() {
213+
return -1000 - spanRect.y;
214+
}
215+
});
216+
217+
beforeAll(() => {
218+
spyElementPrototypes(HTMLDivElement, {
219+
getBoundingClientRect() {
220+
return popupRect;
221+
}
222+
});
223+
});
224+
225+
describe('not flip if cant', () => {
226+
const list = [
227+
{
228+
placement: 'right',
229+
x: 10,
230+
className: '.rc-trigger-popup-placement-right',
231+
},
232+
{
233+
placement: 'left',
234+
x: 90,
235+
className: '.rc-trigger-popup-placement-left',
236+
},
237+
{
238+
placement: 'top',
239+
y: 90,
240+
className: '.rc-trigger-popup-placement-top',
241+
},
242+
{
243+
placement: 'bottom',
244+
y: 10,
245+
className: '.rc-trigger-popup-placement-bottom',
246+
},
247+
];
248+
249+
list.forEach(({ placement, x = 0, y = 0, className }) => {
250+
it(placement, async () => {
251+
spanRect.x = x;
252+
spanRect.y = y;
253+
254+
render(
255+
<Trigger
256+
popupVisible
257+
popupPlacement={placement}
258+
builtinPlacements={builtinPlacements}
259+
popup={<strong>trigger</strong>}
260+
>
261+
<span className="target" />
262+
</Trigger>,
263+
);
264+
265+
await act(async () => {
266+
await Promise.resolve();
267+
});
268+
269+
expect(document.querySelector(className)).toBeTruthy();
270+
});
271+
});
272+
});
273+
274+
describe('flip if can', () => {
275+
const list = [
276+
{
277+
placement: 'right',
278+
x: 90,
279+
className: '.rc-trigger-popup-placement-left',
280+
},
281+
{
282+
placement: 'left',
283+
x: 10,
284+
className: '.rc-trigger-popup-placement-right',
285+
},
286+
{
287+
placement: 'top',
288+
y: 10,
289+
className: '.rc-trigger-popup-placement-bottom',
290+
},
291+
{
292+
placement: 'bottom',
293+
y: 90,
294+
className: '.rc-trigger-popup-placement-top',
295+
},
296+
];
297+
298+
list.forEach(({ placement, x = 0, y = 0, className }) => {
299+
it(placement, async () => {
300+
spanRect.x = x;
301+
spanRect.y = y;
302+
303+
render(
304+
<Trigger
305+
popupVisible
306+
popupPlacement={placement}
307+
builtinPlacements={builtinPlacements}
308+
popup={<strong>trigger</strong>}
309+
>
310+
<span className="target" />
311+
</Trigger>,
312+
);
313+
314+
await act(async () => {
315+
await Promise.resolve();
316+
});
317+
318+
expect(document.querySelector(className)).toBeTruthy();
319+
});
320+
});
321+
});
322+
});
190323
});

0 commit comments

Comments
 (0)