-
Notifications
You must be signed in to change notification settings - Fork 801
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
[Discover] Data Grid Pagination Options #5610
Changes from 3 commits
94b95f7
484e681
aed8b0d
dac7ad7
d04f92f
f170f1e
ed33807
899516d
37ec704
348d67c
bce296c
fbf7399
24937e6
25e54fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { defaultPageOptions, generatePageSizeOptions } from './page_size_options'; | ||
|
||
describe('generatePageSizeOptions', () => { | ||
it('generates default options and additional options based on sample size', () => { | ||
const sampleSize = 1000; | ||
|
||
const pageSizeOptions = generatePageSizeOptions(sampleSize); | ||
|
||
// Expected result based on the provided sample size | ||
const expectedOptions = [...defaultPageOptions, 500, 1000]; | ||
|
||
// Check if the generated options match the expected result | ||
expect(pageSizeOptions).toEqual(expectedOptions); | ||
}); | ||
|
||
it('handles edge case when sample size is less than maxSize', () => { | ||
const sampleSize = 50; | ||
|
||
// Call the function | ||
const pageSizeOptions = generatePageSizeOptions(sampleSize); | ||
|
||
// Check if the generated options match the expected result | ||
expect(pageSizeOptions).toEqual(defaultPageOptions); | ||
}); | ||
|
||
it('handles edge case when sample size is less than 0', () => { | ||
const sampleSize = -10; | ||
|
||
// Call the function | ||
const pageSizeOptions = generatePageSizeOptions(sampleSize); | ||
|
||
// Check if the generated options match the expected result | ||
expect(pageSizeOptions).toEqual(defaultPageOptions); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { uniq } from 'lodash'; | ||
|
||
/** | ||
* Generates an array of pagination options based on the provided `sampleSize`. | ||
* The array includes default values (25, 50, 100) and additional options derived from the `sampleSize` setting. | ||
* Ensures uniqueness and sorts the array in ascending order, representing available page size options for pagination. | ||
* @param {number} sampleSize - The sample size used to determine additional pagination options. | ||
* @returns {number[]} - An array of available page size options. | ||
*/ | ||
|
||
export const generatePageSizeOptions = (sampleSize: number): number[] => { | ||
if (sampleSize && sampleSize > 0) { | ||
const maxSize = 500; | ||
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. Will |
||
const pageSizeFromSetting = [...Array(Math.floor(sampleSize / maxSize)).keys()].map( | ||
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. It seems the page size options is 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. No, the idea was to provide a single page for a large dataset as per the issue. I think replacing 'floor' with 'ceil' works as expected. if you have any suggestions let me know. |
||
(i) => (i + 1) * maxSize | ||
); | ||
return uniq([...defaultPageOptions, ...pageSizeFromSetting]).sort((a, b) => a - b); | ||
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. We can replace |
||
} | ||
return defaultPageOptions; | ||
}; | ||
|
||
export const defaultPageOptions = [25, 50, 100]; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,13 +4,25 @@ | |||||
*/ | ||||||
|
||||||
import { useState, useMemo, useCallback } from 'react'; | ||||||
import { DiscoverServices } from '../../../build_services'; | ||||||
import { SAMPLE_SIZE_SETTING } from '../../../../common'; | ||||||
import { generatePageSizeOptions } from './page_size_options'; | ||||||
export interface Props { | ||||||
services: DiscoverServices; | ||||||
rowCount: number; | ||||||
} | ||||||
|
||||||
export const usePagination = ({ rowCount, services }: Props) => { | ||||||
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. I'd avoid passing the entire
Suggested change
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. yes, thanks for the suggestions. |
||||||
const { uiSettings } = services; | ||||||
|
||||||
export const usePagination = (rowCount: number) => { | ||||||
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 100 }); | ||||||
const pageCount = useMemo(() => Math.ceil(rowCount / pagination.pageSize), [ | ||||||
rowCount, | ||||||
pagination, | ||||||
]); | ||||||
const sampleSize = uiSettings.get(SAMPLE_SIZE_SETTING); | ||||||
|
||||||
const pageSizeOptions = generatePageSizeOptions(sampleSize); | ||||||
|
||||||
const onChangeItemsPerPage = useCallback( | ||||||
(pageSize: number) => setPagination((p) => ({ ...p, pageSize })), | ||||||
|
@@ -31,9 +43,9 @@ export const usePagination = (rowCount: number) => { | |||||
onChangePage, | ||||||
pageIndex: pagination.pageIndex > pageCount - 1 ? 0 : pagination.pageIndex, | ||||||
pageSize: pagination.pageSize, | ||||||
pageSizeOptions: [25, 50, 100], // TODO: make this configurable | ||||||
pageSizeOptions, | ||||||
} | ||||||
: undefined, | ||||||
[pagination, onChangeItemsPerPage, onChangePage, pageCount] | ||||||
[pagination, onChangeItemsPerPage, onChangePage, pageCount, pageSizeOptions] | ||||||
); | ||||||
}; |
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.
Don't know what changed here!