-
-
Notifications
You must be signed in to change notification settings - Fork 617
fix: fix first rows scroll down when expand #1256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Vinh241
wants to merge
2
commits into
react-component:master
from
Vinh241:fixbug/table-virtual-expand-first-row
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -559,4 +559,133 @@ | |
| expect(container.querySelector('.rc-table')).toHaveClass('rc-table-fix-end-shadow-show'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('expanding and collapsing rows', () => { | ||
| it('preserves scroll position when expanding non-first rows', async () => { | ||
| vi.spyOn(global, 'requestAnimationFrame').mockImplementation(cb => { | ||
| cb(0); | ||
| return 0; | ||
| }); | ||
|
|
||
| // Setup a spy on scrollTo | ||
| const originalScrollTo = HTMLElement.prototype.scrollTo; | ||
| const scrollToSpy = vi.fn(); | ||
| HTMLElement.prototype.scrollTo = scrollToSpy; | ||
|
|
||
| // Create a controlled expandable table | ||
| let expandedRowKeys: React.Key[] = []; | ||
|
|
||
| const expandHandler = vi.fn((expanded, record) => { | ||
| if (expanded) { | ||
| expandedRowKeys = [...expandedRowKeys, record.name]; | ||
| } else { | ||
| expandedRowKeys = expandedRowKeys.filter(key => key !== record.name); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这行没跑到 |
||
| } | ||
| }); | ||
|
|
||
| const { container, rerender } = render( | ||
| <VirtualTable | ||
| columns={[{ dataIndex: 'name' }, { dataIndex: 'age' }, { dataIndex: 'address' }]} | ||
| rowKey="name" | ||
| scroll={{ x: 100, y: 100 }} | ||
| data={new Array(100).fill(null).map((_, index) => ({ | ||
| name: `name${index}`, | ||
| age: index, | ||
| address: `address${index}`, | ||
| }))} | ||
| expandable={{ | ||
| expandedRowKeys, | ||
| onExpand: expandHandler, | ||
| expandedRowRender: record => `Expanded ${record.name}`, | ||
| }} | ||
| />, | ||
| ); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| // Get expand buttons | ||
| const expandButtons = container.querySelectorAll('.rc-table-row-expand-icon'); | ||
|
|
||
| // Clear any previous scroll calls | ||
| scrollToSpy.mockClear(); | ||
|
|
||
| try { | ||
| // 1. First test - Expanding the first row should not cause unwanted scrolling | ||
| const firstRowExpandButton = expandButtons[0]; | ||
| fireEvent.click(firstRowExpandButton); | ||
|
|
||
| // Force rerender with the new expanded keys | ||
| rerender( | ||
| <VirtualTable | ||
| columns={[{ dataIndex: 'name' }, { dataIndex: 'age' }, { dataIndex: 'address' }]} | ||
| rowKey="name" | ||
| scroll={{ x: 100, y: 100 }} | ||
| data={new Array(100).fill(null).map((_, index) => ({ | ||
| name: `name${index}`, | ||
| age: index, | ||
| address: `address${index}`, | ||
| }))} | ||
| expandable={{ | ||
| expandedRowKeys: ['name0'], | ||
| onExpand: expandHandler, | ||
| expandedRowRender: record => `Expanded ${record.name}`, | ||
| }} | ||
| />, | ||
| ); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| // Verify our fix is working - the fix should prevent forced scrolling to top (0) | ||
| // for first row expansion | ||
| const forceScrollToTopCalls = scrollToSpy.mock.calls.filter( | ||
| call => call[0] && typeof call[0] === 'object' && 'top' in call[0] && call[0].top === 0, | ||
| ); | ||
|
|
||
| // Our fix should be preventing unnecessary scrollTo(0) calls | ||
| expect(forceScrollToTopCalls.length).toBe(0); | ||
|
|
||
| // 2. Next test - Non-first row expansion should not force scroll to top | ||
| scrollToSpy.mockClear(); | ||
|
|
||
| // Click fifth row expand button | ||
| const fifthRowExpandButton = expandButtons[5]; | ||
| fireEvent.click(fifthRowExpandButton); | ||
|
|
||
| // Update expanded keys | ||
| rerender( | ||
| <VirtualTable | ||
| columns={[{ dataIndex: 'name' }, { dataIndex: 'age' }, { dataIndex: 'address' }]} | ||
| rowKey="name" | ||
| scroll={{ x: 100, y: 100 }} | ||
| data={new Array(100).fill(null).map((_, index) => ({ | ||
| name: `name${index}`, | ||
| age: index, | ||
| address: `address${index}`, | ||
| }))} | ||
| expandable={{ | ||
| expandedRowKeys: ['name0', 'name5'], | ||
| onExpand: expandHandler, | ||
| expandedRowRender: record => `Expanded ${record.name}`, | ||
| }} | ||
| />, | ||
| ); | ||
|
|
||
| await waitFakeTimer(); | ||
|
|
||
| // Again check no forced scrollTo(0) is happening | ||
| const nonFirstRowForceScrollCalls = scrollToSpy.mock.calls.filter( | ||
| call => call[0] && typeof call[0] === 'object' && 'top' in call[0] && call[0].top === 0, | ||
| ); | ||
|
|
||
| // Our fix should prevent unnecessary scrollTo(0) calls for non-first rows too | ||
| expect(nonFirstRowForceScrollCalls.length).toBe(0); | ||
|
|
||
| // Test passes if we make it here without unwanted scrolling | ||
| expect(true).toBeTruthy(); | ||
| } finally { | ||
| // Restore original methods | ||
| HTMLElement.prototype.scrollTo = originalScrollTo; | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems be the
rc-virtual-listlength extends bug. Not a better place to fix inrc-table