/* ============================================================================ * 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 FloatingButton from "@theme/ApiExplorer/FloatingButton"; import { OPENAPI_FORM_FILE_UPLOAD } from "@theme/translationIds"; import MagicDropzone from "react-magic-dropzone"; type PreviewFile = { preview: string } & File; interface RenderPreviewProps { file: PreviewFile; } function RenderPreview({ file }: RenderPreviewProps) { switch (file.type) { case "image/png": case "image/jpeg": case "image/jpg": case "image/svg+xml": return ( ); default: return (
{file.name}
); } } export interface Props { placeholder: string; onChange?(file?: File): any; } function FormFileUpload({ placeholder, onChange }: Props) { const [hover, setHover] = useState(false); const [file, setFile] = useState(); function setAndNotifyFile(file?: PreviewFile) { setFile(file); onChange?.(file); } function handleDrop(accepted: PreviewFile[]) { const [file] = accepted; setAndNotifyFile(file); setHover(false); } return ( setHover(true)} onDragLeave={() => setHover(false)} multiple={false} style={{ marginTop: "calc(var(--ifm-pre-padding) / 2)" }} > {file ? ( <> ) : (
{placeholder}
)}
); } export default FormFileUpload;