-
Notifications
You must be signed in to change notification settings - Fork 37
feat(FilePreview): Add file preview modal #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
packages/module/patternfly-docs/content/extensions/chatbot/examples/Messages/FilePreview.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { useState, FunctionComponent, MouseEvent as ReactMouseEvent } from 'react'; | ||
| import { Button, Checkbox } from '@patternfly/react-core'; | ||
| import FilePreview from '@patternfly/chatbot/dist/dynamic/FilePreview'; | ||
|
|
||
| export const AttachmentEditModalExample: FunctionComponent = () => { | ||
| const [isModalOpen, setIsModalOpen] = useState(false); | ||
| const [isCompact, setIsCompact] = useState(false); | ||
|
|
||
| const handleModalToggle = (_event: ReactMouseEvent | MouseEvent | KeyboardEvent) => { | ||
| setIsModalOpen(!isModalOpen); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Checkbox | ||
| label="Show compact version" | ||
| isChecked={isCompact} | ||
| onChange={() => setIsCompact(!isCompact)} | ||
| id="modal-compact-no-preview" | ||
| name="modal-compact-no-preview" | ||
| ></Checkbox> | ||
| <Button onClick={handleModalToggle}>Launch file preview modal</Button> | ||
| <FilePreview | ||
| isModalOpen={isModalOpen} | ||
| handleModalToggle={handleModalToggle} | ||
| fileName="compressed-file.zip" | ||
| isCompact={isCompact} | ||
| > | ||
| Preview unavailable | ||
| </FilePreview> | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| .pf-chatbot__file-preview-body { | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: var(--pf-t--global--spacer--md); | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .pf-chatbot__file-preview-icon { | ||
| color: var(--pf-t--global--icon--color--subtle); | ||
| width: var(--pf-t--global--icon--size--2xl); | ||
| height: var(--pf-t--global--icon--size--2xl); | ||
| } | ||
|
|
||
| .pf-chatbot__file-preview-name { | ||
| font-size: var(--pf-t--global--font--size--xl); | ||
| font-weight: var(--pf-t--global--font--weight--heading--default); | ||
| } | ||
| .pf-chatbot__file-preview-body { | ||
| color: var(--pf-t--global--text--color--subtle); | ||
| font-size: var(--pf-t--global--font--size--body--lg); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
| import FilePreview from './FilePreview'; | ||
| import { ChatbotDisplayMode } from '../Chatbot'; | ||
| import { Button, ModalBodyProps, ModalHeaderProps } from '@patternfly/react-core'; | ||
|
|
||
| describe('FilePreview', () => { | ||
| const defaultProps = { | ||
| isModalOpen: true, | ||
| handleModalToggle: jest.fn(), | ||
| fileName: 'test-file.txt', | ||
| children: 'File content preview' | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should render with basic props', () => { | ||
| render(<FilePreview {...defaultProps} />); | ||
| expect(screen.getByText('File preview')).toBeInTheDocument(); | ||
| expect(screen.getByText('test-file.txt')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render with custom title', () => { | ||
| const customTitle = 'Custom file preview title'; | ||
| render(<FilePreview {...defaultProps} title={customTitle} />); | ||
| expect(screen.getByRole('heading', { name: customTitle })).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should handle modal toggle when closed', () => { | ||
| const mockToggle = jest.fn(); | ||
| render(<FilePreview {...defaultProps} isModalOpen={false} handleModalToggle={mockToggle} />); | ||
| expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should apply default display mode class', () => { | ||
| render(<FilePreview {...defaultProps} />); | ||
| const modal = screen.getByRole('dialog'); | ||
| expect(modal).toHaveClass('pf-chatbot__file-preview-modal--default'); | ||
| }); | ||
|
|
||
| it('should apply custom display mode class', () => { | ||
| render(<FilePreview {...defaultProps} displayMode={ChatbotDisplayMode.fullscreen} />); | ||
| const modal = screen.getByRole('dialog'); | ||
| expect(modal).toHaveClass('pf-chatbot__file-preview-modal--fullscreen'); | ||
| }); | ||
|
|
||
| it('should apply compact styling when isCompact is true', () => { | ||
| render(<FilePreview {...defaultProps} isCompact />); | ||
| const modal = screen.getByRole('dialog'); | ||
| expect(modal).toHaveClass('pf-m-compact'); | ||
| }); | ||
|
|
||
| it('should not apply compact styling when isCompact is false', () => { | ||
| render(<FilePreview {...defaultProps} isCompact={false} />); | ||
| const modal = screen.getByRole('dialog'); | ||
| expect(modal).not.toHaveClass('pf-m-compact'); | ||
| }); | ||
|
|
||
| it('should apply custom className', () => { | ||
| const customClass = 'custom-file-preview'; | ||
| render(<FilePreview {...defaultProps} className={customClass} />); | ||
| const modal = screen.getByRole('dialog'); | ||
| expect(modal).toHaveClass(customClass); | ||
| }); | ||
|
|
||
| it('should pass through additional props to ChatbotModal', () => { | ||
| render(<FilePreview {...defaultProps} data-testid="file-preview-modal" />); | ||
| const modal = screen.getByTestId('file-preview-modal'); | ||
| expect(modal).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should pass modalHeaderProps to ModalHeader', () => { | ||
| const modalHeaderProps = { | ||
| 'data-testid': 'custom-header' | ||
| } as ModalHeaderProps; | ||
| render(<FilePreview {...defaultProps} modalHeaderProps={modalHeaderProps} />); | ||
| const header = screen.getByTestId('custom-header'); | ||
| expect(header).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should pass modalBodyProps to ModalBody', () => { | ||
| const modalBodyProps = { | ||
| 'data-testid': 'custom-body' | ||
| } as ModalBodyProps; | ||
| render(<FilePreview {...defaultProps} modalBodyProps={modalBodyProps} />); | ||
| const body = screen.getByTestId('custom-body'); | ||
| expect(body).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should pass ouiaId to ChatbotModal', () => { | ||
| const ouiaId = 'file-preview-ouia-id'; | ||
| render(<FilePreview {...defaultProps} ouiaId={ouiaId} />); | ||
| const modal = screen.getByRole('dialog'); | ||
| expect(modal).toHaveAttribute('data-ouia-component-id', ouiaId); | ||
| }); | ||
|
|
||
| it('should handle complex children', () => { | ||
| const complexChildren = ( | ||
| <div> | ||
| <h3>File details</h3> | ||
| <p>Size: 1.2 MB</p> | ||
| <Button>Download</Button> | ||
| </div> | ||
| ); | ||
| render(<FilePreview {...defaultProps}>{complexChildren}</FilePreview>); | ||
| expect(screen.getByRole('heading', { name: /File details/i })).toBeTruthy(); | ||
| expect(screen.getByText('Size: 1.2 MB')).toBeTruthy(); | ||
| expect(screen.getByRole('button', { name: /Download/i })).toBeTruthy(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { ModalBody, ModalBodyProps, ModalHeader, ModalHeaderProps } from '@patternfly/react-core'; | ||
| import type { FunctionComponent } from 'react'; | ||
| import { ChatbotDisplayMode } from '../Chatbot'; | ||
| import ChatbotModal, { ChatbotModalProps } from '../ChatbotModal'; | ||
| import { FileIcon } from '@patternfly/react-icons'; | ||
|
|
||
| export interface FilePreviewProps extends ChatbotModalProps { | ||
| /** 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; | ||
| /** File name */ | ||
| fileName: string; | ||
| /** Sets modal to compact styling. */ | ||
| isCompact?: boolean; | ||
| /** Additional props passed to modal header */ | ||
| modalHeaderProps?: ModalHeaderProps; | ||
| /** Additional props passed to modal body */ | ||
| modalBodyProps?: ModalBodyProps; | ||
| } | ||
|
|
||
| const FilePreview: FunctionComponent<FilePreviewProps> = ({ | ||
| isModalOpen, | ||
| displayMode = ChatbotDisplayMode.default, | ||
| children, | ||
| fileName, | ||
| isCompact, | ||
| className, | ||
| handleModalToggle, | ||
| title = 'File preview', | ||
| modalHeaderProps, | ||
| modalBodyProps, | ||
| ...props | ||
| }: FilePreviewProps) => ( | ||
| <ChatbotModal | ||
| isOpen={isModalOpen} | ||
| className={`pf-chatbot__file-preview-modal pf-chatbot__file-preview-modal--${displayMode} ${isCompact ? 'pf-m-compact' : ''} ${className ? className : ''}`} | ||
| displayMode={displayMode} | ||
| onClose={handleModalToggle} | ||
| isCompact={isCompact} | ||
| {...props} | ||
| > | ||
| <ModalHeader title={title} {...modalHeaderProps} /> | ||
| <ModalBody className="pf-chatbot__file-preview-body" {...modalBodyProps}> | ||
| <FileIcon className="pf-chatbot__file-preview-icon" /> | ||
| <h2 className="pf-chatbot__file-preview-name">{fileName}</h2> | ||
| {children && <div className="pf-chatbot__file-preview-body">{children}</div>} | ||
| </ModalBody> | ||
| </ChatbotModal> | ||
| ); | ||
|
|
||
| export default FilePreview; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { default } from './FilePreview'; | ||
|
|
||
| export * from './FilePreview'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar comment as the other PR regarding the prop names being onClose and isOpen instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skipping this only because it would cause a difference with our other modals' API. We'd have to change all of them imho.