forked from PaloAltoNetworks/docusaurus-openapi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
122 lines (110 loc) · 3.27 KB
/
Copy pathindex.tsx
File metadata and controls
122 lines (110 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* ============================================================================
* 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 { setStringRawBody } from "@theme/ApiExplorer/Body/slice";
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 (
<div
onClick={() => setEditorDisabled(false)}
onBlur={() => setEditorDisabled(true)}
>
<LiveEditor
key={String(isBrowser)}
className={clsx({
"openapi-explorer__playground-editor": true,
"openapi-explorer__form-item-input": showErrors,
error: showErrors,
})}
onChange={onEdit}
disabled={editorDisabled}
tabMode="focus"
/>
</div>
);
}
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(setStringRawBody(code));
}, [action, code]);
const {
control,
formState: { errors },
} = useFormContext();
const showErrorMessage = errors?.requestBody;
const handleChange = (snippet: string, onChange: any) => {
setCode(snippet);
onChange(snippet);
};
return (
<div
className={clsx({
"openapi-explorer__playground-container": true,
})}
>
<LiveProvider
code={code}
transformCode={transformCode ?? ((code) => `${code};`)}
theme={prismTheme}
language={language}
{...props}
>
<Controller
control={control}
rules={{
required:
isRequired && !code
? translate({
id: OPENAPI_FORM.FIELD_REQUIRED,
message: "This field is required",
})
: false,
}}
name="requestBody"
render={({ field: { onChange, name } }) => (
<LiveComponent
onEdit={(e: any) => handleChange(e, onChange)}
name={name}
showErrors={showErrorMessage}
/>
)}
/>
{showErrorMessage && (
<ErrorMessage
errors={errors}
name="requestBody"
render={({ message }) => (
<div className="openapi-explorer__input-error">{message}</div>
)}
/>
)}
</LiveProvider>
</div>
);
}
const LiveApp = withLive(App);
export default LiveApp;