Skip to content

feat(useFileDialog): add MaybRef to multiple, accept, capture, reset, and directory #4813

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 9 commits into from
Jul 28, 2025
Merged
Next Next commit
feat(useFileDialog): add MaybRef to multiple, accept, capture, reset,…
… and directory
  • Loading branch information
hunterwilhelm committed Jun 11, 2025
commit 6c435dd545d2a09d218b111f85e4ed2093b45d6d
91 changes: 91 additions & 0 deletions packages/core/useFileDialog/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,95 @@ describe('useFileDialog', () => {
expect(files.value?.[0]).toEqual(file)
expect(changeHandler).toHaveBeenCalledWith(files.value)
})

it('should work with ref value for multiple option', () => {
const input = document.createElement('input')
input.click = vi.fn()

const multipleRef = shallowRef(true)

const { open } = useFileDialog({
input,
multiple: multipleRef,
})

open()
expect(input.multiple).toBe(true)

multipleRef.value = false
open()
expect(input.multiple).toBe(false)
})

it('should work with ref value for accept option', () => {
const input = document.createElement('input')
input.click = vi.fn()

const acceptRef = shallowRef('image/*')

const { open } = useFileDialog({
input,
accept: acceptRef,
})

open()
expect(input.accept).toBe('image/*')

acceptRef.value = 'video/*'
open()
expect(input.accept).toBe('video/*')
})

it('should work with ref value for directory option', () => {
const input = document.createElement('input')
input.click = vi.fn()

const directoryRef = shallowRef(true)

const { open } = useFileDialog({
input,
directory: directoryRef,
})

open()
expect(input.webkitdirectory).toBe(true)

directoryRef.value = false
open()
expect(input.webkitdirectory).toBe(false)
})

it('should work with ref value for reset option', () => {
const input = document.createElement('input')
input.click = vi.fn()

const resetRef = shallowRef(true)

const { open } = useFileDialog({
input,
reset: resetRef,
})

open()
expect(input.click).toHaveBeenCalled() // Assuming reset does not change input attributes
})

it('should work with ref value for capture option', () => {
const input = document.createElement('input')
input.click = vi.fn()

const captureRef = shallowRef('user')

const { open } = useFileDialog({
input,
capture: captureRef,
})

open()
expect(input.capture).toBe('user')

captureRef.value = 'environment'
open()
expect(input.capture).toBe('environment')
})
})
24 changes: 12 additions & 12 deletions packages/core/useFileDialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import type { EventHookOn } from '@vueuse/shared'
import type { Ref } from 'vue'
import type { MaybeRef, Ref } from 'vue'
import type { ConfigurableDocument } from '../_configurable'
import type { MaybeElementRef } from '../unrefElement'
import { createEventHook, hasOwn } from '@vueuse/shared'
import { ref as deepRef, readonly } from 'vue'
import { ref as deepRef, readonly, toValue } from 'vue'
import { defaultDocument } from '../_configurable'
import { unrefElement } from '../unrefElement'

export interface UseFileDialogOptions extends ConfigurableDocument {
/**
* @default true
*/
multiple?: boolean
multiple?: MaybeRef<boolean>
/**
* @default '*'
*/
accept?: string
accept?: MaybeRef<string>
/**
* Select the input source for the capture file.
* @see [HTMLInputElement Capture](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/capture)
*/
capture?: string
capture?: MaybeRef<string>
/**
* Reset when open file dialog.
* @default false
*/
reset?: boolean
reset?: MaybeRef<boolean>
/**
* Select directories instead of files.
* @see [HTMLInputElement webkitdirectory](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory)
* @default false
*/
directory?: boolean
directory?: MaybeRef<boolean>

/**
* Initial files to set.
Expand Down Expand Up @@ -122,13 +122,13 @@ export function useFileDialog(options: UseFileDialogOptions = {}): UseFileDialog
...options,
...localOptions,
}
input.multiple = _options.multiple!
input.accept = _options.accept!
input.multiple = toValue(_options.multiple)!
input.accept = toValue(_options.accept)!
// webkitdirectory key is not stabled, maybe replaced in the future.
input.webkitdirectory = _options.directory!
input.webkitdirectory = toValue(_options.directory)!
if (hasOwn(_options, 'capture'))
input.capture = _options.capture!
if (_options.reset)
input.capture = toValue(_options.capture)!
if (toValue(_options.reset))
reset()
input.click()
}
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy