-
Notifications
You must be signed in to change notification settings - Fork 12
[UXIT-3464] Add compact Upload component #87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { FormControl, FormField, FormMessage, Root } from '@radix-ui/react-form' | ||
| import { PlusIcon } from 'lucide-react' | ||
| import { useRef, useState } from 'react' | ||
| import { MAX_FILE_SIZE } from '@/constants/files.ts' | ||
| import { formatFileSize } from '@/utils/format-file-size.ts' | ||
| import { ButtonBase as Button } from '../ui/button/button-base.tsx' | ||
|
|
||
| interface UploadButtonProps { | ||
| onUpload: (file: File) => void | ||
| isUploading?: boolean | ||
| accept?: string[] | ||
| maxSize?: number | ||
| } | ||
|
|
||
| export function UploadButton({ | ||
| onUpload, | ||
| isUploading = false, | ||
| accept = ['*'], | ||
| maxSize = MAX_FILE_SIZE, | ||
| }: UploadButtonProps) { | ||
| const fileInputRef = useRef<HTMLInputElement>(null) | ||
| const [error, setError] = useState<string | null>(null) | ||
|
|
||
| function handleButtonClick() { | ||
| fileInputRef.current?.click() | ||
| } | ||
|
|
||
| function handleFileChange(e: React.ChangeEvent<HTMLInputElement>) { | ||
| const file = e.target.files?.[0] | ||
| if (file) { | ||
| if (file.size > maxSize) { | ||
| setError(`File is too large. Maximum size is ${formatFileSize(maxSize)}.`) | ||
| if (fileInputRef.current) fileInputRef.current.value = '' | ||
| return | ||
| } | ||
|
|
||
| setError(null) | ||
| onUpload(file) | ||
| if (fileInputRef.current) fileInputRef.current.value = '' | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Root className="flex flex-col gap-2 items-end" onSubmit={(e) => e.preventDefault()}> | ||
| <FormField name="file"> | ||
| <FormControl asChild> | ||
| <input | ||
| accept={accept.join(',')} | ||
| className="hidden" | ||
| multiple={false} | ||
| onChange={handleFileChange} | ||
| ref={fileInputRef} | ||
| type="file" | ||
| /> | ||
| </FormControl> | ||
| </FormField> | ||
| <div className="w-content"> | ||
| <Button | ||
| disabled={isUploading} | ||
| loading={isUploading} | ||
| onClick={handleButtonClick} | ||
| type="button" | ||
| variant="primary" | ||
| > | ||
| <div className="flex items-center gap-2"> | ||
| <PlusIcon size={20} /> | ||
| <span>Add file</span> | ||
| </div> | ||
| </Button> | ||
| </div> | ||
| <div className="h-3"> | ||
| {error && ( | ||
| <FormField name="file"> | ||
| <FormMessage className="text-sm text-red-500 mt-1 break-words max-w-full">{error}</FormMessage> | ||
| </FormField> | ||
| )} | ||
| </div> | ||
| </Root> | ||
| ) | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const MAX_FILE_SIZE = 200_000_000 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe if you pass
sizehere, the user will only be able to pick files with an appropriate size, hence rendering the usesetError(File is too large. Maximum size is ${formatFileSize(maxSize)}.)unnecessary.To be tested, maybe both are needed!
https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/input/file#size
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like that section is under “Getting information on selected files,” which is all about the File API, not HTML attribute? 👀