|
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 | 299 |
|
300 | | - if (e.key === 'Enter' || e.key === 'Tab') { |
301 | | - e.stopPropagation() |
302 | 300 |
|
303 | | - this.emitEnter() |
304 | | - this.reverse = false |
305 | | - } |
306 | | - }, |
307 | | -
|
308 | | - onBackKeyListener(e) { |
309 | | - if (e.shiftKey && e.key === 'Tab' ) { |
| 301 | + if (e.shiftKey) { |
310 | 302 | e.stopPropagation() |
311 | 303 | e.preventDefault() |
| 304 | +
|
312 | 305 | this.goToPreviousQuestion() |
| 306 | + } else { |
| 307 | + e.preventDefault() |
| 308 | + |
| 309 | + const q = this.activeQuestionComponent() |
| 310 | +
|
| 311 | + if (q.shouldFocus()) { |
| 312 | + q.focusField() |
| 313 | + } else { |
| 314 | + e.stopPropagation() |
| 315 | +
|
| 316 | + this.emitTab() |
| 317 | + this.reverse = false |
| 318 | + } |
313 | 319 | } |
314 | 320 | }, |
315 | 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 | + } |
| 335 | +
|
| 336 | + e.stopPropagation() |
| 337 | + this.reverse = false |
| 338 | + } |
| 339 | + }, |
| 340 | +
|
316 | 341 | emitEnter() { |
317 | 342 | const q = this.activeQuestionComponent() |
318 | 343 |
|
|
325 | 350 | } |
326 | 351 | }, |
327 | 352 |
|
| 353 | + emitTab() { |
| 354 | + const q = this.activeQuestionComponent() |
| 355 | +
|
| 356 | + if (q) { |
| 357 | + // Send tab event to the current question component |
| 358 | + q.onTab() |
| 359 | + } else { |
| 360 | + this.emitEnter() |
| 361 | + } |
| 362 | + }, |
| 363 | +
|
328 | 364 | submit() { |
329 | 365 | this.emitSubmit() |
330 | 366 | this.submitted = true |
|
343 | 379 | * can jump to it. |
344 | 380 | */ |
345 | 381 | isNextQuestionAvailable() { |
| 382 | + if (this.submitted) { |
| 383 | + return false |
| 384 | + } |
| 385 | +
|
346 | 386 | const q = this.activeQuestion |
347 | 387 | |
348 | 388 | if (q && !q.required) { |
|
0 commit comments