Skip to content
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

Draft: fix(CodeEditor): Set default height #11014

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@ export interface CodeEditorProps extends Omit<React.HTMLProps<HTMLDivElement>, '
emptyStateTitle?: React.ReactNode;
/** Editor header main content title. */
headerMainContent?: string;
/** Height of code editor. Defaults to 100%. 'sizeToFit' will automatically change the height
/** Height of code editor. 'sizeToFit' will automatically change the height
* to the height of the content.
*/
height?: string | 'sizeToFit';
/** Flag to add copy button to code editor actions. */
isCopyEnabled?: boolean;
/** Flag indicating the editor is styled using monaco's dark theme. */
isDarkTheme?: boolean;
/** Flag that enables component to consume the available height of its container */
isFullHeight?: boolean;
/** Flag indicating the editor has a plain header. */
isHeaderPlain?: boolean;
/** Flag to add download button to code editor actions. */
Expand Down Expand Up @@ -250,7 +252,6 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
onEditorDidMount: () => {},
language: Language.plaintext,
isDarkTheme: false,
height: '',
width: '',
isLineNumbersVisible: true,
isReadOnly: false,
Expand Down Expand Up @@ -521,6 +522,7 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
},
...optionsProp
};
const isFullHeight = this.props.height === '100%' ? true : this.props.isFullHeight;

return (
<Dropzone multiple={false} onDropAccepted={this.onDropAccepted} onDropRejected={this.onDropRejected}>
Expand Down Expand Up @@ -641,9 +643,15 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
);

const editor = (
<div className={css(styles.codeEditorCode)} ref={this.wrapperRef} tabIndex={0} dir="ltr">
<div
className={css(styles.codeEditorCode)}
ref={this.wrapperRef}
tabIndex={0}
dir="ltr"
style={{ height: '100%' }}
>
<Editor
height={height}
height={height === '100%' ? undefined : height}
width={width}
language={language}
value={value}
Expand All @@ -658,16 +666,28 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
);

return (
<div className={css(styles.codeEditor, isReadOnly && styles.modifiers.readOnly, className)} ref={this.ref}>
<div
className={css(styles.codeEditor, isReadOnly && styles.modifiers.readOnly, className)}
ref={this.ref}
style={
isFullHeight
? { height: '100%', display: 'flex', flexDirection: 'column' }
: { display: 'flex', flexDirection: 'column' }
}
>
{isUploadEnabled || providedEmptyState ? (
<div
{...getRootProps({
onClick: (event) => event.stopPropagation() // Prevents clicking TextArea from opening file dialog
})}
className={css(isLoading && fileUploadStyles.modifiers.loading)}
style={{ display: 'flex', flexDirection: 'column', flexGrow: '1' }}
>
{editorHeader}
<div className={css(styles.codeEditorMain, isDragActive && styles.modifiers.dragHover)}>
<div
className={css(styles.codeEditorMain, isDragActive && styles.modifiers.dragHover)}
style={{ flexGrow: 1 }}
>
<div className={css(styles.codeEditorUpload)}>
<input {...getInputProps()} /* hidden, necessary for react-dropzone */ />
{(showEmptyState || providedEmptyState) && !value ? emptyState : editor}
Expand All @@ -677,7 +697,11 @@ class CodeEditor extends React.Component<CodeEditorProps, CodeEditorState> {
) : (
<>
{editorHeader}
{showEditor && <div className={css(styles.codeEditorMain)}>{editor}</div>}
{showEditor && (
<div className={css(styles.codeEditorMain)} style={{ flexGrow: 1 }}>
{editor}
</div>
)}
</>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ These examples below are the shortcuts that we recommend describing in the popov
```ts file="CodeEditorCustomControl.tsx"

```

### With 100% height

```ts file="CodeEditorModal.tsx"

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { CodeEditor, Language } from '@patternfly/react-code-editor';
import { Button, Modal, ModalBody, ModalFooter, ModalHeader } from '@patternfly/react-core';

export const CodeEditorFullHeight: React.FunctionComponent = () => {
const [isModalOpen, setIsModalOpen] = React.useState(false);

const onEditorDidMount = (editor, monaco) => {
editor.layout();
editor.focus();
monaco.editor.getModels()[0].updateOptions({ tabSize: 5 });
};

const handleModalToggle = (_event: KeyboardEvent | React.MouseEvent) => {
setIsModalOpen(!isModalOpen);
};

const onChange = (value) => {
// eslint-disable-next-line no-console
console.log(value);
};

return (
<React.Fragment>
<Button variant="primary" onClick={handleModalToggle} ouiaId="ShowBasicModal">
Show basic modal with code editor inside
</Button>
<Modal
isOpen={isModalOpen}
onClose={handleModalToggle}
ouiaId="BasicModal"
aria-labelledby="basic-modal-title"
aria-describedby="modal-box-body-basic"
style={{ height: '800px' }}
>
<ModalHeader title="Basic modal" labelId="basic-modal-title" />
<ModalBody id="modal-box-body-basic">
<CodeEditor
isLanguageLabelVisible
code="Some example content"
onChange={onChange}
language={Language.javascript}
onEditorDidMount={onEditorDidMount}
isFullHeight
/>
</ModalBody>
<ModalFooter>
<Button key="confirm" variant="primary" onClick={handleModalToggle}>
Confirm
</Button>
<Button key="cancel" variant="link" onClick={handleModalToggle}>
Cancel
</Button>
</ModalFooter>
</Modal>
</React.Fragment>
);
};
Loading