-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update src/components/NcDialogButton/NcDialogButton.vue
Co-authored-by: Grigorii K. Shartsev <[email protected]> Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
2 changed files
with
116 additions
and
108 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,8 @@ Sometimes a dialog ends with a request and this request might fail due to server | |
In this case it is often desired to keep the dialog open, this can be done by returning `false` from the button callback, | ||
to not block this callback should return a `Promise<false>`. | ||
|
||
It is also possible to get the result of the callback from the dialog, as the result is passed as the payload of the `closing` event. | ||
|
||
While the promise is awaited the button will have a loading state, | ||
this means, as long as no custom `icon`-slot is used, a loading icon will be shown. | ||
Please note that the **button will not be disabled or accessibility reasons**, | ||
|
@@ -141,12 +143,14 @@ because disabled elements cannot be focused and so the loading state could not b | |
```vue | ||
<template> | ||
<div> | ||
<NcButton @click="showDialog = true">Show dialog</NcButton> | ||
<NcButton @click="openDialog">Show dialog</NcButton> | ||
<NcDialog :buttons="buttons" | ||
name="Create user" | ||
:message="message" | ||
:open.sync="showDialog" | ||
@closing="response = $event" | ||
@update:open="clickClosesDialog = false" /> | ||
<div style="margin-top: 8px;">Dialog response: {{ response }}</div> | ||
</div> | ||
</template> | ||
<script> | ||
|
@@ -155,26 +159,37 @@ export default { | |
return { | ||
showDialog: false, | ||
clickClosesDialog: false, | ||
response: 'none', | ||
} | ||
}, | ||
methods: { | ||
async callback() { | ||
// wait 3 seconds | ||
await new Promise((resolve) => window.setTimeout(resolve, 3000)) | ||
this.clickClosesDialog = !this.clickClosesDialog | ||
console.warn(this.clickClosesDialog) | ||
// Do not close the dialog on first and then every second button click | ||
if (this.clickClosesDialog) { | ||
// return false means the dialog stays open | ||
return false | ||
} | ||
return '✅' | ||
}, | ||
openDialog() { | ||
this.response = 'none' | ||
this.showDialog = true | ||
}, | ||
}, | ||
computed: { | ||
buttons() { | ||
return [ | ||
{ | ||
label: 'Create user', | ||
type: 'primary', | ||
callback: async () => { | ||
// wait 3 seconds | ||
await new Promise((resolve) => window.setTimeout(resolve, 3000)) | ||
this.clickClosesDialog = !this.clickClosesDialog | ||
console.warn(this.clickClosesDialog) | ||
// Do not close the dialog on first and then every second button click | ||
if (this.clickClosesDialog) { | ||
// return false means the dialog stays open | ||
return false | ||
} | ||
}, | ||
callback: this.callback, | ||
} | ||
] | ||
}, | ||
|
@@ -198,7 +213,7 @@ export default { | |
:enable-swipe="false" | ||
v-bind="modalProps" | ||
@close="handleClosed" | ||
@update:show="handleClosing"> | ||
@update:show="handleClosing()"> | ||
<!-- The dialog name / header --> | ||
<h2 :id="navigationId" class="dialog__name" v-text="name" /> | ||
<component :is="dialogTagName" | ||
|
@@ -507,25 +522,28 @@ export default defineComponent({ | |
// Because NcModal does not emit `close` when show prop is changed | ||
/** | ||
* Handle clicking a dialog button -> should close | ||
* @param {MouseEvent} event The click event | ||
* @param {unknown} result Result of the callback function | ||
*/ | ||
const handleButtonClose = () => { | ||
const handleButtonClose = (event, result) => { | ||
// Skip close if invalid dialog | ||
if (dialogTagName.value === 'form' && !dialogElement.value.reportValidity()) { | ||
return | ||
} | ||
handleClosing() | ||
handleClosing(result) | ||
window.setTimeout(() => handleClosed(), 300) | ||
} | ||
/** | ||
Check warning on line 537 in src/components/NcDialog/NcDialog.vue GitHub Actions / NPM lint
|
||
* Handle closing the dialog, optional out transition did not run yet | ||
*/ | ||
const handleClosing = () => { | ||
const handleClosing = (result) => { | ||
showModal.value = false | ||
/** | ||
* Emitted when the dialog is closing, so the out transition did not finish yet | ||
* Emitted when the dialog is closing, so the out transition did not finish yet. | ||
* @param result The result of the button callback (`undefined` if closing because of clicking the 'close'-button) | ||
*/ | ||
emit('closing') | ||
emit('closing', result) | ||
} | ||
/** | ||
|
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