Skip to content

Commit

Permalink
feat(NcDialogButton): Allow to return false from callback to keep d…
Browse files Browse the repository at this point in the history
…ialog open

If the callback returns `false` then the click event will not be forwarded.
This could be usful if the click triggers a validation that fails and the user
should be able to fix the issue.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Aug 29, 2024
1 parent 47e15cd commit 642f137
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/components/NcDialogButton/NcDialogButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Dialog button component used by NcDialog in the actions slot to display the butt
<template #icon>
<!-- @slot Allow to set a custom icon for the button -->
<slot name="icon">
<NcIconSvgWrapper v-if="icon !== undefined" :svg="icon" />
<NcLoadingIcon v-if="isLoading" />
<NcIconSvgWrapper v-else-if="icon !== undefined" :svg="icon" />
</slot>
</template>
</NcButton>
Expand All @@ -27,19 +28,23 @@ Dialog button component used by NcDialog in the actions slot to display the butt
import { defineComponent } from 'vue'

Check failure on line 28 in src/components/NcDialogButton/NcDialogButton.vue

View workflow job for this annotation

GitHub Actions / NPM lint

'/home/runner/work/nextcloud-vue/nextcloud-vue/node_modules/vue/dist/vue.runtime.esm.js' imported multiple times
import NcButton from '../NcButton/index.js'
import NcIconSvgWrapper from '../NcIconSvgWrapper/index.js'
import NcLoadingIcon from '../NcLoadingIcon/index.js'
import { ref } from 'vue';

Check failure on line 32 in src/components/NcDialogButton/NcDialogButton.vue

View workflow job for this annotation

GitHub Actions / NPM lint

'/home/runner/work/nextcloud-vue/nextcloud-vue/node_modules/vue/dist/vue.runtime.esm.js' imported multiple times

Check failure on line 32 in src/components/NcDialogButton/NcDialogButton.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
export default defineComponent({
name: 'NcDialogButton',
components: {
NcButton,
NcIconSvgWrapper,
NcLoadingIcon,
},
props: {
/**
* The function that will be called when the button is pressed
* @type {() => void}
* The function that will be called when the button is pressed.
* If the function returns `false` the click is ignored and the dialog will not be closed.
* @type {() => void|false|Promise<void|false>}
*/
callback: {
type: Function,
Expand Down Expand Up @@ -99,16 +104,25 @@ export default defineComponent({
emits: ['click'],
setup(props, { emit }) {
const isLoading = ref(false)
/**
* Handle clicking the button
* @param {MouseEvent} e The click event
*/
const handleClick = (e) => {
props.callback?.()
emit('click', e)
const handleClick = async (e) => {
isLoading.value = true
try {
const response = await props.callback?.()
if (response !== false) {
emit('click', e)
}
} finally {
isLoading.value = false
}
}
return { handleClick }
return { handleClick, isLoading }
},
})
</script>

0 comments on commit 642f137

Please sign in to comment.