2023-11-08 19:28:22 -06:00
|
|
|
import { CaretLeftIcon, CaretRightIcon } from '@radix-ui/react-icons';
|
|
|
|
|
import { Button, Flex, TextField } from '@radix-ui/themes';
|
|
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
|
|
|
|
|
interface PageTurnerProps {
|
|
|
|
|
page: number;
|
|
|
|
|
pages: number;
|
2023-11-13 21:41:37 -06:00
|
|
|
onPageChange: (page: number, isButtonClick: boolean) => void;
|
2023-11-08 19:28:22 -06:00
|
|
|
}
|
|
|
|
|
|
2023-11-13 21:31:36 -06:00
|
|
|
export function PageTurner({ page, onPageChange, pages }: PageTurnerProps) {
|
2023-11-08 19:28:22 -06:00
|
|
|
const pageInput = useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (pageInput.current) pageInput.current.value = `${page + 1}`;
|
|
|
|
|
}, [page]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex justify="center" wrap="wrap" gap="2">
|
|
|
|
|
<Flex gap="2">
|
|
|
|
|
<Button
|
|
|
|
|
variant="soft"
|
2023-11-13 21:41:37 -06:00
|
|
|
onClick={() => onPageChange(Math.max(page - 1, 0), true)}
|
2023-11-08 19:28:22 -06:00
|
|
|
disabled={page === 0}
|
|
|
|
|
>
|
|
|
|
|
<CaretLeftIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
<TextField.Root>
|
|
|
|
|
<TextField.Slot>Page</TextField.Slot>
|
|
|
|
|
<TextField.Input
|
|
|
|
|
type="number"
|
|
|
|
|
ref={pageInput}
|
|
|
|
|
style={{ width: 64, textAlign: 'center' }}
|
|
|
|
|
onBlur={(event) => {
|
2023-11-13 21:31:36 -06:00
|
|
|
onPageChange(
|
2023-11-08 19:28:22 -06:00
|
|
|
Math.max(
|
|
|
|
|
0,
|
|
|
|
|
Math.min(pages - 1, event.target.valueAsNumber - 1),
|
|
|
|
|
),
|
2023-11-13 21:41:37 -06:00
|
|
|
false,
|
2023-11-08 19:28:22 -06:00
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
onKeyDown={(event) => {
|
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
|
(event.target as HTMLInputElement).blur();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<TextField.Slot>out of {pages}</TextField.Slot>
|
|
|
|
|
</TextField.Root>
|
|
|
|
|
<Button
|
|
|
|
|
variant="soft"
|
|
|
|
|
onClick={() => {
|
2023-11-13 21:41:37 -06:00
|
|
|
onPageChange(Math.min(page + 1, pages - 1), true);
|
2023-11-08 19:28:22 -06:00
|
|
|
}}
|
|
|
|
|
disabled={page === pages - 1}
|
|
|
|
|
>
|
|
|
|
|
<CaretRightIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</Flex>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|