Skip to content

Commit 08b13f8

Browse files
authored
fix: use scrollIntoParentView to prevent page scroll (#633)
1 parent d647afc commit 08b13f8

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

src/OptionList/Column.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import pickAttrs from '@rc-component/util/lib/pickAttrs';
44
import type { DefaultOptionType, SingleValueType } from '../Cascader';
55
import CascaderContext from '../context';
66
import { SEARCH_MARK } from '../hooks/useSearchOptions';
7-
import { isLeaf, toPathKey } from '../utils/commonUtil';
7+
import { isLeaf, scrollIntoParentView, toPathKey } from '../utils/commonUtil';
88
import Checkbox from './Checkbox';
99

1010
export const FIX_LABEL = '__cascader_fix_label__';
@@ -110,10 +110,7 @@ export default function Column<OptionType extends DefaultOptionType = DefaultOpt
110110
const activeElement = menuRef.current.querySelector<HTMLElement>(selector);
111111

112112
if (activeElement) {
113-
activeElement.scrollIntoView({
114-
block: 'nearest',
115-
inline: 'nearest',
116-
});
113+
scrollIntoParentView(activeElement);
117114
}
118115
}
119116
}, [activeValue, menuItemPrefixCls]);

src/utils/commonUtil.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export function scrollIntoParentView(element: HTMLElement) {
5353
const elementToParent = element.offsetTop - parent.offsetTop; // offsetParent may not be parent.
5454
if (elementToParent - parent.scrollTop < 0) {
5555
parent.scrollTo({ top: elementToParent });
56-
} else if (elementToParent + element.offsetHeight - parent.scrollTop > parent.offsetHeight) {
56+
} else if (
57+
elementToParent + element.offsetHeight - parent.scrollTop > parent.offsetHeight
58+
) {
5759
parent.scrollTo({ top: elementToParent + element.offsetHeight - parent.offsetHeight });
5860
}
5961
}

tests/index.spec.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CascaderRef, BaseOptionType, CascaderProps } from '../src';
44
import Cascader from '../src';
55
import { addressOptions, addressOptionsForUneven, optionsForActiveMenuItems } from './demoOptions';
66
import { mount } from './enzyme';
7-
import { toRawValues } from '../src/utils/commonUtil';
7+
import * as commonUtil from '../src/utils/commonUtil';
88
import { act, fireEvent, render } from '@testing-library/react';
99
import KeyCode from '@rc-component/util/lib/KeyCode';
1010
import { expectOpen, selectOption } from './util';
@@ -1075,7 +1075,7 @@ describe('Cascader.Basic', () => {
10751075
});
10761076
const items = wrapper.container.querySelectorAll('.rc-cascader-menu-item');
10771077
fireEvent.mouseEnter(items[9]);
1078-
expect(mockScrollTo).toHaveBeenCalledTimes(0);
1078+
expect(mockScrollTo).toHaveBeenCalledTimes(1);
10791079

10801080
spyElement.mockRestore();
10811081
});
@@ -1094,18 +1094,12 @@ describe('Cascader.Basic', () => {
10941094
const input = container.querySelector('input')!;
10951095
fireEvent.focus(input);
10961096

1097+
const spy = jest.spyOn(commonUtil, 'scrollIntoParentView');
10971098
fireEvent.keyDown(input, { key: 'ArrowDown', keyCode: KeyCode.DOWN });
10981099

10991100
const targetElement = container.querySelector('.rc-cascader-menu-item-active')!;
1100-
1101-
const scrollSpy = jest.spyOn(targetElement, 'scrollIntoView').mockImplementation(() => null);
1102-
1103-
expect(scrollSpy).toHaveBeenCalledWith({
1104-
block: 'nearest',
1105-
inline: 'nearest',
1106-
});
1107-
1108-
scrollSpy.mockReset();
1101+
expect(spy).toHaveBeenCalledWith(targetElement);
1102+
spy.mockRestore();
11091103
});
11101104
});
11111105

@@ -1149,7 +1143,7 @@ describe('Cascader.Basic', () => {
11491143
errorSpy.mockReset();
11501144
});
11511145
it('toRawValues undefined', () => {
1152-
expect(toRawValues()).toEqual([]);
1146+
expect(commonUtil.toRawValues()).toEqual([]);
11531147
});
11541148

11551149
it('nativeElement', () => {

0 commit comments

Comments
 (0)