2022-01-05 13:39:34 +00:00
|
|
|
import { ChangeEvent, createRef } from 'react';
|
|
|
|
|
2022-06-17 16:18:42 +00:00
|
|
|
import { Button } from '@@/buttons';
|
2022-08-02 02:10:39 +00:00
|
|
|
import { Icon } from '@@/Icon';
|
2022-01-05 13:39:34 +00:00
|
|
|
|
|
|
|
import styles from './FileUploadField.module.css';
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
onChange(value: File): void;
|
|
|
|
value?: File;
|
2022-01-23 19:48:04 +00:00
|
|
|
accept?: string;
|
2022-01-05 13:39:34 +00:00
|
|
|
title?: string;
|
|
|
|
required?: boolean;
|
2022-05-23 14:32:51 +00:00
|
|
|
inputId: string;
|
2022-01-05 13:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function FileUploadField({
|
|
|
|
onChange,
|
|
|
|
value,
|
2022-01-23 19:48:04 +00:00
|
|
|
accept,
|
2022-01-05 13:39:34 +00:00
|
|
|
title = 'Select a file',
|
|
|
|
required = false,
|
2022-05-23 14:32:51 +00:00
|
|
|
inputId,
|
2022-01-05 13:39:34 +00:00
|
|
|
}: Props) {
|
|
|
|
const fileRef = createRef<HTMLInputElement>();
|
|
|
|
|
|
|
|
return (
|
2022-08-02 02:10:39 +00:00
|
|
|
<div className="file-upload-field vertical-center">
|
2022-01-05 13:39:34 +00:00
|
|
|
<input
|
2022-05-23 14:32:51 +00:00
|
|
|
id={inputId}
|
2022-01-05 13:39:34 +00:00
|
|
|
ref={fileRef}
|
|
|
|
type="file"
|
2022-01-23 19:48:04 +00:00
|
|
|
accept={accept}
|
2022-01-05 13:39:34 +00:00
|
|
|
required={required}
|
|
|
|
className={styles.fileInput}
|
|
|
|
onChange={changeHandler}
|
|
|
|
aria-label="file-input"
|
|
|
|
/>
|
2022-01-23 19:48:04 +00:00
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
color="primary"
|
|
|
|
onClick={handleButtonClick}
|
|
|
|
className={styles.fileButton}
|
|
|
|
>
|
2022-01-05 13:39:34 +00:00
|
|
|
{title}
|
|
|
|
</Button>
|
|
|
|
|
2022-08-02 02:10:39 +00:00
|
|
|
<span className="vertical-center">
|
2022-08-11 20:43:01 +00:00
|
|
|
{value ? value.name : <Icon icon="x-circle" feather mode="danger" />}
|
2022-01-05 13:39:34 +00:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
function handleButtonClick() {
|
|
|
|
if (fileRef && fileRef.current) {
|
|
|
|
fileRef.current.click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeHandler(event: ChangeEvent<HTMLInputElement>) {
|
|
|
|
if (event.target && event.target.files && event.target.files.length > 0) {
|
|
|
|
onChange(event.target.files[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|