ConceptPortal-public/rsconcept/frontend/src/components/ui/DataTable/PaginationTools.tsx

110 lines
3.4 KiB
TypeScript
Raw Normal View History

'use client';
'use no memo';
2023-09-10 20:17:18 +03:00
import { Table } from '@tanstack/react-table';
import clsx from 'clsx';
2023-09-10 20:17:18 +03:00
import { useCallback } from 'react';
2024-05-02 19:13:54 +03:00
import { IconPageFirst, IconPageLast, IconPageLeft, IconPageRight } from '@/components/Icons';
import { prefixes } from '@/utils/constants';
2023-09-10 20:17:18 +03:00
interface PaginationToolsProps<TData> {
id?: string;
2023-12-28 14:04:44 +03:00
table: Table<TData>;
paginationOptions: number[];
onChangePaginationOption?: (newValue: number) => void;
2023-09-10 20:17:18 +03:00
}
function PaginationTools<TData>({
id,
table,
paginationOptions,
onChangePaginationOption
}: PaginationToolsProps<TData>) {
2023-09-10 20:17:18 +03:00
const handlePaginationOptionsChange = useCallback(
2023-12-28 14:04:44 +03:00
(event: React.ChangeEvent<HTMLSelectElement>) => {
const perPage = Number(event.target.value);
table.setPageSize(perPage);
if (onChangePaginationOption) {
onChangePaginationOption(perPage);
}
},
[onChangePaginationOption, table]
);
2023-09-10 20:17:18 +03:00
return (
2023-12-28 14:04:44 +03:00
<div className={clsx('flex justify-end items-center', 'my-2', 'text-sm', 'clr-text-controls', 'select-none')}>
<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'
className='clr-hover clr-text-controls'
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'
className='clr-hover clr-text-controls'
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
id={id ? `${id}__page` : undefined}
2023-12-28 14:04:44 +03:00
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'
className='clr-hover clr-text-controls'
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'
className='clr-hover clr-text-controls'
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>
<select
id={id ? `${id}__per_page` : undefined}
2023-12-28 14:04:44 +03:00
value={table.getState().pagination.pageSize}
onChange={handlePaginationOptionsChange}
className='mx-2 cursor-pointer clr-app'
>
2023-12-28 14:04:44 +03:00
{paginationOptions.map(pageSize => (
<option key={`${prefixes.page_size}${pageSize}`} value={pageSize}>
{pageSize} на стр
</option>
))}
</select>
</div>
2023-12-28 14:04:44 +03:00
);
2023-09-10 20:17:18 +03:00
}
2023-12-28 14:04:44 +03:00
export default PaginationTools;