-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add onChange function to fileuploader, closes #44
- Loading branch information
Showing
3 changed files
with
90 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,50 @@ | ||
import { Icon } from '@equinor/eds-core-react' | ||
import { subdirectory_arrow_right as arrowIcon } from '@equinor/eds-icons' | ||
import { ChangeEvent, useRef } from 'react' | ||
import { theme } from '../../tokens/theme' | ||
import { File } from '../ModelInputFilesTable/ModelInputFilesTable' | ||
import { FileUpload, SelectFile } from './FileUploader.styled' | ||
|
||
interface FileUploaderProps { | ||
file: File | undefined | ||
INI?: boolean | ||
file?: File | ||
onChange: (e: ChangeEvent<HTMLInputElement>) => void | ||
acceptType: 'NC' | 'INI' | ||
} | ||
|
||
export const FileUploader = ({ file, INI }: FileUploaderProps) => { | ||
const text = INI ? 'Select parameter INI file' : 'Select model NC file' | ||
const accept = INI ? '.ini' : '.nc' | ||
export const FileUploader = ({ | ||
file, | ||
onChange, | ||
acceptType, | ||
}: FileUploaderProps) => { | ||
const INI = acceptType === 'INI' | ||
const ref = useRef<HTMLInputElement>(null) | ||
|
||
function handleClick(e: ChangeEvent<HTMLInputElement>) { | ||
e.preventDefault() | ||
ref.current?.click() | ||
} | ||
|
||
return file ? ( | ||
<>{file.name}</> | ||
<>{file}</> | ||
) : ( | ||
<FileUpload htmlFor="file-upload" className="custom-file-upload"> | ||
{INI && ( | ||
<Icon fill={theme.light.text.staticIconsTertiary} data={arrowIcon} /> | ||
)} | ||
<SelectFile>{text}</SelectFile> {INI && '(optional)'} | ||
<input id="file-upload" required type="file" accept={accept} /> | ||
</FileUpload> | ||
<form method="post" encType="multipart/form-data"> | ||
<FileUpload htmlFor="file-upload" className="custom-file-upload"> | ||
{INI && ( | ||
<Icon fill={theme.light.text.staticIconsTertiary} data={arrowIcon} /> | ||
)} | ||
<SelectFile onClick={handleClick}> | ||
{INI ? 'Select parameter INI file' : 'Select model NC file'} | ||
</SelectFile> | ||
{INI && '(optional)'} | ||
<input | ||
id="file-upload" | ||
required={!INI} | ||
type="file" | ||
accept={`.${acceptType.toLowerCase()}`} | ||
onChange={onChange} | ||
ref={ref} | ||
name={acceptType} | ||
/> | ||
</FileUpload> | ||
</form> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters