2024-03-05 17:42:30 -05:00
|
|
|
/**
|
|
|
|
|
* FileInput Component
|
|
|
|
|
*
|
|
|
|
|
* Renders a file input with drag-and-drop support. Displays the selected file's name and size,
|
|
|
|
|
* and simulates file upload progress. Integrates with Formik for form state management.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-03-04 07:53:06 -05:00
|
|
|
import { faFile } from "@fortawesome/free-solid-svg-icons";
|
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
|
import { useField, useFormikContext } from "formik";
|
|
|
|
|
import React, { useRef, useState } from "react";
|
|
|
|
|
import ErrorMessage from "./ErrorMessage";
|
|
|
|
|
import RequiredAsterisk from "./RequiredAsterisk";
|
2024-03-04 20:50:32 -05:00
|
|
|
import Separator from "./Separator";
|
2024-03-03 22:09:33 -05:00
|
|
|
|
|
|
|
|
interface FileInputProps {
|
2024-03-04 07:53:06 -05:00
|
|
|
label: string;
|
|
|
|
|
required?: boolean;
|
|
|
|
|
name: string;
|
2024-03-03 22:09:33 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-03 22:34:43 -05:00
|
|
|
const FileInput: React.FC<FileInputProps> = ({ label, ...props }) => {
|
2024-03-04 07:53:06 -05:00
|
|
|
const { required } = props;
|
|
|
|
|
const [field, meta] = useField(props);
|
|
|
|
|
const { setFieldValue } = useFormikContext();
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
2024-03-05 17:42:30 -05:00
|
|
|
|
|
|
|
|
// State to manage drag-over styling
|
|
|
|
|
const [dragging, setDragging] = useState(false);
|
|
|
|
|
|
|
|
|
|
// State to store information about the selected file
|
|
|
|
|
const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | null>(null);
|
|
|
|
|
|
|
|
|
|
// State to simulate and display upload progress
|
2024-03-04 07:53:06 -05:00
|
|
|
const [uploadProgress, setUploadProgress] = useState(0);
|
|
|
|
|
|
2024-03-05 17:42:30 -05:00
|
|
|
// Simulates file upload progress
|
2024-03-04 07:53:06 -05:00
|
|
|
const simulateUpload = () => {
|
|
|
|
|
let progress = 0;
|
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
|
progress += 10;
|
|
|
|
|
setUploadProgress(progress);
|
|
|
|
|
if (progress >= 100) {
|
|
|
|
|
clearInterval(interval);
|
|
|
|
|
}
|
|
|
|
|
}, 20); // Adjust time interval to control speed of the animation
|
|
|
|
|
};
|
2024-03-03 22:09:33 -05:00
|
|
|
|
2024-03-05 17:42:30 -05:00
|
|
|
// Handles file selection via input change event
|
2024-03-03 22:09:33 -05:00
|
|
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
2024-03-04 07:53:06 -05:00
|
|
|
const { files } = event.target;
|
|
|
|
|
const file = files ? files[0] : null;
|
|
|
|
|
setFieldValue(props.name, file); // Update Formik's state
|
|
|
|
|
if (file) {
|
|
|
|
|
setFileInfo({ name: file.name, size: file.size });
|
|
|
|
|
setUploadProgress(0); // Reset progress
|
|
|
|
|
simulateUpload(); // Simulate upload process
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-03-03 22:09:33 -05:00
|
|
|
|
2024-03-05 17:42:30 -05:00
|
|
|
// Opens the file input dialog
|
2024-03-03 22:09:33 -05:00
|
|
|
const handleClick = () => {
|
|
|
|
|
if (fileInputRef.current) {
|
2024-03-04 07:53:06 -05:00
|
|
|
fileInputRef.current.click();
|
2024-03-03 22:09:33 -05:00
|
|
|
}
|
2024-03-04 07:53:06 -05:00
|
|
|
};
|
2024-03-03 22:09:33 -05:00
|
|
|
|
2024-03-05 17:42:30 -05:00
|
|
|
// Handles file drop
|
2024-03-03 22:09:33 -05:00
|
|
|
const handleDrop = (event: React.DragEvent<HTMLDivElement>) => {
|
2024-03-04 07:53:06 -05:00
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
setDragging(false); // Reset drag state
|
|
|
|
|
const files = event.dataTransfer.files;
|
2024-03-03 22:09:33 -05:00
|
|
|
if (files && files.length > 0) {
|
2024-03-04 07:53:06 -05:00
|
|
|
const file = files[0];
|
|
|
|
|
if (file) {
|
|
|
|
|
setFileInfo({ name: file.name, size: file.size });
|
|
|
|
|
setFieldValue(props.name, file);
|
|
|
|
|
}
|
|
|
|
|
setUploadProgress(0); // Reset progress
|
|
|
|
|
simulateUpload(); // Simulate upload process
|
2024-03-03 22:09:33 -05:00
|
|
|
}
|
2024-03-04 07:53:06 -05:00
|
|
|
};
|
|
|
|
|
|
2024-03-05 17:42:30 -05:00
|
|
|
// Manages drag-over state for styling
|
2024-03-04 07:53:06 -05:00
|
|
|
const handleDragOver = (event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (!dragging) {
|
|
|
|
|
setDragging(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-05 17:42:30 -05:00
|
|
|
// Resets dragging state when dragging leaves the drop area
|
2024-03-04 07:53:06 -05:00
|
|
|
const handleDragLeave = (event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
setDragging(false);
|
|
|
|
|
};
|
2024-03-03 22:09:33 -05:00
|
|
|
|
|
|
|
|
return (
|
2024-03-04 20:50:32 -05:00
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
{/* Title */}
|
|
|
|
|
<div className="flex flex-col gap-4">
|
|
|
|
|
<Separator/>
|
2024-03-04 23:33:49 -05:00
|
|
|
<label className="font-bold text-sm leading-tight text-indigo-900" htmlFor={props.name}>
|
2024-03-04 20:50:32 -05:00
|
|
|
{label}
|
|
|
|
|
<RequiredAsterisk required={required} />
|
|
|
|
|
</label>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* File drop area */}
|
2024-03-04 07:53:06 -05:00
|
|
|
<div
|
|
|
|
|
className={`border border-grey-500 rounded-md p-4 flex flex-col items-center gap-4 ${dragging ? "bg-blue-100" : "bg-white"} cursor-pointer`}
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
onDrop={handleDrop}
|
|
|
|
|
onDragOver={handleDragOver}
|
|
|
|
|
onDragLeave={handleDragLeave}
|
|
|
|
|
>
|
2024-03-03 23:32:46 -05:00
|
|
|
<div className="border border-grey-500 border-dashed flex flex-col gap-4 items-center justify-center rounded-md py-9 w-full">
|
|
|
|
|
<FontAwesomeIcon icon={faFile} className="text-amber-500 h-5 w-5" />
|
2024-03-04 23:33:49 -05:00
|
|
|
<h4 className="text-sm leading-tight">Drag & Drop Here or <span className="font-bold">Browse</span></h4>
|
2024-03-03 22:09:33 -05:00
|
|
|
</div>
|
2024-03-03 23:32:46 -05:00
|
|
|
<button type="button" className="text-white px-12 py-2 rounded-md bg-blue-950 w-fit">Upload Manifest</button>
|
2024-03-03 22:09:33 -05:00
|
|
|
</div>
|
2024-03-04 20:50:32 -05:00
|
|
|
|
|
|
|
|
{/* Hidden input */}
|
2024-03-03 22:34:43 -05:00
|
|
|
<input type="file" className="hidden" ref={fileInputRef} onChange={handleChange} />
|
2024-03-04 20:50:32 -05:00
|
|
|
|
|
|
|
|
{/* File info */}
|
2024-03-04 07:53:06 -05:00
|
|
|
{fileInfo && (
|
2024-03-04 20:50:32 -05:00
|
|
|
<div className="flex gap-4 p-4 items-center border-t border-grey-500">
|
2024-03-04 07:53:06 -05:00
|
|
|
<FontAwesomeIcon icon={faFile} className="text-amber-500 h-5 w-5" />
|
|
|
|
|
<div className="grow">
|
|
|
|
|
<div className="flex justify-between text-sm text-slate-500">
|
|
|
|
|
<p className="text-sm">{fileInfo.name}</p>
|
|
|
|
|
<p className="text-xs">{Math.round(fileInfo.size / 1024)} KB</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="w-full bg-gray-200 rounded-full h-1 dark:bg-gray-700">
|
|
|
|
|
<div className="bg-amber-500 h-1 rounded-full" style={{ width: `${uploadProgress}%` }}></div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-03-04 20:50:32 -05:00
|
|
|
|
2024-03-03 22:09:33 -05:00
|
|
|
<ErrorMessage touched={meta.touched} error={meta.error} />
|
|
|
|
|
</div>
|
2024-03-04 07:53:06 -05:00
|
|
|
);
|
|
|
|
|
};
|
2024-03-03 22:09:33 -05:00
|
|
|
|
2024-03-04 07:53:06 -05:00
|
|
|
export default FileInput;
|