import { Button, ButtonVariant, Icon, ModalBody, ModalBodyProps, ModalFooter, ModalHeader, ModalHeaderProps, Stack, StackItem } from '@patternfly/react-core'; import { useState, useEffect, type FunctionComponent, MouseEvent as ReactMouseEvent, KeyboardEvent as ReactKeyboardEvent } from 'react'; import { ChatbotDisplayMode } from '../Chatbot'; import ChatbotModal, { ChatbotModalProps } from '../ChatbotModal'; import FileDetailsLabel, { FileDetailsLabelProps } from '../FileDetailsLabel'; import { TrashIcon } from '@patternfly/react-icons'; export interface ImagePreviewProps extends Omit { /** Class applied to modal */ className?: string; /** Function that handles modal toggle */ handleModalToggle: (event: React.MouseEvent | MouseEvent | KeyboardEvent) => void; /** Whether modal is open */ isModalOpen: boolean; /** Title of modal */ title?: string; /** Display mode for the Chatbot parent; this influences the styles applied */ displayMode?: ChatbotDisplayMode; /** Sets modal to compact styling. */ isCompact?: boolean; /** Additional props passed to modal header */ modalHeaderProps?: ModalHeaderProps; /** Additional props passed to modal body */ modalBodyProps?: ModalBodyProps; /** Images displayed in modal */ images: { fileName: string; fileSize?: string; image: React.ReactNode }[]; /** Flag indicating if the pagination is disabled. */ isDisabled?: boolean; /** Accessible label for the pagination component. */ paginationAriaLabel?: string; /** Accessible label for the button which moves to the next page. */ toNextPageAriaLabel?: string; /** Accessible label for the button which moves to the previous page. */ toPreviousPageAriaLabel?: string; /** Function called when user clicks to navigate to next page. */ onNextClick?: (event: React.SyntheticEvent, page: number) => void; /** Function called when user clicks to navigate to previous page. */ onPreviousClick?: (event: React.SyntheticEvent, page: number) => void; /** Function called when page is changed. */ onSetPage?: (event: React.MouseEvent | React.KeyboardEvent | MouseEvent, newPage: number) => void; /** Callback function for when file details label close button is clicked */ onCloseFileDetailsLabel?: (event: React.MouseEvent, fileName: string, fileId?: string | number) => void; /** Props passed to file details label */ fileDetailsLabelProps?: Omit; /** Text shown in navigation */ paginationContent?: string; /** Navigation progress announced to assistive devices. Should state the current page/image. */ screenreaderText?: string; } const ImagePreview: FunctionComponent = ({ isModalOpen, displayMode = ChatbotDisplayMode.default, isCompact, className, handleModalToggle, title = 'Preview images', modalHeaderProps, modalBodyProps, images, isDisabled, onSetPage, onPreviousClick, toNextPageAriaLabel = 'Go to next image', toPreviousPageAriaLabel = 'Go to previous image', onNextClick, paginationAriaLabel, onCloseFileDetailsLabel, fileDetailsLabelProps, paginationContent, screenreaderText, ...props }: ImagePreviewProps) => { const [page, setPage] = useState(1); const paginationText = paginationContent || `${page}/${images.length}`; useEffect(() => { if (images.length === 0 || page > images.length) { setPage(1); } }, [images.length, page]); const handleNewPage = (_evt: ReactMouseEvent | ReactKeyboardEvent | MouseEvent, newPage: number) => { setPage(newPage); onSetPage && onSetPage(_evt, newPage); }; return ( {images.length > 0 && images[page - 1] && ( } {...fileDetailsLabelProps} />
{images[page - 1].image}
)}
{images.length > 1 && ( )}
); }; export default ImagePreview;