2023-12-13 14:32:57 +03:00
|
|
|
'use client';
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
import clsx from 'clsx';
|
2023-07-15 17:46:19 +03:00
|
|
|
import { useRef, useState } from 'react';
|
2023-12-16 19:20:26 +03:00
|
|
|
import { BiUpload } from 'react-icons/bi';
|
2023-07-25 20:27:29 +03:00
|
|
|
|
2023-12-18 19:42:27 +03:00
|
|
|
import { CProps } from '../props';
|
2023-07-15 17:46:19 +03:00
|
|
|
import Button from './Button';
|
|
|
|
import Label from './Label';
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
interface FileInputProps extends Omit<CProps.Input, 'accept' | 'type'> {
|
|
|
|
label: string;
|
|
|
|
|
|
|
|
acceptType?: string;
|
|
|
|
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
|
|
|
|
2024-03-18 16:21:39 +03:00
|
|
|
function FileInput({ id, label, acceptType, title, className, style, onChange, ...restProps }: 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-12-18 19:42:27 +03:00
|
|
|
setFileName(event.target.files[0].name);
|
2023-07-15 17:46:19 +03:00
|
|
|
} else {
|
2023-12-18 19:42:27 +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 (
|
2023-12-28 14:04:44 +03:00
|
|
|
<div className={clsx('py-2', 'flex flex-col gap-2 items-center', className)} style={style}>
|
|
|
|
<input
|
2024-03-18 16:21:39 +03:00
|
|
|
id={id}
|
2023-12-28 14:04:44 +03:00
|
|
|
type='file'
|
|
|
|
ref={inputRef}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
accept={acceptType}
|
|
|
|
onChange={handleFileChange}
|
|
|
|
{...restProps}
|
|
|
|
/>
|
2023-12-30 19:43:24 +03:00
|
|
|
<Button text={label} icon={<BiUpload size='1.25rem' />} onClick={handleUploadClick} title={title} />
|
2024-03-18 16:21:39 +03:00
|
|
|
<Label text={fileName} htmlFor={id} />
|
2023-12-28 14:04:44 +03:00
|
|
|
</div>
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
}
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
export default FileInput;
|