2023-12-13 14:32:57 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2023-09-10 20:17:18 +03:00
|
|
|
|
import { Table } from '@tanstack/react-table';
|
2023-12-15 17:34:50 +03:00
|
|
|
|
import clsx from 'clsx';
|
2023-09-10 20:17:18 +03:00
|
|
|
|
import { useCallback } from 'react';
|
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import { prefixes } from '@/utils/constants';
|
|
|
|
|
|
2023-09-10 20:17:18 +03:00
|
|
|
|
import { GotoFirstIcon, GotoLastIcon, GotoNextIcon, GotoPrevIcon } from '../Icons';
|
|
|
|
|
|
|
|
|
|
interface PaginationToolsProps<TData> {
|
|
|
|
|
table: Table<TData>
|
|
|
|
|
paginationOptions: number[]
|
|
|
|
|
onChangePaginationOption?: (newValue: number) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function PaginationTools<TData>({ table, paginationOptions, onChangePaginationOption }: PaginationToolsProps<TData>) {
|
|
|
|
|
const handlePaginationOptionsChange = useCallback(
|
|
|
|
|
(event: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
|
|
const perPage = Number(event.target.value);
|
|
|
|
|
table.setPageSize(perPage);
|
|
|
|
|
if (onChangePaginationOption) {
|
|
|
|
|
onChangePaginationOption(perPage);
|
|
|
|
|
}
|
|
|
|
|
}, [onChangePaginationOption, table]);
|
|
|
|
|
|
|
|
|
|
return (
|
2023-12-15 17:34:50 +03:00
|
|
|
|
<div className={clsx(
|
|
|
|
|
'flex justify-end items-center',
|
|
|
|
|
'w-full my-2',
|
|
|
|
|
'text-sm',
|
|
|
|
|
'clr-text-controls',
|
|
|
|
|
'select-none'
|
|
|
|
|
)}>
|
2023-12-05 01:22:44 +03:00
|
|
|
|
<div className='flex items-center gap-1 mr-3'>
|
|
|
|
|
<div className=''>
|
|
|
|
|
{table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1}
|
|
|
|
|
-
|
|
|
|
|
{Math.min(table.getFilteredRowModel().rows.length, (table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize)}
|
|
|
|
|
</div>
|
|
|
|
|
<span>из</span>
|
|
|
|
|
<div className=''>{table.getFilteredRowModel().rows.length}</div>
|
2023-09-10 20:17:18 +03:00
|
|
|
|
</div>
|
2023-12-05 01:22:44 +03:00
|
|
|
|
<div className='flex'>
|
|
|
|
|
<button type='button'
|
2023-12-15 17:34:50 +03:00
|
|
|
|
className='clr-hover clr-text-controls'
|
2023-12-05 01:22:44 +03:00
|
|
|
|
onClick={() => table.setPageIndex(0)}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
|
|
|
|
<GotoFirstIcon />
|
|
|
|
|
</button>
|
|
|
|
|
<button type='button'
|
2023-12-15 17:34:50 +03:00
|
|
|
|
className='clr-hover clr-text-controls'
|
2023-12-05 01:22:44 +03:00
|
|
|
|
onClick={() => table.previousPage()}
|
|
|
|
|
disabled={!table.getCanPreviousPage()}
|
|
|
|
|
>
|
|
|
|
|
<GotoPrevIcon />
|
|
|
|
|
</button>
|
|
|
|
|
<input
|
|
|
|
|
title='Номер страницы. Выделите для ручного ввода'
|
|
|
|
|
className='w-6 text-center clr-app'
|
|
|
|
|
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'
|
2023-12-15 17:34:50 +03:00
|
|
|
|
className='clr-hover clr-text-controls'
|
2023-12-05 01:22:44 +03:00
|
|
|
|
onClick={() => table.nextPage()}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
|
|
|
|
<GotoNextIcon />
|
|
|
|
|
</button>
|
|
|
|
|
<button type='button'
|
2023-12-15 17:34:50 +03:00
|
|
|
|
className='clr-hover clr-text-controls'
|
2023-12-05 01:22:44 +03:00
|
|
|
|
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
|
|
|
|
|
disabled={!table.getCanNextPage()}
|
|
|
|
|
>
|
|
|
|
|
<GotoLastIcon />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<select
|
|
|
|
|
value={table.getState().pagination.pageSize}
|
|
|
|
|
onChange={handlePaginationOptionsChange}
|
|
|
|
|
className='mx-2 cursor-pointer clr-app'
|
2023-09-10 20:17:18 +03:00
|
|
|
|
>
|
2023-12-05 01:22:44 +03:00
|
|
|
|
{paginationOptions.map(
|
|
|
|
|
(pageSize) => (
|
|
|
|
|
<option key={`${prefixes.page_size}${pageSize}`} value={pageSize}>
|
|
|
|
|
{pageSize} на стр
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
2023-09-10 20:17:18 +03:00
|
|
|
|
</div>);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default PaginationTools;
|