/* ============================================================================ * Copyright (c) Palo Alto Networks * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React, { type JSX, useEffect, useState } from "react"; import { usePrismTheme } from "@docusaurus/theme-common"; import { translate } from "@docusaurus/Translate"; import useIsBrowser from "@docusaurus/useIsBrowser"; import { ErrorMessage } from "@hookform/error-message"; import { OPENAPI_FORM } from "@theme/translationIds"; import clsx from "clsx"; import { Controller, useFormContext } from "react-hook-form"; import { LiveProvider, LiveEditor, withLive } from "react-live"; function Live({ onEdit, showErrors }: any) { const isBrowser = useIsBrowser(); const [editorDisabled, setEditorDisabled] = useState(false); return (
setEditorDisabled(false)} onBlur={() => setEditorDisabled(true)} >
); } const LiveComponent = withLive(Live); function App({ children, transformCode, value, language, action, required: isRequired, ...props }: any): JSX.Element { const prismTheme = usePrismTheme(); const [code, setCode] = React.useState(children.replace(/\n$/, "")); useEffect(() => { action(code); }, [code]); const { control, formState: { errors }, } = useFormContext(); const showErrorMessage = errors?.requestBody; const handleChange = (snippet: string, onChange: any) => { setCode(snippet); onChange(snippet); }; return (
`${code};`)} theme={prismTheme} language={language} {...props} > ( handleChange(e, onChange)} name={name} showErrors={showErrorMessage} /> )} /> {showErrorMessage && ( (
{message}
)} /> )}
); } const LiveApp = withLive(App); export default LiveApp;