Skip to content

Commit

Permalink
feat(NcDialog): Allow to catch reset event
Browse files Browse the repository at this point in the history
Sometimes it is useful to also have a reset button,
we already support the native type `reset` so this allows to catch the `reset` event.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Feb 28, 2025
1 parent 9c25a15 commit 38f52bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
33 changes: 25 additions & 8 deletions src/components/NcDialog/NcDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Note that this is not possible if the dialog contains a navigation!
name="Choose a name"
:open.sync="showDialog"
@submit="currentName = newName"
@reset="newName = ''"
@closing="newName = ''">
<NcTextField label="New name"
placeholder="Min. 6 characters"
Expand All @@ -113,8 +114,12 @@ export default {
return {
showDialog: false,
newName: '',
currentName: 'none yet.',
currentName: 'non yet.',
buttons: [
{
label: 'Reset',
nativeType: 'reset',
},
{
label: 'Submit',
type: 'primary',
Expand Down Expand Up @@ -244,7 +249,7 @@ export default {
<NcDialogButton v-for="(button, idx) in buttons"
:key="idx"
v-bind="button"
@click="handleButtonClose" />
@click="handleButtonClose(button)" />
</slot>
</div>
</component>
Expand Down Expand Up @@ -375,7 +380,7 @@ export default defineComponent({
},

/**
* Optionally pass additionaly classes which will be set on the navigation for custom styling
* Optionally pass additional classes which will be set on the navigation for custom styling
* @default ''
* @example
* ```html
Expand Down Expand Up @@ -419,7 +424,7 @@ export default defineComponent({
},

/**
* Optionally pass additionaly classes which will be set on the content wrapper for custom styling
* Optionally pass additional classes which will be set on the content wrapper for custom styling
* @default ''
*/
contentClasses: {
Expand All @@ -429,7 +434,7 @@ export default defineComponent({
},

/**
* Optionally pass additionaly classes which will be set on the dialog itself
* Optionally pass additional classes which will be set on the dialog itself
* (the default `class` attribute will be set on the modal wrapper)
* @default ''
*/
Expand Down Expand Up @@ -509,6 +514,16 @@ export default defineComponent({
/** Forwarded HTMLFormElement submit event (only if `is-form` is set) */
emit('submit', event)
},
/**
* @param {Event} event Form submit event
*/
reset(event) {
event.preventDefault()
/**
* Forwarded HTMLFormElement reset event (only if `is-form` is set).
*/
emit('reset', event)
}

Check warning on line 526 in src/components/NcDialog/NcDialog.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
}
: {},
)
Expand All @@ -524,9 +539,11 @@ export default defineComponent({
* @param {MouseEvent} event The click event

Check warning on line 539 in src/components/NcDialog/NcDialog.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Expected @param names to be "button". Got "event, result"
* @param {unknown} result Result of the callback function
*/
const handleButtonClose = (event, result) => {
// Skip close if invalid dialog
if (dialogTagName.value === 'form' && !dialogElement.value.reportValidity()) {
const handleButtonClose = (button) => {
// Skip close on submit if invalid dialog
if (button.nativeType === 'submit'
&& dialogTagName.value === 'form'
&& !dialogElement.value.reportValidity()) {
return
}
handleClosing(result)

Check failure on line 549 in src/components/NcDialog/NcDialog.vue

View workflow job for this annotation

GitHub Actions / NPM lint

'result' is not defined
Expand Down
8 changes: 6 additions & 2 deletions src/components/NcDialogButton/NcDialogButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import { t } from '../../l10n.js'
const props = defineProps({
/**
* 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.
* If the function returns `false` the click is ignored and the dialog will not be closed,
* which is the default behavior of "reset"-buttons.
*
* @type {() => unknown|false|Promise<unknown|false>}
*/
callback: {
Expand Down Expand Up @@ -109,7 +111,9 @@ const handleClick = async (e) => {

isLoading.value = true
try {
const result = await props.callback?.()
// for reset buttons the default is "false"
const fallback = props.nativeType === 'reset' ? false : undefined
const result = await props.callback?.() ?? fallback
if (result !== false) {
/**
* The click event (`MouseEvent`) and the value returned by the callback
Expand Down

0 comments on commit 38f52bb

Please sign in to comment.