Skip to content

Commit aaea98c

Browse files
authored
fix: Align flip should both work (#599)
* fix: reverse should be OK * test: add test case
1 parent 338a80f commit aaea98c

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

src/hooks/useAlign.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,21 @@ function getAlignPoint(rect: Rect, points: Points) {
6969
return { x, y };
7070
}
7171

72-
function reversePoints(points: Points, index: number): string {
72+
function reversePoints(points: Points, index: number): Points {
7373
const reverseMap = {
7474
t: 'b',
7575
b: 't',
7676
l: 'r',
7777
r: 'l',
7878
};
7979

80-
return points
81-
.map((point, i) => {
82-
if (i === index) {
83-
return reverseMap[point] || 'c';
84-
}
85-
return point;
86-
})
87-
.join('');
80+
const clone = [...points] as Points;
81+
clone[index] = reverseMap[points[index]] || 'c';
82+
return clone;
83+
}
84+
85+
function flatPoints(points: Points): string {
86+
return points.join('');
8887
}
8988

9089
export default function useAlign(
@@ -340,6 +339,8 @@ export default function useAlign(
340339
...placementInfo,
341340
};
342341

342+
let nextPoints = [popupPoints, targetPoints];
343+
343344
// Next Offset
344345
let nextOffsetX = targetAlignPoint.x - popupAlignPoint.x + popupOffsetX;
345346
let nextOffsetY = targetAlignPoint.y - popupAlignPoint.y + popupOffsetY;
@@ -450,9 +451,9 @@ export default function useAlign(
450451
nextOffsetY = tmpNextOffsetY;
451452
popupOffsetY = -popupOffsetY;
452453

453-
nextAlignInfo.points = [
454-
reversePoints(popupPoints, 0),
455-
reversePoints(targetPoints, 0),
454+
nextPoints = [
455+
reversePoints(nextPoints[0], 0),
456+
reversePoints(nextPoints[1], 0),
456457
];
457458
} else {
458459
prevFlipRef.current.bt = false;
@@ -496,9 +497,9 @@ export default function useAlign(
496497
nextOffsetY = tmpNextOffsetY;
497498
popupOffsetY = -popupOffsetY;
498499

499-
nextAlignInfo.points = [
500-
reversePoints(popupPoints, 0),
501-
reversePoints(targetPoints, 0),
500+
nextPoints = [
501+
reversePoints(nextPoints[0], 0),
502+
reversePoints(nextPoints[1], 0),
502503
];
503504
} else {
504505
prevFlipRef.current.tb = false;
@@ -549,9 +550,9 @@ export default function useAlign(
549550
nextOffsetX = tmpNextOffsetX;
550551
popupOffsetX = -popupOffsetX;
551552

552-
nextAlignInfo.points = [
553-
reversePoints(popupPoints, 1),
554-
reversePoints(targetPoints, 1),
553+
nextPoints = [
554+
reversePoints(nextPoints[0], 1),
555+
reversePoints(nextPoints[1], 1),
555556
];
556557
} else {
557558
prevFlipRef.current.rl = false;
@@ -595,15 +596,20 @@ export default function useAlign(
595596
nextOffsetX = tmpNextOffsetX;
596597
popupOffsetX = -popupOffsetX;
597598

598-
nextAlignInfo.points = [
599-
reversePoints(popupPoints, 1),
600-
reversePoints(targetPoints, 1),
599+
nextPoints = [
600+
reversePoints(nextPoints[0], 1),
601+
reversePoints(nextPoints[1], 1),
601602
];
602603
} else {
603604
prevFlipRef.current.lr = false;
604605
}
605606
}
606607

608+
nextAlignInfo.points = [
609+
flatPoints(nextPoints[0]),
610+
flatPoints(nextPoints[1]),
611+
];
612+
607613
// ============================ Shift ============================
608614
syncNextPopupPosition();
609615

src/util.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import type {
2-
AlignType,
3-
BuildInPlacements,
4-
} from './interface';
1+
import type { AlignType, BuildInPlacements } from './interface';
52

63
function isPointsEq(
74
a1: string[] = [],
85
a2: string[] = [],
96
isAlignPoint: boolean,
107
): boolean {
8+
const getVal = (a: string[], index: number) => a[index] || '';
9+
1110
if (isAlignPoint) {
12-
return a1[0] === a2[0];
11+
return getVal(a1, 0) === getVal(a2, 0);
1312
}
14-
return a1[0] === a2[0] && a1[1] === a2[1];
13+
return getVal(a1, 0) === getVal(a2, 0) && getVal(a1, 1) === getVal(a2, 1);
1514
}
1615

1716
export function getAlignPopupClassName(

tests/align.test.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,41 @@ describe('Trigger.Align', () => {
296296
top: `56px`,
297297
});
298298
});
299+
300+
it('both adjustX and adjustY should get correct points', async () => {
301+
// Set target position to top left corner to force flip to bottom right
302+
rectX = 0;
303+
rectY = 0;
304+
rectWidth = 100;
305+
rectHeight = 100;
306+
307+
const onPopupAlign = jest.fn();
308+
309+
render(
310+
<Trigger
311+
popupVisible
312+
popup={<span className="bamboo" />}
313+
popupAlign={{
314+
points: ['tl', 'bl'],
315+
overflow: {
316+
adjustX: true,
317+
adjustY: true,
318+
},
319+
}}
320+
onPopupAlign={onPopupAlign}
321+
>
322+
<div />
323+
</Trigger>,
324+
);
325+
326+
await awaitFakeTimer();
327+
328+
// Check that the points have been flipped correctly
329+
expect(onPopupAlign).toHaveBeenCalledWith(
330+
expect.anything(),
331+
expect.objectContaining({
332+
points: ['br', 'tr'],
333+
}),
334+
);
335+
});
299336
});

0 commit comments

Comments
 (0)