-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from rebeccaalpert/preview-attachment
feat(PreviewAttachment): add Preview Attachment component
- Loading branch information
Showing
11 changed files
with
291 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
22 changes: 22 additions & 0 deletions
22
...nt/extensions/virtual-assistant/examples/PreviewAttachment/PreviewAttachment.md
This file contains 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 @@ | ||
--- | ||
# Sidenav top-level section | ||
# should be the same for all markdown files | ||
section: extensions | ||
subsection: Chat bots / AI | ||
# Sidenav secondary level section | ||
# should be the same for all markdown files | ||
id: Preview attachment | ||
# Tab (react | react-demos | html | html-demos | design-guidelines | accessibility) | ||
source: react | ||
# If you use typescript, the name of the interface to display props for | ||
# These are found through the sourceProps function provided in patternfly-docs.source.js | ||
propComponents: ['PreviewAttachment'] | ||
--- | ||
|
||
import PreviewAttachment from '@patternfly/virtual-assistant/dist/dynamic/PreviewAttachment'; | ||
|
||
### Basic example | ||
|
||
```js file="./PreviewAttachment.tsx" | ||
|
||
``` |
25 changes: 25 additions & 0 deletions
25
...ocs/content/extensions/virtual-assistant/examples/PreviewAttachment/PreviewAttachment.tsx
This file contains 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,25 @@ | ||
import React from 'react'; | ||
import { Button } from '@patternfly/react-core'; | ||
import { PreviewAttachment } from '@patternfly/virtual-assistant/dist/dynamic/PreviewAttachment'; | ||
|
||
export const BasicDemo: React.FunctionComponent = () => { | ||
const [isModalOpen, setIsModalOpen] = React.useState(false); | ||
|
||
const handleModalToggle = (_event: React.MouseEvent | MouseEvent | KeyboardEvent) => { | ||
setIsModalOpen(!isModalOpen); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Button onClick={handleModalToggle}>Launch modal</Button> | ||
<PreviewAttachment | ||
code="I am a code snippet" | ||
fileName="test.yaml" | ||
handleModalToggle={handleModalToggle} | ||
isModalOpen={isModalOpen} | ||
onDismiss={() => null} | ||
onEdit={() => null} | ||
/> | ||
</> | ||
); | ||
}; |
This file contains 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
4 changes: 2 additions & 2 deletions
4
...le/src/AttachmentEdit/AttachmentEdit.scss → packages/module/src/CodeModal/CodeModal.scss
This file contains 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 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,150 @@ | ||
// ============================================================================ | ||
// Code Modal - Chatbot Modal with Code Editor | ||
// ============================================================================ | ||
import React, { useState } from 'react'; | ||
import path from 'path'; | ||
|
||
// Import PatternFly components | ||
import { CodeEditor, Language } from '@patternfly/react-code-editor'; | ||
import { | ||
Button, | ||
Flex, | ||
Icon, | ||
Modal, | ||
ModalBody, | ||
ModalFooter, | ||
ModalHeader, | ||
Stack, | ||
StackItem | ||
} from '@patternfly/react-core'; | ||
import { CodeIcon } from '@patternfly/react-icons'; | ||
|
||
export interface CodeModalProps { | ||
/** Text shown in code editor */ | ||
code: string; | ||
/** Filename, including extension, of file shown in editor */ | ||
fileName: string; | ||
/** Whether copying code is allowed */ | ||
isCopyEnabled?: boolean; | ||
/** Whether code is read-only */ | ||
isReadOnly?: boolean; | ||
/** Action assigned to primary modal button */ | ||
onPrimaryAction: (event: React.MouseEvent | MouseEvent | KeyboardEvent, code?: string) => void; | ||
/** Action assigned to secondary modal button */ | ||
onSecondaryAction: (event: React.MouseEvent | MouseEvent | KeyboardEvent) => void; | ||
/** Name of primary modal button */ | ||
primaryActionBtn: string; | ||
/** Name of secondary modal button */ | ||
secondaryActionBtn: string; | ||
/** Function that handles modal toggle */ | ||
handleModalToggle: (event: React.MouseEvent | MouseEvent | KeyboardEvent) => void; | ||
/** Whether modal is open */ | ||
isModalOpen: boolean; | ||
/** Title of modal */ | ||
title: string; | ||
} | ||
|
||
export const CodeModal: React.FunctionComponent<CodeModalProps> = ({ | ||
fileName, | ||
code, | ||
handleModalToggle, | ||
isCopyEnabled, | ||
isModalOpen, | ||
isReadOnly, | ||
onPrimaryAction, | ||
onSecondaryAction, | ||
primaryActionBtn, | ||
secondaryActionBtn, | ||
title, | ||
...props | ||
}: CodeModalProps) => { | ||
const [newCode, setNewCode] = useState(code); | ||
|
||
const handlePrimaryAction = (_event: React.MouseEvent | MouseEvent | KeyboardEvent) => { | ||
handleModalToggle(_event); | ||
if (!isReadOnly) { | ||
onPrimaryAction(_event, newCode); | ||
} else { | ||
onPrimaryAction(_event); | ||
} | ||
}; | ||
|
||
const handleSecondaryAction = (_event: React.MouseEvent | MouseEvent | KeyboardEvent) => { | ||
handleModalToggle(_event); | ||
onSecondaryAction(_event); | ||
}; | ||
|
||
const onEditorDidMount = (editor, monaco) => { | ||
editor.layout(); | ||
editor.focus(); | ||
monaco.editor.getModels()[0].updateOptions({ tabSize: 5 }); | ||
}; | ||
|
||
const onCodeChange = (value: string) => { | ||
if (!isReadOnly) { | ||
setNewCode(value); | ||
} | ||
}; | ||
|
||
return ( | ||
<Modal | ||
isOpen={isModalOpen} | ||
onClose={handleModalToggle} | ||
ouiaId="CodeModal" | ||
aria-labelledby="code-modal-title" | ||
aria-describedby="code-modal" | ||
width="25%" | ||
> | ||
<ModalHeader title={title} labelId="code-modal-title" /> | ||
<ModalBody id="code-modal-body"> | ||
<Stack hasGutter> | ||
<StackItem> | ||
<Flex> | ||
<Flex | ||
className="pf-chatbot__code-icon" | ||
justifyContent={{ default: 'justifyContentCenter' }} | ||
alignItems={{ default: 'alignItemsCenter' }} | ||
alignSelf={{ default: 'alignSelfCenter' }} | ||
> | ||
<Icon> | ||
<CodeIcon color="white" /> | ||
</Icon> | ||
</Flex> | ||
<Stack> | ||
<StackItem>{path.parse(fileName).name}</StackItem> | ||
<StackItem className="pf-chatbot__code-language"> | ||
{Language[path.extname(fileName).slice(1)].toUpperCase()} | ||
</StackItem> | ||
</Stack> | ||
</Flex> | ||
</StackItem> | ||
<StackItem> | ||
<CodeEditor | ||
isDarkTheme | ||
isLineNumbersVisible | ||
isLanguageLabelVisible | ||
isCopyEnabled={isCopyEnabled} | ||
isReadOnly={isReadOnly} | ||
code={newCode} | ||
language={Language[path.extname(fileName).slice(1)]} | ||
onEditorDidMount={onEditorDidMount} | ||
height="400px" | ||
onCodeChange={onCodeChange} | ||
{...props} | ||
/> | ||
</StackItem> | ||
</Stack> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button isBlock key="code-modal-primary" variant="primary" onClick={handlePrimaryAction} form="code-modal-form"> | ||
{primaryActionBtn} | ||
</Button> | ||
<Button isBlock key="code-modal-secondary" variant="secondary" onClick={handleSecondaryAction}> | ||
{secondaryActionBtn} | ||
</Button> | ||
</ModalFooter> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default CodeModal; |
This file contains 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 './CodeModal'; | ||
|
||
export * from './CodeModal'; |
Oops, something went wrong.