Skip to content

Commit 0587b35

Browse files
authored
fix: use getBoundingClientRect to solve the width precision problem (#959)
* fix: use getBoundingClientRect to solve the width precision problem * chore: change test case
1 parent 10b9601 commit 0587b35

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/BaseSelect.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ const BaseSelect = React.forwardRef((props: BaseSelectProps, ref: React.Ref<Base
631631

632632
useLayoutEffect(() => {
633633
if (triggerOpen) {
634-
const newWidth = Math.ceil(containerRef.current?.offsetWidth);
634+
// Guaranteed width accuracy
635+
const newWidth = Math.ceil(containerRef.current?.getBoundingClientRect().width);
635636
if (containerWidth !== newWidth && !Number.isNaN(newWidth)) {
636637
setContainerWidth(newWidth);
637638
}

tests/Select.test.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { fireEvent, render as testingRender } from '@testing-library/react';
22
import { mount, render } from 'enzyme';
33
import KeyCode from 'rc-util/lib/KeyCode';
4-
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
4+
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
55
import { resetWarned } from 'rc-util/lib/warning';
66
import VirtualList from 'rc-virtual-list';
77
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
@@ -1323,8 +1323,10 @@ describe('Select.Basic', () => {
13231323
let domHook;
13241324

13251325
beforeAll(() => {
1326-
domHook = spyElementPrototype(HTMLElement, 'offsetWidth', {
1327-
get: () => 1000,
1326+
domHook = spyElementPrototypes(HTMLElement, {
1327+
getBoundingClientRect: () => ({
1328+
width: 1000
1329+
}),
13281330
});
13291331
});
13301332

0 commit comments

Comments
 (0)