This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug(UIKIT-1819,DataGridPagination): Исправлено исчезновение футера пр…
…и переходе между страницами (#1163)
- Loading branch information
Showing
2 changed files
with
59 additions
and
34 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
44 changes: 31 additions & 13 deletions
44
packages/components/src/DataGridPagination/useLogic/useLogic.ts
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 |
---|---|---|
@@ -1,35 +1,53 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { type SelectChangeEvent } from '@mui/material'; | ||
|
||
export const useLogic = ( | ||
totalCount: number, | ||
rowsPerPage: number, | ||
page: number, | ||
onSetCountPerPage?: (rowsPerPage: number) => void, | ||
) => { | ||
const pageCount = Math.ceil(totalCount / rowsPerPage); | ||
import { DEFAULT_ROWS_PER_PAGE } from '../constants'; | ||
import { type DataGridPaginationProps } from '../DataGridPagination'; | ||
|
||
type UseLogicParams = DataGridPaginationProps; | ||
|
||
export const useLogic = ({ | ||
totalCount, | ||
rowsPerPage = DEFAULT_ROWS_PER_PAGE, | ||
page, | ||
onSetCountPerPage, | ||
}: UseLogicParams) => { | ||
const prevTotalCountRef = useRef<number>(0); | ||
|
||
useEffect(() => { | ||
if (totalCount) { | ||
prevTotalCountRef.current = totalCount; | ||
} | ||
}, [totalCount]); | ||
|
||
const actualTotalCount = totalCount || prevTotalCountRef.current; | ||
|
||
const pageCount = Math.ceil(actualTotalCount / rowsPerPage); | ||
|
||
const rangeStart = (page - 1) * rowsPerPage + 1; | ||
|
||
const rangeEnd = () => { | ||
const end = page * rowsPerPage; | ||
|
||
return end < totalCount ? end : totalCount; | ||
return end < actualTotalCount ? end : actualTotalCount; | ||
}; | ||
|
||
const formattedRange = () => { | ||
return `${rangeStart} — ${rangeEnd()} из ${totalCount} записей`; | ||
return `${rangeStart} — ${rangeEnd()} из ${actualTotalCount} записей`; | ||
}; | ||
|
||
const isVisiblePagination = !( | ||
!onSetCountPerPage && totalCount <= rowsPerPage | ||
!onSetCountPerPage && actualTotalCount <= rowsPerPage | ||
); | ||
|
||
const handleChangeRowsPerPage = (event: SelectChangeEvent<unknown>) => { | ||
onSetCountPerPage?.(Number(event.target.value)); | ||
}; | ||
|
||
return { | ||
handleChangeRowsPerPage, | ||
formattedRange, | ||
pageCount, | ||
isVisiblePagination, | ||
pageCount, | ||
formattedRange, | ||
handleChangeRowsPerPage, | ||
}; | ||
}; |