fix(admin): 修复履约渠道分页并统一列表分页状态
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Table as AntTable } from 'antd'
|
||||
import type { TableProps } from 'antd'
|
||||
import type { TablePaginationConfig, TableProps } from 'antd'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import { normalizePercentColumns } from '@/utils/percent-table-layout'
|
||||
|
||||
@@ -12,7 +13,56 @@ import { normalizePercentColumns } from '@/utils/percent-table-layout'
|
||||
export default function PercentTable<RecordType extends object = Record<string, unknown>>(
|
||||
props: TableProps<RecordType>,
|
||||
) {
|
||||
const { columns, scroll, tableLayout: _tableLayout, ...rest } = props
|
||||
const { columns, scroll, tableLayout: _tableLayout, pagination, dataSource, ...rest } = props
|
||||
const paginationConfig = pagination && typeof pagination === 'object' ? pagination : null
|
||||
const paginationIsExternallyControlled = paginationConfig?.current !== undefined
|
||||
// 本地分页也必须由组件持有 current;只交给 AntD 内部状态时,父级重渲染会丢失翻页。
|
||||
const [localPagination, setLocalPagination] = useState(() => ({
|
||||
current: 1,
|
||||
pageSize: Number(paginationConfig?.defaultPageSize || paginationConfig?.pageSize || 10),
|
||||
}))
|
||||
|
||||
useEffect(() => {
|
||||
if (paginationIsExternallyControlled || !paginationConfig?.pageSize) return
|
||||
const nextPageSize = Number(paginationConfig.pageSize)
|
||||
if (!Number.isFinite(nextPageSize) || nextPageSize <= 0) return
|
||||
setLocalPagination((current) =>
|
||||
current.pageSize === nextPageSize ? current : { current: 1, pageSize: nextPageSize },
|
||||
)
|
||||
}, [paginationConfig?.pageSize, paginationIsExternallyControlled])
|
||||
|
||||
useEffect(() => {
|
||||
if (paginationIsExternallyControlled || !paginationConfig) return
|
||||
const total = Math.max(0, Number(paginationConfig.total ?? dataSource?.length ?? 0))
|
||||
const maxPage = Math.max(1, Math.ceil(total / Math.max(1, localPagination.pageSize)))
|
||||
if (localPagination.current > maxPage) {
|
||||
setLocalPagination((current) => ({ ...current, current: maxPage }))
|
||||
}
|
||||
}, [
|
||||
dataSource?.length,
|
||||
localPagination.current,
|
||||
localPagination.pageSize,
|
||||
paginationConfig,
|
||||
paginationIsExternallyControlled,
|
||||
])
|
||||
|
||||
const resolvedPagination: TablePaginationConfig | false | undefined = paginationConfig
|
||||
? paginationIsExternallyControlled
|
||||
? paginationConfig
|
||||
: {
|
||||
...paginationConfig,
|
||||
current: localPagination.current,
|
||||
pageSize: localPagination.pageSize,
|
||||
onChange: (nextPage, nextPageSize) => {
|
||||
const normalizedPageSize = Math.max(1, Number(nextPageSize) || localPagination.pageSize)
|
||||
setLocalPagination({
|
||||
current: normalizedPageSize === localPagination.pageSize ? nextPage : 1,
|
||||
pageSize: normalizedPageSize,
|
||||
})
|
||||
paginationConfig.onChange?.(nextPage, nextPageSize)
|
||||
},
|
||||
}
|
||||
: pagination
|
||||
const reservedPercent =
|
||||
(props.rowSelection ? 4 : 0) +
|
||||
(props.expandable && typeof props.expandable === 'object' ? 4 : 0)
|
||||
@@ -24,7 +74,9 @@ export default function PercentTable<RecordType extends object = Record<string,
|
||||
return (
|
||||
<AntTable<RecordType>
|
||||
{...rest}
|
||||
dataSource={dataSource}
|
||||
columns={normalizedColumns}
|
||||
pagination={resolvedPagination}
|
||||
tableLayout="fixed"
|
||||
scroll={{ ...scrollConfig, x: '100%' }}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user