import clsx from 'clsx'; import { ChangeEvent, ComponentProps, createRef } from 'react'; import { CheckIcon, Loader2Icon, UploadIcon, XCircleIcon } from 'lucide-react'; import { AutomationTestingProps } from '@/types'; import { Button } from '@@/buttons'; import { Icon } from '@@/Icon'; import { Tooltip } from '@@/Tip/Tooltip'; export interface Props extends AutomationTestingProps { onChange(value: File): void; value?: File | null; accept?: string; title?: string; required?: boolean; inputId: string; className?: string; color?: ComponentProps['color']; name?: string; hideFilename?: boolean; tooltip?: string; state?: 'uploading' | 'success'; disabled?: boolean; } export function FileUploadField({ onChange, value, accept, title = 'Select a file', required = false, inputId, className, color = 'primary', name, hideFilename, tooltip, disabled, state, 'data-cy': dataCy, }: Props) { const fileRef = createRef(); return (
{tooltip && }
); function handleButtonClick() { if (fileRef && fileRef.current) { fileRef.current.click(); } } function changeHandler(event: ChangeEvent) { if (event.target && event.target.files && event.target.files.length > 0) { onChange(event.target.files[0]); } } } function FileStatus({ state, value, hideFilename, required, dataCy, }: { state?: 'uploading' | 'success'; value?: File | null; hideFilename?: boolean; required?: boolean; dataCy?: string; }) { const stateIcon = getStateIcon(state, dataCy); const filename = !hideFilename && value ? value.name : null; if (stateIcon || filename) { return ( {stateIcon} {filename} ); } if (required) { return ( ); } return null; } function getStateIcon(state?: 'uploading' | 'success', dataCy?: string) { if (state === 'uploading') { return ( ); } if (state === 'success') { return ( ); } return null; }