-
Notifications
You must be signed in to change notification settings - Fork 78
feat(FR-1678): Add automatic page adjustment to BAITable #4636
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -17,7 +17,14 @@ import { AnyObject, GetProps } from 'antd/es/_util/type'; | |
| import { ColumnType, ColumnsType } from 'antd/es/table'; | ||
| import classNames from 'classnames'; | ||
| import _ from 'lodash'; | ||
| import { ReactNode, useEffect, useMemo, useRef, useState } from 'react'; | ||
| import { | ||
| ReactNode, | ||
| useEffect, | ||
| useEffectEvent, | ||
| useMemo, | ||
| useRef, | ||
| useState, | ||
| } from 'react'; | ||
| import { Resizable, ResizeCallbackData } from 'react-resizable'; | ||
|
|
||
| /** | ||
|
|
@@ -28,6 +35,7 @@ interface BAITablePaginationConfig | |
| extends Omit<TablePaginationConfig, 'position'> { | ||
| /** Additional content to display in the pagination area */ | ||
| extraContent?: ReactNode; | ||
| autoAdjustCurrentPage?: boolean; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -214,6 +222,7 @@ const BAITable = <RecordType extends object = any>({ | |
| ...tableProps | ||
| }: BAITableProps<RecordType>): React.ReactElement => { | ||
| const { styles } = useStyles(); | ||
| const { autoAdjustCurrentPage = true } = tableProps.pagination || {}; | ||
| const [resizedColumnWidths, setResizedColumnWidths] = useState< | ||
| Record<string, number> | ||
| >(generateResizedColumnWidths(columns)); | ||
|
|
@@ -316,6 +325,34 @@ const BAITable = <RecordType extends object = any>({ | |
| columnOverrides, | ||
| ]); | ||
|
|
||
| const adjustCurrentPage = useEffectEvent(() => { | ||
| if (!tableProps.pagination) return; | ||
| // if current page exceeds max page, adjust it | ||
| const current = currentPage || 1; | ||
| const pageSize = currentPageSize || 10; | ||
| const total = | ||
| tableProps.pagination?.total || tableProps.dataSource?.length || 0; | ||
| const maxPage = Math.max(1, Math.ceil(total / pageSize)); | ||
|
|
||
| if (current > maxPage) { | ||
| setCurrentPage(maxPage); | ||
| tableProps.pagination?.onChange?.(maxPage, pageSize); | ||
| return; | ||
| } | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (tableProps.pagination && autoAdjustCurrentPage) { | ||
| adjustCurrentPage(); | ||
| } | ||
| }, [ | ||
| tableProps.dataSource, | ||
| tableProps.pagination, | ||
| autoAdjustCurrentPage, | ||
| currentPage, | ||
| currentPageSize, | ||
| ]); | ||
|
Comment on lines
+348
to
+354
|
||
|
|
||
| return ( | ||
| <BAIFlex direction="column" align="stretch" gap={'sm'}> | ||
| <Table | ||
|
|
||
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.
The new
autoAdjustCurrentPageproperty is missing JSDoc documentation. Please add a comment describing what this option does, its default value, and when it should be disabled.Suggested documentation: