import { useEffect, useRef } from 'react'; import { FileUploadProps } from './types'; import ErrorMessage from '../ErrorMessage'; import HintText from '../../common/HintText'; import DSFileUpload from '@scottish-government/design-system/dist/scripts/components/file-upload/file-upload' const FileUpload = ({ accept, capture, className, errorMessage, hasError, hintText, id, isMultiple, label, name, onBlur, onChange, text, ...props }: FileUploadProps) => { const ref = useRef(null); const errorMessageId = `error-message-${id}`; const hintTextId = `hint-text-${id}`; const describedbys: string[] = []; if (hintText) { describedbys.push(hintTextId) }; if (errorMessage) { describedbys.push(errorMessageId) }; const options = { text: text } useEffect(() => { if (ref.current) { new DSFileUpload(ref.current, options).init(); } }, [ref]); function handleBlur(event: React.FocusEvent) { if (typeof onBlur === 'function') { onBlur(event); } } function handleChange(event: React.ChangeEvent) { if (typeof onChange === 'function') { onChange(event); } } return (
{hintText && {hintText}} {errorMessage && {errorMessage}}
); }; FileUpload.displayName = 'FileUpload'; export default FileUpload;