Skip to content

Commit 7082769

Browse files
author
Pooya Parsa
committed
Improve modals API
1 parent 642964d commit 7082769

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

docs/pages/docs/components/modals.vue

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@
2222
return {
2323
component: 'bModal',
2424
jsfiddle: 'bofh9aaa',
25+
events: [
26+
{
27+
event: 'shown',
28+
description: 'always emits when modal is shown'
29+
},
30+
{
31+
event: 'hidden',
32+
description: 'always emits when modal is hidden'
33+
},
34+
{
35+
event: 'ok',
36+
description: 'when default ok button pressed'
37+
},
38+
{
39+
event: 'cancel',
40+
description: 'when default cancel button pressed'
41+
}
42+
],
2543
slots: [
2644
{
2745
name: 'modal-header',

examples/modals/demo.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
<div id="app">
22
<b-btn @click="$root.$emit('show::modal','modal1')">Launch demo modal</b-btn>
33

4+
<!-- Main UI -->
45
<div class="mt-3 mb-3">
56
Submitted Names:
67
<ul>
78
<li v-for="n in names">{{n}}</li>
89
</ul>
910
</div>
1011

11-
<b-modal id="modal1" title="Submit your name" :on-save="onSave" save-title="Submit">
12-
<b-form-input type="text" placeholder="Enter your name" v-model="name"></b-form-input>
12+
<!-- Modal Component -->
13+
<b-modal id="modal1"
14+
title="Submit your name"
15+
@ok="submit"
16+
@shown="clearName"
17+
>
18+
<b-form-input type="text"
19+
placeholder="Enter your name" v-model="name"
20+
></b-form-input>
1321
</b-modal>
22+
1423
</div>

examples/modals/demo.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ window.app = new Vue({
55
names: []
66
},
77
methods: {
8-
onSave() {
8+
clearName() {
9+
this.name = "";
10+
},
11+
submit() {
912
if (!this.name) {
1013
/* eslint-disable no-alert */
1114
alert('Please enter your name');

lib/components/modal.vue

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<div key="modal" :id="id"
1212
v-show="visible"
1313
:class="['modal',{fade :fade}]"
14-
@click="onClickOut($event)">
14+
@click="onClickOut($event)"
15+
>
1516

1617
<div :class="['modal-dialog','modal-'+size]">
1718
<div class="modal-content">
@@ -33,8 +34,8 @@
3334

3435
<div class="modal-footer" v-if="!hideFooter">
3536
<slot name="modal-footer">
36-
<b-btn variant="secondary" @click="$close">{{closeTitle}}</b-btn>
37-
<b-btn variant="primary" @click="$save">{{saveTitle}}</b-btn>
37+
<b-btn variant="secondary" @click="hide(false)">{{closeTitle}}</b-btn>
38+
<b-btn variant="primary" @click="hide(true)">{{okTitle}}</b-btn>
3839
</slot>
3940
</div>
4041

@@ -54,9 +55,8 @@
5455
5556
/* Make modal display as block instead of inline style, and because Vue's v-show deletes inline "display" style*/
5657
.modal {
57-
display:block;
58+
display: block;
5859
}
59-
6060
</style>
6161

6262
<script>
@@ -80,7 +80,7 @@
8080
},
8181
title: {
8282
type: String,
83-
default: 'Save'
83+
default: ''
8484
},
8585
size: {
8686
type: String,
@@ -94,18 +94,10 @@
9494
type: String,
9595
default: 'Close'
9696
},
97-
onClose: {
98-
type: Function,
99-
default: null
100-
},
101-
saveTitle: {
97+
okTitle: {
10298
type: String,
10399
default: 'OK'
104100
},
105-
onSave: {
106-
type: Function,
107-
default: null
108-
},
109101
closeOnBackdrop: {
110102
type: Boolean,
111103
default: true
@@ -127,29 +119,23 @@
127119
this.visible = true;
128120
this.$root.$emit('shown::modal', this.id);
129121
this.body.classList.add('modal-open');
122+
this.$emit('shown');
130123
},
131-
hide() {
124+
hide(isOK) {
132125
if (!this.visible) {
133126
return;
134127
}
135128
this.visible = false;
136129
this.$root.$emit('hidden::modal', this.id);
137130
this.body.classList.remove('modal-open');
138-
},
139-
$save() {
140-
if (this.onSave) {
141-
if (this.onSave() === false) {
142-
// Cancel event
143-
return;
144-
}
145-
}
146-
this.hide();
147-
},
148-
$close() {
149-
if (this.onClose) {
150-
this.onClose();
131+
132+
this.$emit('hidden', isOK);
133+
134+
if (isOK === true) {
135+
this.$emit('ok');
136+
} else if (isOK === false) {
137+
this.$emit('cancel');
151138
}
152-
this.hide();
153139
},
154140
onClickOut(e) {
155141
// If backdrop clicked, hide modal

0 commit comments

Comments
 (0)