import { useRef, MouseEvent, forwardRef, ForwardedRef, useImperativeHandle, useState, ChangeEvent, useId, InputHTMLAttributes, } from 'react'; import { InputProps } from '../../types'; import { getFileSize } from '../../utils'; import { Button, ButtonProps } from '../Button/Button'; import { InputWrapper } from '../utils'; import { ReactComponent as RemoveSVG } from './images/remove.svg'; import { ReactComponent as SelectSVG } from './images/select.svg'; import styles from './FileInput.module.scss'; import { useChangeEffect } from 'hooks'; type Props = Omit, 'size' | 'value' | 'onChange' | 'accept'> & Omit & { value?: File | undefined; label?: string; error?: string; color?: ButtonProps['color']; accept?: string | string[]; onChange?: (value: File | undefined) => void; }; const FileInput = forwardRef((props: Props, forwardedRef: ForwardedRef) => { const { value, label, className, gap, error, tooltip, accept, onChange, direction = 'x', size = 'normal', color = 'light', ...attrs } = props; const { disabled } = attrs; const [innerValue, setInnerValue] = useState(); const file = value || innerValue; const setFile = onChange || setInnerValue; const ref = useRef(null); const id = useId(); const acceptValue = Array.isArray(accept) ? accept.join(',') : accept; // TODO: figure out what's wrong // @ts-ignore useImperativeHandle(forwardedRef, () => ref.current); const handleButtonClick = () => ref.current?.click(); const reset = () => { if (!ref.current) return; ref.current.value = ''; const changeEvent = new Event('change', { bubbles: true }); ref.current.dispatchEvent(changeEvent); }; const handleRemoveButtonClick = (e: MouseEvent) => { e.preventDefault(); reset(); }; const handleChange = ({ target }: ChangeEvent) => setFile(target.files?.[0]); useChangeEffect(() => { if (!value) reset(); }, [value]); return ( {file ? ( <>
{!error && Size: {getFileSize(file.size)}} ) : (