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
154 lines (136 loc) · 4.88 KB
/
Copy pathindex.tsx
File metadata and controls
154 lines (136 loc) · 4.88 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* ============================================================================
* 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, { useState } from "react";
import { translate } from "@docusaurus/Translate";
import FormItem from "@theme/ApiExplorer/FormItem";
import ParamArrayFormItem from "@theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem";
import ParamBooleanFormItem from "@theme/ApiExplorer/ParamOptions/ParamFormItems/ParamBooleanFormItem";
import ParamMultiSelectFormItem from "@theme/ApiExplorer/ParamOptions/ParamFormItems/ParamMultiSelectFormItem";
import ParamSelectFormItem from "@theme/ApiExplorer/ParamOptions/ParamFormItems/ParamSelectFormItem";
import ParamTextFormItem from "@theme/ApiExplorer/ParamOptions/ParamFormItems/ParamTextFormItem";
import { useTypedSelector } from "@theme/ApiItem/hooks";
import { OPENAPI_PARAM_OPTIONS } from "@theme/translationIds";
import { Param } from "./slice";
export interface ParamProps {
param: Param;
}
function ParamOption({ param }: ParamProps) {
if (param.schema?.type === "array" && param.schema.items?.enum) {
return <ParamMultiSelectFormItem param={param} />;
}
if (param.schema?.type === "array") {
return <ParamArrayFormItem param={param} />;
}
if (param.schema?.enum) {
return <ParamSelectFormItem param={param} />;
}
if (param.schema?.type === "boolean") {
return <ParamBooleanFormItem param={param} />;
}
// integer, number, string, int32, int64, float, double, object, byte, binary,
// date-time, date, password
return <ParamTextFormItem param={param} />;
}
function ParamOptionWrapper({ param }: ParamProps) {
return (
<FormItem label={param.name} type={param.in} required={param.required}>
<ParamOption param={param} />
</FormItem>
);
}
function ParamOptions() {
const [showOptional, setShowOptional] = useState(false);
const pathParams = useTypedSelector((state: any) => state.params.path);
const queryParams = useTypedSelector((state: any) => state.params.query);
const cookieParams = useTypedSelector((state: any) => state.params.cookie);
const headerParams = useTypedSelector((state: any) => state.params.header);
const allParams = [
...pathParams,
...queryParams,
...cookieParams,
...headerParams,
];
const requiredParams = allParams.filter((p) => p.required);
const optionalParams = allParams.filter((p) => !p.required);
return (
<>
{/* Required Parameters */}
{requiredParams.map((param) => (
<ParamOptionWrapper key={`${param.in}-${param.name}`} param={param} />
))}
{/* Optional Parameters */}
{optionalParams.length > 0 && (
<>
<button
type="button"
className="openapi-explorer__show-more-btn"
onClick={() => setShowOptional((prev) => !prev)}
>
<span
style={{
width: "1.5em",
display: "inline-block",
textAlign: "center",
}}
>
<span
className={
showOptional
? "openapi-explorer__plus-btn--expanded"
: "openapi-explorer__plus-btn"
}
>
<div>
<svg
style={{
fill: "currentColor",
width: "10px",
height: "10px",
}}
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M9 7h6a1 1 0 0 1 0 2H9v6a1 1 0 0 1-2 0V9H1a1 1 0 1 1 0-2h6V1a1 1 0 1 1 2 0z"
fillRule="evenodd"
></path>
</svg>
</div>
</span>
</span>
{showOptional
? translate({
id: OPENAPI_PARAM_OPTIONS.HIDE_OPTIONAL,
message: "Hide optional parameters",
})
: translate({
id: OPENAPI_PARAM_OPTIONS.SHOW_OPTIONAL,
message: "Show optional parameters",
})}
</button>
<div
className={
showOptional
? "openapi-explorer__show-options"
: "openapi-explorer__hide-options"
}
>
{optionalParams.map((param) => (
<ParamOptionWrapper
key={`${param.in}-${param.name}`}
param={param}
/>
))}
</div>
</>
)}
</>
);
}
export default ParamOptions;