Files
blitzkit/src/app/tools/tankopedia/components/PageTurner.tsx
T

80 lines
2.4 KiB
TypeScript
Raw Normal View History

2023-12-04 12:39:14 -06:00
import { CaretLeftIcon, CaretRightIcon } from '@radix-ui/react-icons';
2024-05-22 13:20:56 -05:00
import { Flex, IconButton, TextField } from '@radix-ui/themes';
2023-12-04 12:39:14 -06:00
import { useEffect, useRef } from 'react';
2024-04-23 09:57:59 -05:00
import { TankDefinition } from '../../../../core/blitzkit/tankDefinitions';
2024-01-21 10:34:52 -06:00
import mutateTankopediaPersistent, {
useTankopediaPersistent,
} from '../../../../stores/tankopedia';
2023-12-04 12:39:14 -06:00
interface PageTurnerProps {
2023-12-14 22:19:01 -06:00
searchedList: TankDefinition[];
2023-12-04 12:39:14 -06:00
tanksPerPage: number;
}
2024-01-21 10:34:52 -06:00
export function PageTurner({ searchedList, tanksPerPage }: PageTurnerProps) {
const page = useTankopediaPersistent((state) => state.filters.page);
2023-12-04 12:39:14 -06:00
const pageInput = useRef<HTMLInputElement>(null);
useEffect(() => {
if (pageInput.current) pageInput.current.value = `${page + 1}`;
}, [page]);
return (
<Flex align="center" justify="center" gap="2">
2024-05-22 13:20:56 -05:00
<IconButton
2023-12-04 12:39:14 -06:00
variant="soft"
disabled={page === 0}
2024-01-21 10:34:52 -06:00
onClick={() => {
mutateTankopediaPersistent((draft) => {
draft.filters.page = Math.max(0, page - 1);
});
}}
2023-12-04 12:39:14 -06:00
>
<CaretLeftIcon />
2024-05-22 13:20:56 -05:00
</IconButton>
2024-04-01 18:36:14 -05:00
<TextField.Root
defaultValue={1}
type="number"
ref={pageInput}
min={1}
max={Math.floor(searchedList.length / tanksPerPage) + 1}
style={{ textAlign: 'center' }}
onBlur={(event) => {
mutateTankopediaPersistent((draft) => {
draft.filters.page = Math.max(
0,
Math.min(
Math.floor(searchedList.length / tanksPerPage),
event.target.valueAsNumber - 1,
),
);
});
}}
onKeyDown={(event) => {
if (event.key === 'Enter') {
(event.target as HTMLInputElement).blur();
}
}}
>
2023-12-04 12:39:14 -06:00
<TextField.Slot>Page</TextField.Slot>
<TextField.Slot>
out of {Math.floor(searchedList.length / tanksPerPage) + 1}
</TextField.Slot>
</TextField.Root>
2024-05-22 13:20:56 -05:00
<IconButton
2023-12-04 12:39:14 -06:00
variant="soft"
disabled={Math.floor(searchedList.length / tanksPerPage) === page}
2024-01-21 10:34:52 -06:00
onClick={() => {
mutateTankopediaPersistent((draft) => {
draft.filters.page = Math.min(
Math.floor(searchedList.length / tanksPerPage),
page + 1,
);
});
}}
2023-12-04 12:39:14 -06:00
>
<CaretRightIcon />
2024-05-22 13:20:56 -05:00
</IconButton>
2023-12-04 12:39:14 -06:00
</Flex>
);
}