);
interface IFileUploaderProps {
graphicName: ISupportedGraphicNames | ISupportedGraphicNames[];
message: string;
additionalInfo?: string;
/** Controls the loading spinner on the upload button */
isLoading?: boolean;
/** Disables the upload button */
diabled?: boolean;
/** A comma seperated string of one or more file types accepted to upload.
* This is the same as the html accept attribute.
* https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept
*/
accept?: string;
/** The text to display on the upload button
* @default "Upload"
*/
buttonMessage?: string;
className?: string;
/** renders the button to open the file uploader to appear as a button or
* a link.
* @default "button"
*/
buttonType?: "button" | "link";
/** If provided FileUploader will display this component when the file is
* selected. This is used for previewing the file before uploading.
*/
filePreview?: ReactNode; // TODO: refactor this to be a function that returns a ReactNode?
onFileUpload: (files: FileList | null) => void;
}
/**
* A component that encapsulates the UI for uploading a file.
*/
export const FileUploader = ({
graphicName: graphicNames,
message,
additionalInfo,
isLoading = false,
diabled = false,
accept,
filePreview,
className,
buttonMessage = "Upload",
buttonType = "button",
onFileUpload,
}: IFileUploaderProps) => {
const [isFileSelected, setIsFileSelected] = useState(false);
const classes = classnames(baseClass, className, {
[`${baseClass}__file-preview`]: filePreview !== undefined && isFileSelected,
});
const buttonVariant = buttonType === "button" ? "brand" : "text-icon";
const onFileSelect = (e: React.ChangeEvent) => {
const files = e.target.files;
onFileUpload(files);
setIsFileSelected(true);
e.target.value = "";
};
const renderGraphics = () => {
const graphicNamesArr =
typeof graphicNames === "string" ? [graphicNames] : graphicNames;
return graphicNamesArr.map((graphicName) => (
));
};
return (
{isFileSelected && filePreview ? (
filePreview
) : (
<>