Skip to content
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

feat(useFileDialog): add a parameter reset #3059

Merged
merged 2 commits into from May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions packages/core/useFileDialog/index.ts
Expand Up @@ -18,11 +18,17 @@ export interface UseFileDialogOptions extends ConfigurableDocument {
* @see [HTMLInputElement Capture](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/capture)
*/
capture?: string
/**
* Reset when open file dialog.
* @default false
*/
reset?: boolean
}

const DEFAULT_OPTIONS: UseFileDialogOptions = {
multiple: true,
accept: '*',
reset: false,
}

export interface UseFileDialogReturn {
Expand Down Expand Up @@ -57,6 +63,12 @@ export function useFileDialog(options: UseFileDialogOptions = {}): UseFileDialog
}
}

const reset = () => {
files.value = null
if (input)
input.value = ''
}

const open = (localOptions?: Partial<UseFileDialogOptions>) => {
if (!input)
return
Expand All @@ -69,16 +81,11 @@ export function useFileDialog(options: UseFileDialogOptions = {}): UseFileDialog
input.accept = _options.accept!
if (hasOwn(_options, 'capture'))
input.capture = _options.capture!

if (_options.reset)
reset()
input.click()
}

const reset = () => {
files.value = null
if (input)
input.value = ''
}

return {
files: readonly(files),
open,
Expand Down