CodeEditor

Der CodeEditor dient dem Bearbeiten von Code, mit Syntax-Highlighting.
import { CodeEditor } from "@mittwald/flow-react-components";

<CodeEditor
  language="tsx"
  value={
    'import React, { FC } from "react";\n' +
    "\n" +
    "const ExampleCodeComponent: FC = () => {\n" +
    "  \n" +
    "  useEffect(() => {\n" +
    "    // some effect \n" +
    "  }, []);\n" +
    "  \n" +
    "  return <>Example JSX</>;\n" +
    "};"
  }
/>

Best Practices

  • Setze im language Property die Sprache passend zum Code-Inhalt. So wird das Syntax-Highlighting korrekt dargestellt.

Kombiniere mit ...

React Hook Form

Weitere Details zur Formularlogik und -validierung findest du in der Component Form (React Hook Form).


Properties

PropertyTypeDefaultDescription
autoFocusboolean-focus on the editor.
classNamestring-The elements class name.
copyablebooleantrueWhether a button to copy the code to the clipboard is shown.
defaultValuestring-The initial code of an uncontrolled editor.
editablebooleantrueThis disables editing of the editor content by the user.
extensionsExtension[]-Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information. They can either be built-in extension-providing objects, such as [state fields](https://codemirror.net/6/docs/ref/#state.StateField) or [facet providers](https://codemirror.net/6/docs/ref/#state.Facet.of), or objects with an extension in its `extension` property. Extensions can be nested in arrays arbitrarily deep—they will be flattened when processed.
heightstring-
indentWithTabbooleantrueControls whether pressing the `Tab` key inserts a tab character and indents the text (`true`) or behaves according to the browser's default behavior (`false`).
initialState{ json: any; fields?: Record<string, StateField<any>>; }-Create a state from its JSON representation serialized with [toJSON](https://codemirror.net/docs/ref/#state.EditorState.toJSON) function
isInvalidbooleanfalseWhether the editor is displayed as invalid.
isReadOnlybooleanfalseWhether the code can be read but not edited.
isRequiredbooleanfalseWhether the field must be filled in. Only marks the editor as required for assistive technology — the editor does not validate itself.
language"text" | "go" | "p" | "b" | "html" | "map" | "q" | "s" | "svg" | "m" | "markdown" | "d" | "in" | "r" | "apl" | "asc" | "asn" | "asn1" | "bash" | "bf" | "BUILD" | "bzl" | "c" | "c++" | "cc" | ... 193 more ...-The language the code is highlighted as.
maxHeightstring-
maxWidthstring-
minHeightstring-
minWidthstring-
placeholderstring | HTMLElement-Enables a placeholder—a piece of example content to show when the editor is empty.
rootShadowRoot | Document-If the view is going to be mounted in a shadow root or document other than the one held by the global variable document (the default), you should pass it here. Originally from the [config of EditorView](https://codemirror.net/6/docs/ref/#view.EditorView.constructor%5Econfig.root)
selectionEditorSelection | { anchor: number; head?: number; }-The starting selection. Defaults to a cursor at the very start of the document.
showActiveLineMarkerbooleantrueWhether the line the cursor is on is highlighted.
showCodeFoldingbooleantrueWhether the gutter offers controls to fold and unfold code blocks.
showCodeIndentationMakersbooleantrueWhether indentation levels are visualized with guide lines.
showLineNumbersbooleantrueWhether line numbers are shown in the gutter.
showLinterMarkersbooleantrueWhether linter results are shown in the gutter.
valuestring-value of the auto created model in the editor.
widthstring-
wrapWithReactElement<unknown, string | JSXElementConstructor<any>>-A React element the component is wrapped with. The element is cloned and receives the component as its only child — useful to render the component inside a link, a tooltip trigger or any other wrapper without changing the surrounding markup.

Events

PropertyTypeDefaultDescription
onChange((value: string, viewUpdate: ViewUpdate) => void)-Fired whenever a change occurs to the document.
onCreateEditor((view: EditorView, state: EditorState) => void)-The first time the editor executes the event.
onStatistics((data: Statistics) => void)-Some data on the statistics editor.
onUpdate((viewUpdate: ViewUpdate) => void)-Fired whenever any state change occurs within the editor, including non-document changes like lint results.

Accessibility

PropertyTypeDefaultDescription
aria-describedbystring-Identifies the element (or elements) that describes the object. @see aria-labelledby
aria-hiddenBooleanish-Indicates whether the element is exposed to an accessibility API. @see aria-disabled.
aria-labelstring-Defines a string value that labels the current element. @see aria-labelledby.
aria-labelledbystring-Identifies the element (or elements) that labels the current element. @see aria-describedby.

Auf dieser Seite