-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Table Visualization] restore pagination to table vis (#2461)
* add pagination patically resolved: #2305 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
- Loading branch information
Showing
4 changed files
with
46 additions
and
16 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
*/ | ||
|
||
export * from './convert_to_formatted_data'; | ||
export * from './use_pagination'; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { TableVisConfig } from '../types'; | ||
|
||
export const usePagination = (visConfig: TableVisConfig, nrow: number) => { | ||
const [pagination, setPagination] = useState({ | ||
pageIndex: 0, | ||
pageSize: visConfig.perPage, | ||
}); | ||
const onChangeItemsPerPage = useCallback( | ||
(pageSize) => setPagination((p) => ({ ...p, pageSize, pageIndex: 0 })), | ||
[setPagination] | ||
); | ||
const onChangePage = useCallback((pageIndex) => setPagination((p) => ({ ...p, pageIndex })), [ | ||
setPagination, | ||
]); | ||
|
||
useEffect(() => { | ||
const maxiPageIndex = Math.ceil(nrow / visConfig.perPage) - 1; | ||
setPagination((p) => ({ | ||
pageIndex: p.pageIndex > maxiPageIndex ? maxiPageIndex : p.pageIndex, | ||
pageSize: visConfig.perPage, | ||
})); | ||
}, [nrow, visConfig.perPage]); | ||
|
||
return useMemo( | ||
() => ({ | ||
...pagination, | ||
onChangeItemsPerPage, | ||
onChangePage, | ||
}), | ||
[pagination, onChangeItemsPerPage, onChangePage] | ||
); | ||
}; |