Skip to content

Commit

Permalink
Added accept parameter to FileUpload component (#21899)
Browse files Browse the repository at this point in the history
ref DES-182

- adds `accept` parameter to the file uploads making it possible to
define a set of accepted file types
- allows only zip and json files for the universal importer
  • Loading branch information
minimaluminium authored Jan 30, 2025
1 parent 048f177 commit 2827562
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions apps/admin-x-design-system/src/global/form/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export interface FileUploadProps {
style?: CSSProperties;
unstyled?: boolean;
inputRef?: React.RefObject<HTMLInputElement>;
accept?: string;
}

const FileUpload: React.FC<FileUploadProps> = ({id, onUpload, children, style, unstyled = false, inputRef, className, dragIndicatorClassName, ...props}) => {
const FileUpload: React.FC<FileUploadProps> = ({id, onUpload, children, style, unstyled = false, inputRef, className, dragIndicatorClassName, accept, ...props}) => {
const [fileKey, setFileKey] = useState<number>(Date.now());
const [isDragging, setIsDragging] = useState(false);

Expand Down Expand Up @@ -52,7 +53,7 @@ const FileUpload: React.FC<FileUploadProps> = ({id, onUpload, children, style, u
return (
<label className={clsx('relative', className)} htmlFor={id} style={style} onDragEnter={handleDragging} onDragLeave={handleStopDragging} onDragOver={handleDragging} onDrop={handleDrop} {...props}>
<div className={clsx({'absolute inset-1 rounded': true, 'border-2 border-dashed border-grey-400/25': isDragging}, isDragging && [dragIndicatorClassName])} />
<input key={fileKey} ref={inputRef || null} id={id} type="file" hidden onChange={handleFileChange} />
<input key={fileKey} ref={inputRef || null} accept={accept} id={id} type="file" hidden onChange={handleFileChange} />
{(typeof children === 'string') ?
<div className={!unstyled ? `inline-flex h-[34px] cursor-pointer items-center justify-center rounded px-4 text-sm font-semibold hover:bg-grey-100 dark:text-white dark:hover:bg-grey-900` : ''}>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const UniversalImportModal: React.FC = () => {
>
<div className='py-4 leading-9'>
<FileUpload
accept="application/json, application/zip"
id="import-file"
onUpload={async (file) => {
setUploading(true);
Expand Down

0 comments on commit 2827562

Please sign in to comment.