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

fix(FilePicker): Stop default close event in case of button press #1207

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/components/FilePicker/FilePicker.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<NcDialog v-bind="dialogProps" @update:open="handleClose">
<NcDialog v-bind="dialogProps" :open.sync="isOpen" @update:open="handleClose">
<template #navigation="{ isCollapsed }">
<FilePickerNavigation :is-collapsed="isCollapsed" :current-view.sync="currentView" :filter-string.sync="filterString" />
</template>
Expand Down Expand Up @@ -120,6 +120,8 @@ const emit = defineEmits<{
(e: 'close', v?: Node[]): void
}>()

const isOpen = ref(true)

/**
* Props to be passed to the underlying Dialog component
*/
Expand All @@ -143,14 +145,28 @@ const dialogButtons = computed(() => {

return buttons.map((button) => ({
...button,
callback: async () => {
const nodes = selectedFiles.value.length === 0 && props.allowPickDirectory ? [await getFile(currentPath.value)] : selectedFiles.value as Node[]
button.callback(nodes)
emit('close', nodes)
callback: () => {
// lock default close handling
isHandlingCallback = true
handleButtonClick(button.callback)
},
} as IFilePickerButton))
})

/**
* Flag that is set when a button was clicked to prevent the default close event to be emitted
* This is needed as `handleButtonClick` is async and thus might execute after NcDialog already closed
*/
let isHandlingCallback = false

const handleButtonClick = async (callback: IFilePickerButton['callback']) => {
const nodes = selectedFiles.value.length === 0 && props.allowPickDirectory ? [await getFile(currentPath.value)] : selectedFiles.value as Node[]
callback(nodes)
emit('close', nodes)
// Unlock close
isHandlingCallback = false
}

/**
* Name of the currently active view
*/
Expand Down Expand Up @@ -272,7 +288,7 @@ const onCreateFolder = async (name: string) => {
* @param open If the dialog is open
*/
const handleClose = (open: boolean) => {
if (!open) {
if (!open && !isHandlingCallback) {
emit('close')
}
}
Expand Down
Loading