diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.tsx index e2fd9a10f..210776fcf 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.tsx @@ -43,11 +43,6 @@ export default function FileArrayFormBodyItem({ return; } - let maxIndex = 0; - - newItems.keys().forEach((item) => { - maxIndex = item > maxIndex ? item : maxIndex; - }); newItems.set(index, { src: `/path/to/${file.name}`, content: file, diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FormBodyItem/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FormBodyItem/index.tsx index 55abd27df..f87c9178a 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FormBodyItem/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FormBodyItem/index.tsx @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. * ========================================================================== */ -import React from "react"; +import React, { useEffect, useState } from "react"; import FormFileUpload from "@theme/ApiExplorer/FormFileUpload"; import FormLabel from "@theme/ApiExplorer/FormLabel"; @@ -24,6 +24,7 @@ interface FormBodyItemProps { schema: SchemaObject; label?: string; required?: boolean; + exampleValue?: SchemaObject["example"]; } export default function FormBodyItem({ @@ -32,8 +33,27 @@ export default function FormBodyItem({ schema, label, required, + exampleValue, }: FormBodyItemProps): React.JSX.Element { const dispatch = useTypedDispatch(); + const [value, setValue] = useState(() => { + let initialValue = exampleValue ?? ""; + + if (schemaObject.type === "object" && exampleValue) { + initialValue = JSON.stringify(exampleValue, null, 2); + } + + return initialValue; + }); + + useEffect(() => { + if (value) { + dispatch(setStringFormBody({ key: id, value })); + } else { + dispatch(clearFormBodyKey(id)); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); if ( schemaObject.type === "array" && @@ -73,27 +93,18 @@ export default function FormBodyItem({ ); } - if ( - schemaObject.type === "object" && - (schemaObject.example || schemaObject.examples) - ) { - const objectExample = JSON.stringify( - schemaObject.example ?? schemaObject.examples[0], - null, - 2 - ); - + if (schemaObject.type === "object") { return ( - <> - {label && } - - dispatch(setStringFormBody({ key: id, value: code })) - } - > - {objectExample} - - + <> + {label && } + + dispatch(setStringFormBody({ key: id, value: code })) + } + > + {value} + + ); } @@ -105,9 +116,11 @@ export default function FormBodyItem({ ) => { const val = e.target.value; + setValue(val); if (val === "---") { dispatch(clearFormBodyKey(id)); } else { @@ -127,12 +140,14 @@ export default function FormBodyItem({ ) => { + setValue(e.target.value); dispatch(setStringFormBody({ key: id, value: e.target.value })); }} /> diff --git a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/index.tsx b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/index.tsx index d168b7e89..f8c976e17 100644 --- a/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/index.tsx +++ b/packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/index.tsx @@ -316,22 +316,86 @@ function Body({ ) { return ( - {Object.entries(schema.properties ?? {}).map(([key, val]: any) => { - return ( - - + {/* @ts-ignore */} + + {Object.entries(schema.properties ?? {}).map(([key, val]: any) => { + return ( + + + + ); + })} + + {example && ( + // @ts-ignore + + {Object.entries(schema.properties ?? {}).map( + ([schemaKey, schemaVal]: any) => { + return ( + + + + ); } - /> - - ); - })} + )} + + )} + {examples && + Object.entries(examples).map(([key, value]) => { + return ( + // @ts-ignore + + {Object.entries(schema.properties ?? {}).map( + ([schemaKey, schemaVal]: any) => { + return ( + + + + ); + } + )} + + ); + })} + ); }