Skip to content

fix(VFileUpload): filter files based on accepted type if passed (#21150) #21158

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

Closed
wants to merge 14 commits into from

Conversation

jcjp
Copy link
Contributor

@jcjp jcjp commented Mar 25, 2025

Description

Component VFileUpload allows unsupported files to be included in the list if accept type is passed.


@jcjp jcjp self-assigned this Mar 25, 2025
@jcjp jcjp requested review from johnleider and Spatlani March 25, 2025 15:22
@jcjp jcjp added labs Must be completed for this component to leave labs C: VFileUpload T: bug Functionality that does not work as intended/expected labels Mar 25, 2025
@johnleider
Copy link
Member

Do we currently handle this use-case in VFileInput?

@jcjp
Copy link
Contributor Author

jcjp commented Mar 26, 2025

Do we currently handle this use-case in VFileInput?

Will check I wasn't able to see if we handle this already there.

@MajesticPotatoe
Copy link
Member

We currently don't handle this on v-file-input

@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch from 8609aa5 to 63b48ca Compare March 27, 2025 11:17
@johnleider
Copy link
Member

We currently don't handle this on v-file-input

Would it then be considered a bug for that component as well? If so, let's implement a shared fix.

@MajesticPotatoe
Copy link
Member

We currently don't handle this on v-file-input

Would it then be considered a bug for that component as well? If so, let's implement a shared fix.

Yes

@jcjp
Copy link
Contributor Author

jcjp commented Mar 28, 2025

I will work on adding the fix on VFileInput as well on this PR will update. Updated move the filtering on a shared function and both utilized on VFileUpload and VFileInput, let me know if this solution works thanks!

@johnleider johnleider added this to the v3.7.x milestone Mar 28, 2025
@jcjp jcjp changed the title fix(VFileUpload): filter only accepted type on drop or browse fix(VFileUpload): filter files based on accepted type if passed Mar 29, 2025
@KaelWD KaelWD modified the milestones: v3.7.x, v3.8.x Apr 1, 2025
@KaelWD
Copy link
Member

KaelWD commented Apr 23, 2025

This check could probably be moved to the useProxiedModel transformOut callback.

@jcjp
Copy link
Contributor Author

jcjp commented Apr 25, 2025

This check could probably be moved to the useProxiedModel transformOut callback.

Thanks will check and try to move this solution to that part.

@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch from d775471 to 3d63982 Compare April 27, 2025 15:18
@jcjp jcjp requested a review from KaelWD April 27, 2025 15:19
@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch 2 times, most recently from 3642a4d to bcc117f Compare April 28, 2025 15:44
@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch from bcc117f to 99576a1 Compare May 3, 2025 12:32
@jcjp jcjp requested a review from Copilot May 3, 2025 12:35
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue in VFileUpload where files were not being filtered by the accepted type and updates the file input and drag-and-drop event handling accordingly.

  • Introduces a new helper function filterFilesByAcceptType to restrict files by type.
  • Updates the model conversion functions in VFileUpload and VFileInput components to apply file filtering.
  • Adjusts event handlers for drag-and-drop and file input change to use the new filtering logic.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/vuetify/src/util/helpers.ts Adds the filterFilesByAcceptType helper for file type filtering.
packages/vuetify/src/labs/VFileUpload/VFileUpload.tsx Integrates file filtering in model conversion and modifies the drag-and-drop event handler.
packages/vuetify/src/components/VFileInput/tests/VFileInput.spec.browser.tsx Updates the test to include an accept attribute.
packages/vuetify/src/components/VFileInput/VFileInput.tsx Incorporates file filtering in model conversion and event handlers.

export function filterFilesByAcceptType (files: null | FileList | File[], acceptType?: string): File[] {
if (!files) return []
if (!acceptType) return Array.from(files)
return Array.from(files).filter(file => file.type === acceptType)

This comment was marked as off-topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File type will be dependent provided from the user, like in the example linked on this PR, but do let me know if it is worthwhile to add the suggestion of copilot thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

support for image/png, image/jpeg or wildcards image/*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this one it will depend on the type the that user set if no type was set it will return what was uploaded by default with no restriction.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what happens when user sets accept="image/*" and selects jpeg image with type of image/jpeg that does not strictly match the accept attribute value... the file does not pass out of this function. None of the files get out when the user is using wildcards or even comma-separated list of types.

@johnleider johnleider added the S: has merge conflicts The pending Pull Request has merge conflicts label May 13, 2025
@johnleider johnleider removed the request for review from Spatlani May 13, 2025 20:49
@jcjp jcjp requested a review from eesayas May 18, 2025 05:46
@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch from 433ba1c to 5ff49bf Compare May 18, 2025 06:15
@jcjp jcjp removed the S: has merge conflicts The pending Pull Request has merge conflicts label May 18, 2025
@jcjp jcjp changed the title fix(VFileUpload): filter files based on accepted type if passed fix(VFileUpload): filter files based on accepted type if passed (#21150) May 18, 2025
@johnleider
Copy link
Member

I don't know how I feel about having all of this functionality in the transform out function: https://github.com/vuetifyjs/vuetify/pull/21158/files#diff-1782c90664c1a45585f4b491f5801e24eeeaca9cd2c0b96ebfa168d37baa4c92R103-R118

@KaelWD is this what you meant?

@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch 2 times, most recently from 8e0e4d4 to ed21f30 Compare May 31, 2025 13:38
@jcjp jcjp enabled auto-merge (squash) June 4, 2025 10:30
jcjp added 13 commits June 4, 2025 18:31
chore(VFileInput): move filter to shared function
chore(VFileUpload): update to use shared function
fix(VFileInput): same with VFileUpload filter if accept type passed
refactor(helper): return empty if no files passed
refactor(helper): return files as array if no accept type passed
feat(VFileUpload): accept File[] as type input on filter
- to be able to identify the event type when a model change on transform out
- fixes unit test issue on VFileInput not emit change on blur
@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch from ed21f30 to 9333d52 Compare June 4, 2025 10:34
@jcjp jcjp requested a review from a team June 4, 2025 10:35
@johnleider
Copy link
Member

Bringing @J-Sek into this since he's ventured into file land.

@jcjp jcjp marked this pull request as draft June 14, 2025 11:32
auto-merge was automatically disabled June 14, 2025 11:32

Pull request was converted to draft

@jcjp jcjp force-pushed the fix(VFileUpload)/accepts-unsupported-type branch from 4ed69b3 to e0f8f68 Compare June 14, 2025 11:38
@jcjp
Copy link
Contributor Author

jcjp commented Jun 15, 2025

Replaced by: #21576

@jcjp jcjp closed this Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: VFileInput C: VFileUpload labs Must be completed for this component to leave labs T: bug Functionality that does not work as intended/expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug Report][3.7.18] VFileUpload drag-and-drop doesn't use "accept" prop for allowed file types
6 participants