import React, { useRef } from 'react'; import { Flex, Box, Text } from '@chakra-ui/layout'; import { Input } from '@chakra-ui/input'; import { Button } from '@chakra-ui/button'; import { MdAttachFile } from 'react-icons/md'; import * as helpres from '@educt/helpers'; /** * Hooks */ import { useColorModeValue } from '@chakra-ui/color-mode'; type FileSelectPropsType = { onChange: (file: File) => void; supportedFormats?: string[]; file?: File | undefined; }; const FileSelect: React.FC = ({ onChange, file, supportedFormats }) => { const fileInputRef = useRef(null); /** * Handle the file input selection */ const onSelectFileHandler = () => { if (fileInputRef.current) { fileInputRef.current.click(); } }; const onChangeFileHandler = (e: React.ChangeEvent) => { e.preventDefault(); const { files } = e.target; if (files && files.length !== 0) { const file = files[0]; onChange(file); } }; return ( {file ? ( {file.name}
{helpres.transformBytes(file.size)}
) : ( Choose a file... )} {supportedFormats && ( Supported formats: {supportedFormats.join(', ')} )}
); }; export default FileSelect;