forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Clean up pagination handling in Unified Data Table #4
Closed
davismcphee
wants to merge
2
commits into
logeekal:fix/pagination_unified_data_table
from
davismcphee:cleanuo-fix/pagination_unified_data_table
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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ import { | |||||
EuiDataGridProps, | ||||||
EuiHorizontalRule, | ||||||
EuiDataGridToolBarVisibilityDisplaySelectorOptions, | ||||||
EuiDataGridPaginationProps, | ||||||
} from '@elastic/eui'; | ||||||
import type { DataView } from '@kbn/data-views-plugin/public'; | ||||||
import { | ||||||
|
@@ -261,6 +262,12 @@ export interface UnifiedDataTableProps { | |||||
* Update rows per page state | ||||||
*/ | ||||||
onUpdateRowsPerPage?: (rowsPerPage: number) => void; | ||||||
/** | ||||||
* | ||||||
* this callback is triggered when user navigates to a different page | ||||||
* | ||||||
*/ | ||||||
onUpdatePageIndex?: (pageIndex: number) => void; | ||||||
/** | ||||||
* Configuration option to limit sample size slider | ||||||
*/ | ||||||
|
@@ -420,12 +427,6 @@ export interface UnifiedDataTableProps { | |||||
* @param row | ||||||
*/ | ||||||
getRowIndicator?: ColorIndicatorControlColumnParams['getRowIndicator']; | ||||||
/** | ||||||
* | ||||||
* this callback is triggered when user navigates to a different page | ||||||
* | ||||||
*/ | ||||||
onChangePage?: (pageIndex: number) => void; | ||||||
} | ||||||
|
||||||
export const EuiDataGridMemoized = React.memo(EuiDataGrid); | ||||||
|
@@ -470,6 +471,7 @@ export const UnifiedDataTable = ({ | |||||
isPlainRecord = false, | ||||||
rowsPerPageState, | ||||||
onUpdateRowsPerPage, | ||||||
onUpdatePageIndex, | ||||||
onFieldEdited, | ||||||
services, | ||||||
renderCustomGridBody, | ||||||
|
@@ -499,7 +501,6 @@ export const UnifiedDataTable = ({ | |||||
getRowIndicator, | ||||||
dataGridDensityState, | ||||||
onUpdateDataGridDensity, | ||||||
onChangePage: onChangePageProp, | ||||||
}: UnifiedDataTableProps) => { | ||||||
const { fieldFormats, toastNotifications, dataViewFieldEditor, uiSettings, storage, data } = | ||||||
services; | ||||||
|
@@ -603,74 +604,42 @@ export const UnifiedDataTable = ({ | |||||
typeof rowsPerPageState === 'number' && rowsPerPageState > 0 | ||||||
? rowsPerPageState | ||||||
: DEFAULT_ROWS_PER_PAGE; | ||||||
const [pagination, setPagination] = useState({ | ||||||
pageIndex: 0, | ||||||
pageSize: currentPageSize, | ||||||
}); | ||||||
const [currentPageIndex, setCurrentPageIndex] = useState(0); | ||||||
const rowCount = useMemo(() => (displayedRows ? displayedRows.length : 0), [displayedRows]); | ||||||
const pageCount = useMemo( | ||||||
() => Math.ceil(rowCount / pagination.pageSize), | ||||||
[rowCount, pagination] | ||||||
() => Math.ceil(rowCount / currentPageSize), | ||||||
[currentPageSize, rowCount] | ||||||
); | ||||||
|
||||||
const paginationObj = useMemo(() => { | ||||||
const pagination = useMemo<EuiDataGridPaginationProps | undefined>(() => { | ||||||
const onChangeItemsPerPage = (pageSize: number) => { | ||||||
onUpdateRowsPerPage?.(pageSize); | ||||||
}; | ||||||
|
||||||
const onChangePage = (pageIndex: number) => { | ||||||
setPagination((paginationData) => ({ ...paginationData, pageIndex })); | ||||||
setCurrentPageIndex(pageIndex); | ||||||
onUpdatePageIndex?.(pageIndex); | ||||||
}; | ||||||
|
||||||
return isPaginationEnabled | ||||||
? { | ||||||
onChangeItemsPerPage, | ||||||
onChangePage, | ||||||
pageIndex: pagination.pageIndex > pageCount - 1 ? 0 : pagination.pageIndex, | ||||||
pageSize: pagination.pageSize, | ||||||
pageSizeOptions: rowsPerPageOptions ?? getRowsPerPageOptions(pagination.pageSize), | ||||||
pageIndex: currentPageIndex > pageCount - 1 ? 0 : currentPageIndex, | ||||||
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.
Suggested change
...Then passing And we'd only need to manually sync when useEffect(() => {
setCurrentPageIndex((previousPageIndex) => {
const calculatedPageIndex = previousPageIndex > pageCount - 1 ? 0 : previousPageIndex;
if (calculatedPageIndex !== previousPageIndex) {
onUpdatePageIndex?.(calculatedPageIndex);
}
return calculatedPageIndex;
});
}, [onUpdatePageIndex, pageCount]); |
||||||
pageSize: currentPageSize, | ||||||
pageSizeOptions: rowsPerPageOptions ?? getRowsPerPageOptions(currentPageSize), | ||||||
} | ||||||
: undefined; | ||||||
}, [ | ||||||
currentPageIndex, | ||||||
currentPageSize, | ||||||
isPaginationEnabled, | ||||||
pagination.pageIndex, | ||||||
pagination.pageSize, | ||||||
onUpdatePageIndex, | ||||||
onUpdateRowsPerPage, | ||||||
pageCount, | ||||||
rowsPerPageOptions, | ||||||
onUpdateRowsPerPage, | ||||||
]); | ||||||
|
||||||
useEffect(() => { | ||||||
/* | ||||||
* Only for pageSize | ||||||
* Sync pageSize with consumer provided pageSize | ||||||
*/ | ||||||
setPagination((paginationData) => | ||||||
paginationData.pageSize === currentPageSize | ||||||
? paginationData | ||||||
: { ...paginationData, pageSize: currentPageSize } | ||||||
); | ||||||
}, [currentPageSize]); | ||||||
|
||||||
useEffect(() => { | ||||||
/* | ||||||
* Only for pageIndex | ||||||
* Sync pagination with EUI calculated pageIndex | ||||||
* | ||||||
*/ | ||||||
setPagination((prevPagination) => ({ | ||||||
...prevPagination, | ||||||
pageIndex: paginationObj?.pageIndex ?? 0, | ||||||
})); | ||||||
}, [paginationObj?.pageIndex]); | ||||||
|
||||||
useEffect(() => { | ||||||
/* | ||||||
* Propagate new pageIndex to the consumer | ||||||
*/ | ||||||
onChangePageProp?.(pagination.pageIndex); | ||||||
}, [pagination.pageIndex, onChangePageProp]); | ||||||
|
||||||
const unifiedDataTableContextValue = useMemo<DataTableContext>( | ||||||
() => ({ | ||||||
expanded: expandedDoc, | ||||||
|
@@ -683,8 +652,8 @@ export const UnifiedDataTable = ({ | |||||
valueToStringConverter, | ||||||
componentsTourSteps, | ||||||
isPlainRecord, | ||||||
pageIndex: isPaginationEnabled ? paginationObj?.pageIndex : 0, | ||||||
pageSize: isPaginationEnabled ? paginationObj?.pageSize : displayedRows.length, | ||||||
pageIndex: isPaginationEnabled ? pagination?.pageIndex : 0, | ||||||
pageSize: isPaginationEnabled ? pagination?.pageSize : displayedRows.length, | ||||||
}), | ||||||
[ | ||||||
componentsTourSteps, | ||||||
|
@@ -697,7 +666,7 @@ export const UnifiedDataTable = ({ | |||||
onFilter, | ||||||
setExpandedDoc, | ||||||
selectedDocsState, | ||||||
paginationObj, | ||||||
pagination, | ||||||
valueToStringConverter, | ||||||
] | ||||||
); | ||||||
|
@@ -1181,7 +1150,7 @@ export const UnifiedDataTable = ({ | |||||
data-test-subj="docTable" | ||||||
leadingControlColumns={leadingControlColumns} | ||||||
onColumnResize={onResize} | ||||||
pagination={paginationObj} | ||||||
pagination={pagination} | ||||||
renderCellValue={renderCellValue} | ||||||
ref={dataGridRef} | ||||||
rowCount={rowCount} | ||||||
|
@@ -1210,7 +1179,7 @@ export const UnifiedDataTable = ({ | |||||
rowCount={rowCount} | ||||||
sampleSize={sampleSizeState} | ||||||
pageCount={pageCount} | ||||||
pageIndex={paginationObj?.pageIndex} | ||||||
pageIndex={pagination?.pageIndex} | ||||||
totalHits={totalHits} | ||||||
onFetchMoreRecords={onFetchMoreRecords} | ||||||
data={data} | ||||||
|
This file contains 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 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
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.
I think this implementation could be updated to fix the bug you found by calculating the page index within this callback...
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.
@davismcphee , i do not think calculating there is needed, because
onChangePage
is not called when we need newpageIndex
to be calculated. The otheruseEffect
should be enough and i have included that.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.
Also, I think
onUpdatePageIndex
should now be solely based oncurrentPageIndex
. Any changes to that should be automatically triggeronUpdatePageIndex
instead of callingonUpdatePageIndex
at multiple places.