/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { EuiSpacer, EuiSwitch } from "@elastic/eui"; import React, { forwardRef, useRef, useState, useImperativeHandle } from "react"; import JSONEditor, { IJSONEditorRef } from "../JSONEditor"; import MonacoJSONEditor, { MonacoJSONEditorProps } from "../MonacoJSONEditor"; import JSONDiffEditor, { JSONDiffEditorProps } from "../JSONDiffEditor"; import "./SwitchableEditor.scss"; export interface SwitchableEditorProps extends JSONDiffEditorProps, Pick { mode: "json" | "diff"; } export interface ISwitchableEditorRef extends IJSONEditorRef {} const SwitchableEditor = forwardRef( ({ mode, diagnosticsOptions, path, ...others }: SwitchableEditorProps, ref: React.Ref) => { const [checked, setChecked] = useState(false); const editorRef = useRef(null); useImperativeHandle(ref, () => ({ getValue: () => editorRef.current?.getValue() || "", setValue: (...args) => editorRef.current?.setValue(...args), validate: () => editorRef.current?.validate() || Promise.resolve("Not ready"), })); return ( <> {mode === "diff" ? ( <> { const targetChecked = e.target.checked; const validateResult = await editorRef.current?.validate(); if (!validateResult) { setChecked(targetChecked); } }} /> ) : null} {checked ? ( diagnosticsOptions ? ( ) : ( ) ) : ( )} ); } ); export default SwitchableEditor;