feat(ui): add jump to option for gallery pagination

This commit is contained in:
Mary Hipp 2024-07-24 11:20:59 -04:00 committed by psychedelicious
parent 3657285b1b
commit 2a9fdc6314

View File

@ -1,7 +1,7 @@
import { Button, Flex, IconButton, Spacer } from '@invoke-ai/ui-library'; import { Button, CompositeNumberInput, Flex, Icon, IconButton, Spacer } from '@invoke-ai/ui-library';
import { ELLIPSIS, useGalleryPagination } from 'features/gallery/hooks/useGalleryPagination'; import { ELLIPSIS, useGalleryPagination } from 'features/gallery/hooks/useGalleryPagination';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { PiCaretLeftBold, PiCaretRightBold } from 'react-icons/pi'; import { PiCaretLeftBold, PiCaretRightBold, PiDotsThreeBold } from 'react-icons/pi';
export const GalleryPagination = () => { export const GalleryPagination = () => {
const { goPrev, goNext, isPrevEnabled, isNextEnabled, pageButtons, goToPage, currentPage, total } = const { goPrev, goNext, isPrevEnabled, isNextEnabled, pageButtons, goToPage, currentPage, total } =
@ -15,48 +15,63 @@ export const GalleryPagination = () => {
goNext(); goNext();
}, [goNext]); }, [goNext]);
const onChangeJumpTo = useCallback(
(v: number) => {
goToPage(v - 1);
},
[goToPage]
);
if (!total) { if (!total) {
return null; return null;
} }
return ( return (
<Flex gap={2} alignItems="center" w="full"> <Flex justifyContent="space-between" alignItems="center" w="full">
<IconButton <Spacer w="auto" />
size="sm" <Flex flexGrow="1" justifyContent="center">
aria-label="prev" <Flex gap={1} alignItems="center">
icon={<PiCaretLeftBold />} <IconButton
onClick={onClickPrev} size="xs"
isDisabled={!isPrevEnabled} aria-label="prev"
variant="ghost" icon={<PiCaretLeftBold />}
/> onClick={onClickPrev}
<Spacer /> isDisabled={!isPrevEnabled}
{pageButtons.map((page, i) => { variant="ghost"
if (page === ELLIPSIS) { />
return ( {pageButtons.map((page, i) => {
<Button size="sm" key={`ellipsis_${i}`} variant="link" isDisabled> if (page === ELLIPSIS) {
... return <Icon as={PiDotsThreeBold} boxSize="4" key={`ellipsis-${i}`} />;
</Button> }
); return (
} <Button
return ( size="xs"
<Button key={page}
size="sm" onClick={goToPage.bind(null, page - 1)}
key={page} variant={currentPage === page - 1 ? 'solid' : 'outline'}
onClick={goToPage.bind(null, page - 1)} >
variant={currentPage === page - 1 ? 'solid' : 'outline'} {page}
> </Button>
{page} );
</Button> })}
); <IconButton
})} size="xs"
<Spacer /> aria-label="next"
<IconButton icon={<PiCaretRightBold />}
size="sm" onClick={onClickNext}
aria-label="next" isDisabled={!isNextEnabled}
icon={<PiCaretRightBold />} variant="ghost"
onClick={onClickNext} />
isDisabled={!isNextEnabled} </Flex>
variant="ghost" </Flex>
<CompositeNumberInput
size="xs"
maxW="60px"
value={currentPage + 1}
min={1}
max={total}
step={1}
onChange={onChangeJumpTo}
/> />
</Flex> </Flex>
); );