ConceptPortal-public/rsconcept/frontend/src/components/Common/FileInput.tsx

66 lines
1.5 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
2023-07-15 17:46:19 +03:00
import { useRef, useState } from 'react';
2023-07-25 20:27:29 +03:00
2023-07-15 17:46:19 +03:00
import { UploadIcon } from '../Icons';
import Button from './Button';
import Label from './Label';
2023-09-07 16:30:43 +03:00
interface FileInputProps
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'className' | 'title' | 'style' | 'accept' | 'type'> {
2023-07-15 17:46:19 +03:00
label: string
2023-09-07 16:30:43 +03:00
tooltip?: string
dimensions?: string
2023-11-05 16:31:49 +03:00
acceptType?: string
2023-07-15 17:46:19 +03:00
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void
}
2023-09-07 16:30:43 +03:00
function FileInput({
label, acceptType, tooltip,
dimensions = 'w-fit', onChange,
...restProps
2023-09-07 16:30:43 +03:00
}: FileInputProps) {
2023-07-15 17:46:19 +03:00
const inputRef = useRef<HTMLInputElement | null>(null);
2023-09-07 16:30:43 +03:00
const [fileName, setFileName] = useState('');
2023-07-25 20:27:29 +03:00
2023-07-15 17:46:19 +03:00
const handleUploadClick = () => {
inputRef.current?.click();
};
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
2023-07-25 20:27:29 +03:00
if (event.target.files && event.target.files.length > 0) {
2023-09-07 16:30:43 +03:00
setFileName(event.target.files[0].name)
2023-07-15 17:46:19 +03:00
} else {
2023-09-07 16:30:43 +03:00
setFileName('')
2023-07-15 17:46:19 +03:00
}
if (onChange) {
onChange(event);
}
};
2023-07-25 20:27:29 +03:00
2023-07-15 17:46:19 +03:00
return (
<div className={clsx(
'py-2',
'flex flex-col gap-2 items-start',
dimensions
)}>
<input type='file'
ref={inputRef}
style={{ display: 'none' }}
accept={acceptType}
onChange={handleFileChange}
{...restProps}
/>
<Button
text={label}
icon={<UploadIcon/>}
onClick={handleUploadClick}
tooltip={tooltip}
/>
<Label text={fileName} />
</div>);
2023-07-15 17:46:19 +03:00
}
export default FileInput;