Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useFileDialog): new function (#1218)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com> Co-authored-by: Jelf <353742991@qq.com>
- Loading branch information
1 parent
82510b8
commit 44526f0
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,20 @@ | ||
<script setup lang="ts"> | ||
import { useFileDialog } from '.' | ||
const { files, open, reset } = useFileDialog() | ||
</script> | ||
|
||
<template> | ||
<button type="button" @click="open()"> | ||
Choose files | ||
</button> | ||
<button type="button" :disabled="!files" @click="reset()"> | ||
Reset | ||
</button> | ||
<template v-if="files"> | ||
<p>You have selected: <b>{{ files.length }} files</b></p> | ||
<li v-for="file of files" :key="file.name"> | ||
{{ file.name }} | ||
</li> | ||
</template> | ||
</template> |
This file contains 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,21 @@ | ||
--- | ||
category: Browser | ||
--- | ||
|
||
# useFileDialog | ||
|
||
Open file dialog with ease. | ||
|
||
## Usage | ||
|
||
```ts | ||
import { useFileDialog } from '@vueuse/core' | ||
|
||
const { files, open, reset } = useDialog() | ||
``` | ||
|
||
```html | ||
<template> | ||
<button type="button" @click="open">Choose file</button> | ||
</template> | ||
``` |
This file contains 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,65 @@ | ||
import { readonly, ref } from 'vue-demi' | ||
|
||
export interface UseFileDialogOptions { | ||
/** | ||
* @default true | ||
*/ | ||
multiple?: boolean | ||
/** | ||
* @default '*' | ||
*/ | ||
accept?: 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 | ||
} | ||
|
||
const DEFAULT_OPTIONS: UseFileDialogOptions = { | ||
multiple: true, | ||
accept: '*', | ||
} | ||
|
||
/** | ||
* Open file dialog with ease. | ||
* | ||
* @see https://vueuse.org/useFileDialog | ||
* @param options | ||
*/ | ||
export function useFileDialog(options?: Partial<UseFileDialogOptions>) { | ||
const files = ref<FileList | null>(null) | ||
|
||
const input = document.createElement('input') | ||
input.type = 'file' | ||
|
||
input.onchange = (event: Event) => { | ||
const result = event.target as HTMLInputElement | ||
files.value = result.files | ||
} | ||
|
||
const open = (localOptions?: Partial<UseFileDialogOptions>) => { | ||
const _options = { | ||
...DEFAULT_OPTIONS, | ||
...options, | ||
...localOptions, | ||
} | ||
input.multiple = _options.multiple! | ||
input.accept = _options.accept! | ||
input.capture = _options.capture! | ||
|
||
input.click() | ||
} | ||
|
||
const reset = () => { | ||
files.value = null | ||
} | ||
|
||
return { | ||
files: readonly(files), | ||
open, | ||
reset, | ||
} | ||
} | ||
|
||
export type UseFileDialogReturn = ReturnType<typeof useFileDialog> |