Skip to content

Commit 30452c2

Browse files
authored
feat(useFileDialog): return onCancel handler (#4184)
1 parent c540701 commit 30452c2

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

packages/core/useFileDialog/demo.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<script setup lang="ts">
22
import { useFileDialog } from '.'
33
4-
const { files, open, reset, onChange } = useFileDialog()
4+
const { files, open, reset, onCancel, onChange } = useFileDialog()
55
onChange((files) => {
66
/** do something with files */
77
})
8+
9+
onCancel(() => {
10+
/** do something on cancel */
11+
})
812
</script>
913

1014
<template>

packages/core/useFileDialog/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ Open file dialog with ease.
1111
```ts
1212
import { useFileDialog } from '@vueuse/core'
1313

14-
const { files, open, reset, onChange } = useFileDialog({
14+
const { files, open, reset, onCancel, onChange } = useFileDialog({
1515
accept: 'image/*', // Set to accept only image files
1616
directory: true, // Select directories instead of files if set true
1717
})
1818

1919
onChange((files) => {
2020
/** do something with files */
2121
})
22+
23+
onCancel(() => {
24+
/** do something on cancel */
25+
})
2226
```
2327

2428
```vue

packages/core/useFileDialog/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface UseFileDialogReturn {
4343
open: (localOptions?: Partial<UseFileDialogOptions>) => void
4444
reset: () => void
4545
onChange: EventHookOn<FileList | null>
46+
onCancel: EventHookOn
4647
}
4748

4849
/**
@@ -57,7 +58,8 @@ export function useFileDialog(options: UseFileDialogOptions = {}): UseFileDialog
5758
} = options
5859

5960
const files = ref<FileList | null>(null)
60-
const { on: onChange, trigger } = createEventHook()
61+
const { on: onChange, trigger: changeTrigger } = createEventHook()
62+
const { on: onCancel, trigger: cancelTrigger } = createEventHook()
6163
let input: HTMLInputElement | undefined
6264
if (document) {
6365
input = document.createElement('input')
@@ -66,15 +68,19 @@ export function useFileDialog(options: UseFileDialogOptions = {}): UseFileDialog
6668
input.onchange = (event: Event) => {
6769
const result = event.target as HTMLInputElement
6870
files.value = result.files
69-
trigger(files.value)
71+
changeTrigger(files.value)
72+
}
73+
74+
input.oncancel = () => {
75+
cancelTrigger()
7076
}
7177
}
7278

7379
const reset = () => {
7480
files.value = null
7581
if (input && input.value) {
7682
input.value = ''
77-
trigger(null)
83+
changeTrigger(null)
7884
}
7985
}
8086

@@ -101,6 +107,7 @@ export function useFileDialog(options: UseFileDialogOptions = {}): UseFileDialog
101107
files: readonly(files),
102108
open,
103109
reset,
110+
onCancel,
104111
onChange,
105112
}
106113
}

0 commit comments

Comments
 (0)