|
144 | 144 | } |
145 | 145 | }, |
146 | 146 | mounted() { |
147 | | - document.addEventListener('keyup', this.onKeyListener, true) |
148 | | - document.addEventListener('keydown', this.onBackKeyListener) |
| 147 | + document.addEventListener('keydown', this.onKeyDownListener) |
| 148 | + document.addEventListener('keyup', this.onKeyUpListener, true) |
149 | 149 | window.addEventListener('beforeunload', this.onBeforeUnload) |
150 | 150 |
|
151 | 151 | this.setQuestions() |
152 | 152 | }, |
153 | 153 | beforeDestroy() { |
154 | | - document.removeEventListener('keyup', this.onKeyListener, true) |
155 | | - document.removeEventListener('keydown', this.onBackKeyListener) |
| 154 | + document.removeEventListener('keydown', this.onKeyDownListener) |
| 155 | + document.removeEventListener('keyup', this.onKeyUpListener, true) |
156 | 156 | window.removeEventListener('beforeunload', this.onBeforeUnload) |
157 | 157 | }, |
158 | 158 | computed: { |
|
290 | 290 | }, |
291 | 291 |
|
292 | 292 | /** |
293 | | - * Global key listener, listens for Enter or Tab key events. |
| 293 | + * Global key listeners, listen for Enter or Tab key events. |
294 | 294 | */ |
295 | | - onKeyListener(e) { |
296 | | - if (e.shiftKey) { |
| 295 | + onKeyDownListener(e) { |
| 296 | + if (e.key !== 'Tab' || this.submitted) { |
297 | 297 | return |
298 | 298 | } |
299 | | - if (e.key === 'Enter'|| e.key === 'Tab') { |
300 | | - const q = this.activeQuestionComponent() |
301 | 299 |
|
302 | | - if(!q.getFocus() && q.getCanReceiveFocus()){ |
303 | | - q.focusField() |
304 | | - return |
305 | | - } |
306 | 300 |
|
307 | | - if(e.key === 'Enter') { |
308 | | - this.emitEnter() |
309 | | - } |
310 | | -
|
311 | | - e.stopPropagation() |
312 | | - this.reverse = false |
313 | | - } |
314 | | - }, |
315 | | -
|
316 | | - onBackKeyListener(e) { |
317 | | - if (e.shiftKey && e.key === 'Tab' ) { |
| 301 | + if (e.shiftKey) { |
318 | 302 | e.stopPropagation() |
319 | 303 | e.preventDefault() |
320 | | - this.goToPreviousQuestion() |
321 | | - } |
322 | 304 |
|
323 | | - if (e.key === 'Tab') { |
| 305 | + this.goToPreviousQuestion() |
| 306 | + } else { |
324 | 307 | e.preventDefault() |
| 308 | + |
325 | 309 | const q = this.activeQuestionComponent() |
326 | 310 |
|
327 | | - if(!q.getFocus() && q.getCanReceiveFocus()){ |
| 311 | + if (q.shouldFocus()) { |
328 | 312 | q.focusField() |
329 | | - return |
| 313 | + } else { |
| 314 | + e.stopPropagation() |
| 315 | +
|
| 316 | + this.emitTab() |
| 317 | + this.reverse = false |
330 | 318 | } |
| 319 | + } |
| 320 | + }, |
| 321 | +
|
| 322 | + onKeyUpListener(e) { |
| 323 | + if (e.shiftKey || ['Tab', 'Enter'].indexOf(e.key) === -1 || this.submitted) { |
| 324 | + return |
| 325 | + } |
| 326 | +
|
| 327 | + const q = this.activeQuestionComponent() |
| 328 | +
|
| 329 | + if (e.key === 'Tab' && q.shouldFocus()) { |
| 330 | + q.focusField() |
| 331 | + } else { |
| 332 | + if (e.key === 'Enter') { |
| 333 | + this.emitEnter() |
| 334 | + } |
331 | 335 |
|
332 | | - this.emitTab() |
333 | 336 | e.stopPropagation() |
334 | 337 | this.reverse = false |
335 | 338 | } |
336 | | - }, |
| 339 | + }, |
337 | 340 |
|
338 | 341 | emitEnter() { |
339 | 342 | const q = this.activeQuestionComponent() |
|
353 | 356 | if (q) { |
354 | 357 | // Send tab event to the current question component |
355 | 358 | q.onTab() |
356 | | - } else if (this.completed && this.isOnLastStep) { |
357 | | - // We're finished - submit form |
358 | | - this.submit() |
| 359 | + } else { |
| 360 | + this.emitEnter() |
359 | 361 | } |
360 | 362 | }, |
361 | 363 |
|
|
377 | 379 | * can jump to it. |
378 | 380 | */ |
379 | 381 | isNextQuestionAvailable() { |
| 382 | + if (this.submitted) { |
| 383 | + return false |
| 384 | + } |
| 385 | +
|
380 | 386 | const q = this.activeQuestion |
381 | 387 | |
382 | 388 | if (q && !q.required) { |
|
0 commit comments