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 <[email protected]>
  • Loading branch information
susnux committed Aug 21, 2024
1 parent f84ff80 commit f43e99d
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 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="currentName = ''"
@closing="newName = ''">
<NcTextField label="New name"
placeholder="Min. 6 characters"
Expand All @@ -114,8 +115,14 @@ export default {
return {
showDialog: false,
newName: '',
currentName: 'none yet.',
currentName: 'non yet.',
buttons: [
{
label: 'Reset',
nativeType: 'reset',
// returning `false` to prevent the dialog from closing
callback: () => false,
},
{
label: 'Submit',
type: 'primary',
Expand Down Expand Up @@ -169,7 +176,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 @@ -300,7 +307,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 @@ -344,7 +351,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 @@ -354,7 +361,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 @@ -434,6 +441,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 453 in src/components/NcDialog/NcDialog.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
}
: {},
)
Expand All @@ -447,9 +464,11 @@ export default defineComponent({
/**

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

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "button" declaration
* Handle clicking a dialog button -> should close
*/
const handleButtonClose = () => {
// 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()
Expand Down

0 comments on commit f43e99d

Please sign in to comment.