Skip to content

Commit 9198e91

Browse files
committed
YAML Editor with ref
1 parent 3903dde commit 9198e91

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

frontend/packages/console-dynamic-plugin-sdk/docs/api.md

+1
Original file line numberDiff line numberDiff line change
@@ -1903,6 +1903,7 @@ A basic lazy loaded YAML editor with hover help and completion.
19031903
| `toolbarLinks` | Array of ReactNode rendered on the toolbar links section on top of the editor. |
19041904
| `onChange` | Callback for on code change event. |
19051905
| `onSave` | Callback called when the command CTRL / CMD + S is triggered. |
1906+
| `ref` | React reference to `{ editor?: IStandaloneCodeEditor }`. Using the 'editor' property, you are able to access to all methods to control the editor. For more information, visit https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html. |
19061907

19071908

19081909

frontend/packages/console-dynamic-plugin-sdk/src/api/dynamic-core-api.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
UsePrometheusPoll,
2828
TimestampProps,
2929
NamespaceBarProps,
30+
YAMLEditorRef,
3031
} from '../extensions/console-types';
3132
import { StatusPopupSectionProps, StatusPopupItemProps } from '../extensions/dashboard-types';
3233

@@ -574,8 +575,9 @@ export { useFlag } from '../utils/flags';
574575
* @param {YAMLEditorProps['toolbarLinks']} toolbarLinks - Array of ReactNode rendered on the toolbar links section on top of the editor.
575576
* @param {YAMLEditorProps['onChange']} onChange - Callback for on code change event.
576577
* @param {YAMLEditorProps['onSave']} onSave - Callback called when the command CTRL / CMD + S is triggered.
578+
* @param {YAMLEditorRef} ref - React reference to `{ editor?: IStandaloneCodeEditor }`. Using the 'editor' property, you are able to access to all methods to control the editor. For more information, visit https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html.
577579
*/
578-
export const YAMLEditor: React.FC<YAMLEditorProps> = require('@console/internal/components/AsyncYAMLEditor')
580+
export const YAMLEditor: React.ForwardRefExoticComponent<YAMLEditorProps & React.RefAttributes<YAMLEditorRef>> = require('@console/internal/components/AsyncYAMLEditor')
579581
.AsyncYAMLEditor;
580582

581583
/**

frontend/packages/console-dynamic-plugin-sdk/src/extensions/console-types.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from 'react';
22
import { ButtonProps } from '@patternfly/react-core';
33
import { ICell, OnSelect, SortByDirection, TableGridBreakpoint } from '@patternfly/react-table';
4+
import MonacoEditor from 'react-monaco-editor/lib/editor';
45
import { RouteComponentProps } from 'react-router';
56
import {
67
ExtensionK8sGroupKindModel,
@@ -623,8 +624,12 @@ export type YAMLEditorProps = {
623624
minHeight?: string | number;
624625
showShortcuts?: boolean;
625626
toolbarLinks?: React.ReactNodeArray;
626-
onChange?: (newValue, event) => {};
627-
onSave?: () => {};
627+
onChange?: (newValue, event) => void;
628+
onSave?: () => void;
629+
};
630+
631+
export type YAMLEditorRef = {
632+
editor?: MonacoEditor['editor'];
628633
};
629634

630635
export type ResourceYAMLEditorProps = {

frontend/public/components/AsyncYAMLEditor.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import * as React from 'react';
2-
import { YAMLEditorProps } from '@console/dynamic-plugin-sdk';
2+
import { YAMLEditorProps, YAMLEditorRef } from '@console/dynamic-plugin-sdk';
33

4-
export const AsyncYAMLEditor: React.FC<YAMLEditorProps> = React.lazy(() =>
4+
export const AsyncYAMLEditor: React.RefForwardingComponent<
5+
React.RefAttributes<YAMLEditorRef>,
6+
YAMLEditorProps
7+
> = React.lazy(() =>
58
import('@console/shared/src/components/editor/YAMLEditor').then((m) => ({
69
default: m.default,
710
})),

0 commit comments

Comments
 (0)