Skip to content

Commit f609316

Browse files
authored
fix(docs): improve <b-modal> prevent closing example (#3054)
1 parent ba78e40 commit f609316

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

src/components/modal/README.md

+41-21
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,38 @@ called synchronously, as async is not supported.
186186
```html
187187
<template>
188188
<div>
189-
<b-button v-b-modal.modal-prevent>Launch demo modal</b-button>
189+
<b-button v-b-modal.modal-prevent-closing>Open Modal</b-button>
190190

191-
<!-- Main UI -->
192-
<div class="mt-3 mb-3">
191+
<div class="mt-3">
193192
Submitted Names:
194-
<ul>
195-
<li v-for="n in names">{{ n }}</li>
193+
<div v-if="submittedNames.length === 0">--</div>
194+
<ul v-else class="mb-0 pl-3">
195+
<li v-for="name in submittedNames">{{ name }}</li>
196196
</ul>
197197
</div>
198198

199-
<!-- Modal Component -->
200199
<b-modal
201-
id="modal-prevent"
200+
id="modal-prevent-closing"
202201
ref="modal"
203-
title="Submit your name"
202+
title="Submit Your Name"
203+
@show="resetModal"
204+
@hidden="resetModal"
204205
@ok="handleOk"
205-
@shown="clearName"
206206
>
207-
<form @submit.stop.prevent="handleSubmit">
208-
<b-form-input v-model="name" placeholder="Enter your name"></b-form-input>
207+
<form ref="form" @submit.stop.prevent="handleSubmit">
208+
<b-form-group
209+
:state="nameState"
210+
label="Name"
211+
label-for="name-input"
212+
invalid-feedback="Name is required"
213+
>
214+
<b-form-input
215+
id="name-input"
216+
v-model="name"
217+
:state="nameState"
218+
required
219+
></b-form-input>
220+
</b-form-group>
209221
</form>
210222
</b-modal>
211223
</div>
@@ -216,27 +228,35 @@ called synchronously, as async is not supported.
216228
data() {
217229
return {
218230
name: '',
219-
names: []
231+
nameState: null,
232+
submittedNames: []
220233
}
221234
},
222235
methods: {
223-
clearName() {
236+
checkFormValidity() {
237+
const valid = this.$refs.form.checkValidity()
238+
this.nameState = valid ? 'valid' : 'invalid'
239+
return valid
240+
},
241+
resetModal() {
224242
this.name = ''
243+
this.nameState = null
225244
},
226245
handleOk(bvModalEvt) {
227246
// Prevent modal from closing
228247
bvModalEvt.preventDefault()
229-
if (!this.name) {
230-
alert('Please enter your name')
231-
} else {
232-
this.handleSubmit()
233-
}
248+
// Trigger submit handler
249+
this.handleSubmit()
234250
},
235251
handleSubmit() {
236-
this.names.push(this.name)
237-
this.clearName()
252+
// Exit when the form isn't valid
253+
if (!this.checkFormValidity()) {
254+
return
255+
}
256+
// Push the name to submitted names
257+
this.submittedNames.push(this.name)
258+
// Hide the modal manually
238259
this.$nextTick(() => {
239-
// Wrapped in $nextTick to ensure DOM is rendered before closing
240260
this.$refs.modal.hide()
241261
})
242262
}

0 commit comments

Comments
 (0)