@@ -186,26 +186,38 @@ called synchronously, as async is not supported.
186
186
``` html
187
187
<template >
188
188
<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 >
190
190
191
- <!-- Main UI -->
192
- <div class =" mt-3 mb-3" >
191
+ <div class =" mt-3" >
193
192
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 >
196
196
</ul >
197
197
</div >
198
198
199
- <!-- Modal Component -->
200
199
<b-modal
201
- id =" modal-prevent"
200
+ id =" modal-prevent-closing "
202
201
ref =" modal"
203
- title =" Submit your name"
202
+ title =" Submit Your Name"
203
+ @show =" resetModal"
204
+ @hidden =" resetModal"
204
205
@ok =" handleOk"
205
- @shown =" clearName"
206
206
>
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 >
209
221
</form >
210
222
</b-modal >
211
223
</div >
@@ -216,27 +228,35 @@ called synchronously, as async is not supported.
216
228
data () {
217
229
return {
218
230
name: ' ' ,
219
- names: []
231
+ nameState: null ,
232
+ submittedNames: []
220
233
}
221
234
},
222
235
methods: {
223
- clearName () {
236
+ checkFormValidity () {
237
+ const valid = this .$refs .form .checkValidity ()
238
+ this .nameState = valid ? ' valid' : ' invalid'
239
+ return valid
240
+ },
241
+ resetModal () {
224
242
this .name = ' '
243
+ this .nameState = null
225
244
},
226
245
handleOk (bvModalEvt ) {
227
246
// Prevent modal from closing
228
247
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 ()
234
250
},
235
251
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
238
259
this .$nextTick (() => {
239
- // Wrapped in $nextTick to ensure DOM is rendered before closing
240
260
this .$refs .modal .hide ()
241
261
})
242
262
}
0 commit comments