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): new function #1218

Merged
merged 11 commits into from Jul 6, 2022
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -7,7 +7,7 @@ Collection of essential Vue Composition Utilities
<a href="https://www.npmjs.com/package/@vueuse/core" target="__blank"><img src="https://img.shields.io/npm/v/@vueuse/core?color=a1b858&label=" alt="NPM version"></a>
<a href="https://www.npmjs.com/package/@vueuse/core" target="__blank"><img alt="NPM Downloads" src="https://img.shields.io/npm/dm/@vueuse/core?color=50a36f&label="></a>
<a href="https://vueuse.org" target="__blank"><img src="https://img.shields.io/static/v1?label=&message=docs%20%26%20demos&color=1e8a7a" alt="Docs & Demos"></a>
<img alt="Function Count" src="https://img.shields.io/badge/-183%20functions-13708a">
<img alt="Function Count" src="https://img.shields.io/badge/-184%20functions-13708a">
<br>
<a href="https://github.com/vueuse/vueuse" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/vueuse/vueuse?style=social"></a>
</p>
Expand Down
1 change: 1 addition & 0 deletions packages/core/index.ts
Expand Up @@ -41,6 +41,7 @@ export * from './useEventSource'
export * from './useEyeDropper'
export * from './useFavicon'
export * from './useFetch'
export * from './useFileDialog'
export * from './useFocus'
export * from './useFocusWithin'
export * from './useFps'
Expand Down
12 changes: 12 additions & 0 deletions packages/core/useFileDialog/demo.vue
@@ -0,0 +1,12 @@
<script setup lang="ts">
import { useFileDialog } from '.'

const { files, openFileDialog } = useFileDialog()
</script>

<template>
<p>You have selected: {{ files ? files.length : 0 }} files</p>
<button type="button" @click="openFileDialog">
Choose file
</button>
</template>
21 changes: 21 additions & 0 deletions packages/core/useFileDialog/index.md
@@ -0,0 +1,21 @@
---
category: Browser
---

# useFileDialog

Open file dialog with ease.

## Usage

```ts
import { useFileDialog } from '@vueuse/core'

const { files, openFileDialog } = useDialog()
```

```html
<template>
<button type="button" @click="openFileDialog">Choose file</button>
</template>
```
52 changes: 52 additions & 0 deletions packages/core/useFileDialog/index.ts
@@ -0,0 +1,52 @@
import { readonly, ref } from 'vue-demi'

export interface UseFileDialogOptions {
okxiaoliang4 marked this conversation as resolved.
Show resolved Hide resolved
multiple?: boolean
accept?: string
}
okxiaoliang4 marked this conversation as resolved.
Show resolved Hide resolved

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 open = (localOptions?: Partial<UseFileDialogOptions>) => {
const _options = {
...DEFAULT_OPTIONS,
...options,
...localOptions,
}
const input = document.createElement('input')
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to move to the useFileDialog scope? I'm worried that using the open function multiple times will create too many input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved outside useFileDialog now. Thanks!

input.type = 'file'
input.multiple = _options.multiple!
input.accept = _options.accept!

okxiaoliang4 marked this conversation as resolved.
Show resolved Hide resolved
input.onchange = (event: Event) => {
const result = event.target as HTMLInputElement
files.value = result.files
}

input.click()
}

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

return {
files: readonly(files),
Copy link
Member

Choose a reason for hiding this comment

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

Since it's readonly, we might need an extra function reset

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, a reset function would be needed

open,
reset,
}
}

export type UseFileDialogReturn = ReturnType<typeof useFileDialog>