Skip to content

Commit 9017520

Browse files
authored
fix: measure persion (#316)
* fix: measure persion * test: more test * chore: comment
1 parent 032df04 commit 9017520

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/Popup/useStretchStyle.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ export default (
77
const [targetSize, setTargetSize] = React.useState({ width: 0, height: 0 });
88

99
function measureStretch(element: HTMLElement) {
10+
let { offsetWidth: tgtWidth, offsetHeight: tgtHeight } = element;
11+
const { width, height } = element.getBoundingClientRect();
12+
13+
// Rect is more accurate than offset, use if near
14+
if (Math.abs(tgtWidth - width) < 1 && Math.abs(tgtHeight - height) < 1) {
15+
tgtWidth = width;
16+
tgtHeight = height;
17+
}
18+
1019
setTargetSize({
11-
width: element.offsetWidth,
12-
height: element.offsetHeight,
20+
width: tgtWidth,
21+
height: tgtHeight,
1322
});
1423
}
1524

tests/basic.test.jsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable max-classes-per-file */
22

3-
import React, { StrictMode, createRef } from 'react';
4-
import ReactDOM from 'react-dom';
53
import { act, cleanup, fireEvent, render } from '@testing-library/react';
64
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
5+
import React, { createRef, StrictMode } from 'react';
6+
import ReactDOM from 'react-dom';
77
import Trigger from '../src';
88
import { placementAlignMap } from './util';
99

@@ -480,15 +480,21 @@ describe('Trigger.Basic', () => {
480480
</Trigger>,
481481
);
482482

483+
const width = 1128;
484+
const height = 903;
483485
let domSpy;
486+
let rect = {};
484487

485488
beforeAll(() => {
486489
domSpy = spyElementPrototypes(HTMLElement, {
487490
offsetWidth: {
488-
get: () => 1128,
491+
get: () => width,
489492
},
490493
offsetHeight: {
491-
get: () => 903,
494+
get: () => height,
495+
},
496+
getBoundingClientRect() {
497+
return rect;
492498
},
493499
});
494500
});
@@ -497,16 +503,19 @@ describe('Trigger.Basic', () => {
497503
domSpy.mockRestore();
498504
});
499505

500-
['width', 'height', 'min-width', 'min-height'].forEach((prop) => {
501-
it(prop, () => {
502-
const { container } = createTrigger(prop);
506+
[null, { width, height }].forEach((mockRect) => {
507+
['width', 'height', 'min-width', 'min-height'].forEach((prop) => {
508+
it(`${mockRect ? 'offset' : 'getBoundingClientRect'}: ${prop}`, () => {
509+
const { container } = createTrigger(prop);
510+
rect = mockRect || {};
503511

504-
fireEvent.click(container.querySelector('.target'));
505-
act(() => jest.runAllTimers());
512+
fireEvent.click(container.querySelector('.target'));
513+
act(() => jest.runAllTimers());
506514

507-
expect(
508-
document.querySelector('.rc-trigger-popup').style,
509-
).toHaveProperty(prop);
515+
expect(
516+
document.querySelector('.rc-trigger-popup').style,
517+
).toHaveProperty(prop);
518+
});
510519
});
511520
});
512521
});

0 commit comments

Comments
 (0)