Skip to content

Commit 1a1ae6c

Browse files
authored
[Discover] Fix infinite scrolling of classic table (#110944)
1 parent 1eed669 commit 1a1ae6c

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

src/plugins/discover/public/application/apps/main/components/doc_table/doc_table_infinite.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { debounce } from 'lodash';
1313
import { EuiButtonEmpty } from '@elastic/eui';
1414
import { DocTableProps, DocTableRenderProps, DocTableWrapper } from './doc_table_wrapper';
1515
import { SkipBottomButton } from '../skip_bottom_button';
16+
import { shouldLoadNextDocPatch } from './lib/should_load_next_doc_patch';
1617

1718
const FOOTER_PADDING = { padding: 0 };
1819

@@ -35,12 +36,7 @@ const DocTableInfiniteContent = (props: DocTableRenderProps) => {
3536
const scheduleCheck = debounce(() => {
3637
const isMobileView = document.getElementsByClassName('dscSidebar__mobile').length > 0;
3738
const usedScrollDiv = isMobileView ? scrollMobileElem : scrollDiv;
38-
39-
const scrollusedHeight = usedScrollDiv.scrollHeight;
40-
const scrollTop = Math.abs(usedScrollDiv.scrollTop);
41-
const clientHeight = usedScrollDiv.clientHeight;
42-
43-
if (scrollTop + clientHeight === scrollusedHeight) {
39+
if (shouldLoadNextDocPatch(usedScrollDiv)) {
4440
setLimit((prevLimit) => prevLimit + 50);
4541
}
4642
}, 50);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { shouldLoadNextDocPatch } from './should_load_next_doc_patch';
10+
11+
describe('shouldLoadNextDocPatch', () => {
12+
test('next patch should not be loaded', () => {
13+
const scrollingDomEl = {
14+
scrollHeight: 500,
15+
scrollTop: 100,
16+
clientHeight: 100,
17+
} as HTMLElement;
18+
19+
expect(shouldLoadNextDocPatch(scrollingDomEl)).toBeFalsy();
20+
});
21+
22+
test('next patch should be loaded', () => {
23+
const scrollingDomEl = {
24+
scrollHeight: 500,
25+
scrollTop: 350,
26+
clientHeight: 100,
27+
} as HTMLElement;
28+
29+
expect(shouldLoadNextDocPatch(scrollingDomEl)).toBeTruthy();
30+
});
31+
32+
test("next patch should be loaded even there's a decimal scroll height", () => {
33+
const scrollingDomEl = {
34+
scrollHeight: 500,
35+
scrollTop: 350.34234234,
36+
clientHeight: 100,
37+
} as HTMLElement;
38+
39+
expect(shouldLoadNextDocPatch(scrollingDomEl)).toBeTruthy();
40+
});
41+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
// use a buffer to start rendering more documents before the user completely scrolles down
10+
const verticalScrollBuffer = 100;
11+
12+
/**
13+
* Helper function to determine if the next patch of 50 documents should be loaded
14+
*/
15+
export function shouldLoadNextDocPatch(domEl: HTMLElement) {
16+
// the height of the scrolling div, including content not visible on the screen due to overflow.
17+
const scrollHeight = domEl.scrollHeight;
18+
// the number of pixels that the div is is scrolled vertically
19+
const scrollTop = domEl.scrollTop;
20+
// the inner height of the scrolling div, excluding content that's visible on the screen
21+
const clientHeight = domEl.clientHeight;
22+
23+
const consumedHeight = scrollTop + clientHeight;
24+
const remainingHeight = scrollHeight - consumedHeight;
25+
return remainingHeight < verticalScrollBuffer;
26+
}

0 commit comments

Comments
 (0)