Skip to content

Commit 7256827

Browse files
John-Goffstasson
authored andcommitted
feat(dialog): Add validation functionality for cancel event
Copies the validation for accept button clicks and applies it to cancel. Useful to prevent the dialog from closing when using the dialog for more complex actions.
1 parent 417e7c1 commit 7256827

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

components/dialog/README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,18 @@ var vm = new Vue({
4242
4343
### events
4444

45-
| props | args | Description |
46-
| ----------- | ------- | ------------------------------------------------------- |
47-
| `@change` | Boolean | notify v-model/listeners that drawer state has changed. |
48-
| `@accept` | none | emitted when dialog is accepted |
49-
| `@cancel` | none | emitted when dialog is cancelled |
50-
| `@validate` | accept | emmited before the dialog is accepted _(\*)_ |
51-
52-
> Note that if you listen to the @validate event, then You must call
53-
> the accept argument to finally close the box. Use `accept(false)` to
54-
> prevent emitting the `accept` event and just close.
45+
| props | args | Description |
46+
| ----------------- | ------- | ------------------------------------------------------- |
47+
| `@change` | Boolean | notify v-model/listeners that drawer state has changed. |
48+
| `@accept` | none | emitted when dialog is accepted |
49+
| `@cancel` | none | emitted when dialog is cancelled |
50+
| `@validate` | accept | emitted before the dialog is accepted _(\*)_ |
51+
| `@validateCancel` | cancel | emitted before the dialog is cancelled _(\*)_ |
52+
53+
> Note that if you listen to the @validate or @validateCancel events, then You must call
54+
> the accept or cancel argument to finally close the box. Use `accept(false)` to
55+
> prevent emitting the `accept` event and just close, and `cancel(false)` to prevent emitting
56+
> the `cancel` event.
5557
5658
### Custom validation logic
5759

@@ -87,6 +89,29 @@ export default {
8789
};
8890
```
8991

92+
You can use `@validateCancel` to trigger validation logic for the cancel event, as follows:
93+
94+
```html
95+
<mdc-dialog ref="dialog" title="Dialog" accept="Accept" cancel="Decline"
96+
@validateCancel="onValidateCancel"
97+
>Lorem ipsum dolor sit amet</mdc-dialog>
98+
```
99+
100+
```javascript
101+
export default {
102+
methods: {
103+
onValidateCancel({ cancel }) {
104+
let isValid = false;
105+
// custom validation logic here
106+
// ..
107+
if (isValid) {
108+
cancel();
109+
}
110+
},
111+
},
112+
};
113+
```
114+
90115
### Reference
91116

92117
<https://material.io/components/web/catalog/dialogs>

components/dialog/demo.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
title="Dialog"
77
accept="Accept"
88
cancel="Decline"
9-
@validate="$event.accept(false)">
9+
@validate="$event.accept(false)"
10+
@validateCancel="$event.cancel(false)">
1011
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et
1112
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
1213
ea commodo consequat.

components/dialog/mdc-dialog.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,22 @@ export default {
151151
}
152152
},
153153
onCancel() {
154-
this.foundation.cancel(true)
154+
if (this.$listeners['validateCancel']) {
155+
this.$emit('validateCancel', {
156+
cancel: (notify = true) => {
157+
// if notify = false, the dialog will close
158+
// but the notifyAccept method will not be called
159+
// so we need to notify listeners the open state
160+
// is changing.
161+
if (!notify) {
162+
this.$emit('change', false)
163+
}
164+
this.foundation.cancel(notify)
165+
}
166+
})
167+
} else {
168+
this.foundation.cancel(true)
169+
}
155170
},
156171
onAccept() {
157172
if (this.$listeners['validate']) {

0 commit comments

Comments
 (0)