2024-12-13 13:55:26 +03:00
|
|
|
'use no memo';
|
2025-03-12 21:07:42 +03:00
|
|
|
'use client';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
2025-02-21 21:15:05 +03:00
|
|
|
import { type Table } from '@tanstack/react-table';
|
2023-09-10 20:17:18 +03:00
|
|
|
|
2025-03-12 12:04:50 +03:00
|
|
|
import { IconPageFirst, IconPageLast, IconPageLeft, IconPageRight } from '../icons';
|
2025-02-10 01:32:55 +03:00
|
|
|
|
2025-04-16 15:22:31 +03:00
|
|
|
import { SelectPagination } from './select-pagination';
|
|
|
|
|
2023-09-10 20:17:18 +03:00
|
|
|
interface PaginationToolsProps<TData> {
|
2024-03-18 16:21:39 +03:00
|
|
|
id?: string;
|
2023-12-28 14:04:44 +03:00
|
|
|
table: Table<TData>;
|
2025-04-30 01:10:45 +03:00
|
|
|
paginationOptions: readonly number[];
|
2025-03-20 19:23:23 +03:00
|
|
|
onChangePaginationOption?: (newValue: number) => void;
|
2023-09-10 20:17:18 +03:00
|
|
|
}
|
|
|
|
|
2025-03-20 19:23:23 +03:00
|
|
|
export function PaginationTools<TData>({
|
|
|
|
id,
|
|
|
|
table,
|
|
|
|
onChangePaginationOption,
|
|
|
|
paginationOptions
|
|
|
|
}: PaginationToolsProps<TData>) {
|
2023-09-10 20:17:18 +03:00
|
|
|
return (
|
2025-04-12 21:44:13 +03:00
|
|
|
<div className='flex justify-end items-center my-2 text-sm cc-controls select-none'>
|
2023-12-28 14:04:44 +03:00
|
|
|
<span className='mr-3'>
|
|
|
|
{`${table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1}
|
2023-12-18 20:45:33 +03:00
|
|
|
-
|
2023-12-28 14:04:44 +03:00
|
|
|
${Math.min(
|
|
|
|
table.getFilteredRowModel().rows.length,
|
|
|
|
(table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize
|
|
|
|
)}
|
2023-12-18 20:45:33 +03:00
|
|
|
из
|
|
|
|
${table.getFilteredRowModel().rows.length}`}
|
2023-12-28 14:04:44 +03:00
|
|
|
</span>
|
|
|
|
<div className='flex'>
|
|
|
|
<button
|
|
|
|
type='button'
|
2025-03-20 18:24:40 +03:00
|
|
|
aria-label='Первая страница'
|
2025-06-17 22:28:56 +03:00
|
|
|
className='cc-hover-bg cc-controls cc-animate-color focus-outline'
|
2023-12-28 14:04:44 +03:00
|
|
|
onClick={() => table.setPageIndex(0)}
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
>
|
2024-05-02 19:13:54 +03:00
|
|
|
<IconPageFirst size='1.5rem' />
|
2023-12-28 14:04:44 +03:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type='button'
|
2025-03-20 18:24:40 +03:00
|
|
|
aria-label='Предыдущая страница'
|
2025-06-17 22:28:56 +03:00
|
|
|
className='cc-hover-bg cc-controls cc-animate-color focus-outline'
|
2023-12-28 14:04:44 +03:00
|
|
|
onClick={() => table.previousPage()}
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
>
|
2024-05-02 19:13:54 +03:00
|
|
|
<IconPageLeft size='1.5rem' />
|
2023-12-28 14:04:44 +03:00
|
|
|
</button>
|
|
|
|
<input
|
2024-03-18 16:21:39 +03:00
|
|
|
id={id ? `${id}__page` : undefined}
|
2023-12-28 14:04:44 +03:00
|
|
|
title='Номер страницы. Выделите для ручного ввода'
|
2025-03-20 18:24:40 +03:00
|
|
|
aria-label='Номер страницы'
|
2025-06-17 22:28:56 +03:00
|
|
|
className='w-6 text-center bg-transparent focus-outline rounded-md'
|
2023-12-28 14:04:44 +03:00
|
|
|
value={table.getState().pagination.pageIndex + 1}
|
|
|
|
onChange={event => {
|
|
|
|
const page = event.target.value ? Number(event.target.value) - 1 : 0;
|
|
|
|
if (page + 1 <= table.getPageCount()) {
|
|
|
|
table.setPageIndex(page);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<button
|
|
|
|
type='button'
|
2025-03-20 18:24:40 +03:00
|
|
|
aria-label='Следующая страница'
|
2025-06-17 22:28:56 +03:00
|
|
|
className='cc-hover-bg cc-controls cc-animate-color focus-outline'
|
2023-12-28 14:04:44 +03:00
|
|
|
onClick={() => table.nextPage()}
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
>
|
2024-05-02 19:13:54 +03:00
|
|
|
<IconPageRight size='1.5rem' />
|
2023-12-28 14:04:44 +03:00
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type='button'
|
2025-03-20 18:24:40 +03:00
|
|
|
aria-label='Последняя страница'
|
2025-06-17 22:28:56 +03:00
|
|
|
className='cc-hover-bg cc-controls cc-animate-color focus-outline'
|
2023-12-28 14:04:44 +03:00
|
|
|
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
>
|
2024-05-02 19:13:54 +03:00
|
|
|
<IconPageLast size='1.5rem' />
|
2023-12-28 14:04:44 +03:00
|
|
|
</button>
|
|
|
|
</div>
|
2025-04-16 15:22:31 +03:00
|
|
|
<SelectPagination
|
2024-03-18 16:21:39 +03:00
|
|
|
id={id ? `${id}__per_page` : undefined}
|
2025-04-16 15:22:31 +03:00
|
|
|
table={table}
|
|
|
|
paginationOptions={paginationOptions}
|
|
|
|
onChange={onChangePaginationOption}
|
|
|
|
/>
|
2023-12-05 01:22:44 +03:00
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
);
|
2023-09-10 20:17:18 +03:00
|
|
|
}
|