2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
2024-12-13 13:55:11 +03:00
|
|
|
|
'use no memo';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-02-12 21:36:03 +03:00
|
|
|
|
import { useCallback } from 'react';
|
2025-02-20 20:22:05 +03:00
|
|
|
|
import { type Table } from '@tanstack/react-table';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
|
2025-03-12 11:54:32 +03:00
|
|
|
|
import { IconPageFirst, IconPageLast, IconPageLeft, IconPageRight } from '../icons1';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
|
2024-06-07 20:17:03 +03:00
|
|
|
|
interface PaginationToolsProps<TData> {
|
|
|
|
|
id?: string;
|
|
|
|
|
table: Table<TData>;
|
|
|
|
|
paginationOptions: number[];
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 00:18:34 +03:00
|
|
|
|
export function PaginationTools<TData>({ id, table, paginationOptions }: PaginationToolsProps<TData>) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
const handlePaginationOptionsChange = useCallback(
|
|
|
|
|
(event: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
|
|
const perPage = Number(event.target.value);
|
|
|
|
|
table.setPageSize(perPage);
|
|
|
|
|
},
|
2025-03-12 00:18:34 +03:00
|
|
|
|
[table]
|
2024-06-07 20:17:03 +03:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
2025-03-10 16:01:40 +03:00
|
|
|
|
<div className='flex justify-end items-center my-2 text-sm clr-text-controls select-none'>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
<span className='mr-3'>
|
|
|
|
|
{`${table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1}
|
|
|
|
|
-
|
|
|
|
|
${Math.min(
|
|
|
|
|
table.getFilteredRowModel().rows.length,
|
|
|
|
|
(table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize
|
|
|
|
|
)}
|
|
|
|
|
из
|
|
|
|
|
${table.getFilteredRowModel().rows.length}`}
|
|
|
|
|
</span>
|
|
|
|
|
<div className='flex'>
|
|
|
|
|
<button
|
|
|
|
|
type='button'
|
2024-12-17 11:37:42 +03:00
|
|
|
|
className='clr-hover clr-text-controls cc-animate-color'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
onClick={() => table.setPageIndex(0)}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
|
|
|
|
<IconPageFirst size='1.5rem' />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type='button'
|
2024-12-17 11:37:42 +03:00
|
|
|
|
className='clr-hover clr-text-controls cc-animate-color'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
onClick={() => table.previousPage()}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
|
|
|
|
<IconPageLeft size='1.5rem' />
|
|
|
|
|
</button>
|
|
|
|
|
<input
|
|
|
|
|
id={id ? `${id}__page` : undefined}
|
|
|
|
|
title='Номер страницы. Выделите для ручного ввода'
|
2024-12-17 10:52:36 +03:00
|
|
|
|
className='w-6 text-center bg-prim-100'
|
2024-06-07 20:17:03 +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'
|
2024-12-17 11:37:42 +03:00
|
|
|
|
className='clr-hover clr-text-controls cc-animate-color'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
onClick={() => table.nextPage()}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
|
|
|
|
<IconPageRight size='1.5rem' />
|
|
|
|
|
</button>
|
|
|
|
|
<button
|
|
|
|
|
type='button'
|
2024-12-17 11:37:42 +03:00
|
|
|
|
className='clr-hover clr-text-controls cc-animate-color'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
|
|
|
|
<IconPageLast size='1.5rem' />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<select
|
|
|
|
|
id={id ? `${id}__per_page` : undefined}
|
|
|
|
|
value={table.getState().pagination.pageSize}
|
|
|
|
|
onChange={handlePaginationOptionsChange}
|
2024-12-17 10:52:36 +03:00
|
|
|
|
className='mx-2 cursor-pointer bg-prim-100'
|
2024-06-07 20:17:03 +03:00
|
|
|
|
>
|
|
|
|
|
{paginationOptions.map(pageSize => (
|
|
|
|
|
<option key={`${prefixes.page_size}${pageSize}`} value={pageSize}>
|
|
|
|
|
{pageSize} на стр
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|