Skip to content

Commit 733e566

Browse files
committed
Tab and focus improvements
1 parent cf073ea commit 733e566

File tree

9 files changed

+81
-79
lines changed

9 files changed

+81
-79
lines changed

examples/questionnaire/Example.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@
229229
// we need to implement the "keyup" listener manually.
230230
231231
if ($event.key === 'Enter' && this.completed && !this.submitted) {
232-
// Set `submitted` to true so the form knows not to allow back/forward
233-
// navigation anymore.
234-
this.$refs.flowform.submitted = true
235232
this.onSendData()
236233
}
237234
},
@@ -250,6 +247,10 @@
250247
},
251248
252249
onSendData() {
250+
// Set `submitted` to true so the form knows not to allow back/forward
251+
// navigation anymore.
252+
this.$refs.flowform.submitted = true
253+
253254
this.submitted = true
254255
255256
/* eslint-disable-next-line no-unused-vars */

examples/quiz/Example.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,11 @@
321321
},
322322
323323
onQuizSubmit() {
324-
this.submitted = true
324+
// Set `submitted` to true so the form knows not to allow back/forward
325+
// navigation anymore.
326+
this.$refs.flowform.submitted = true
327+
328+
this.submitted = true
325329
this.calculateScore()
326330
}
327331
}

src/assets/css/common.css

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ span.faux-form {
275275
margin-top: 28px;
276276
}
277277

278-
span.f-sub + .f-answer.full-width, div.field-sectionbreaktype .f-answer {
279-
margin-top: 14px
278+
span.f-sub + .f-answer.full-width,
279+
div.field-sectionbreaktype .f-answer {
280+
margin-top: 16px
280281
}
281282

282283
span.f-empty {
@@ -349,7 +350,6 @@ h3,
349350
}
350351

351352
@media screen and (max-width:767px) {
352-
353353
h2,
354354
.fh2 {
355355
font-size: 2.2rem;
@@ -359,7 +359,6 @@ h3,
359359
}
360360

361361
@media screen and (max-width:479px), (max-height:400px) {
362-
363362
h2,
364363
.fh2 {
365364
font-size: 1.4rem;

src/components/FlowForm.vue

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@
144144
}
145145
},
146146
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)
149149
window.addEventListener('beforeunload', this.onBeforeUnload)
150150
151151
this.setQuestions()
152152
},
153153
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)
156156
window.removeEventListener('beforeunload', this.onBeforeUnload)
157157
},
158158
computed: {
@@ -290,50 +290,53 @@
290290
},
291291
292292
/**
293-
* Global key listener, listens for Enter or Tab key events.
293+
* Global key listeners, listen for Enter or Tab key events.
294294
*/
295-
onKeyListener(e) {
296-
if (e.shiftKey) {
295+
onKeyDownListener(e) {
296+
if (e.key !== 'Tab' || this.submitted) {
297297
return
298298
}
299-
if (e.key === 'Enter'|| e.key === 'Tab') {
300-
const q = this.activeQuestionComponent()
301299
302-
if(!q.getFocus() && q.getCanReceiveFocus()){
303-
q.focusField()
304-
return
305-
}
306300
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) {
318302
e.stopPropagation()
319303
e.preventDefault()
320-
this.goToPreviousQuestion()
321-
}
322304
323-
if (e.key === 'Tab') {
305+
this.goToPreviousQuestion()
306+
} else {
324307
e.preventDefault()
308+
325309
const q = this.activeQuestionComponent()
326310
327-
if(!q.getFocus() && q.getCanReceiveFocus()){
311+
if (q.shouldFocus()) {
328312
q.focusField()
329-
return
313+
} else {
314+
e.stopPropagation()
315+
316+
this.emitTab()
317+
this.reverse = false
330318
}
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+
}
331335
332-
this.emitTab()
333336
e.stopPropagation()
334337
this.reverse = false
335338
}
336-
},
339+
},
337340
338341
emitEnter() {
339342
const q = this.activeQuestionComponent()
@@ -353,9 +356,8 @@
353356
if (q) {
354357
// Send tab event to the current question component
355358
q.onTab()
356-
} else if (this.completed && this.isOnLastStep) {
357-
// We're finished - submit form
358-
this.submit()
359+
} else {
360+
this.emitEnter()
359361
}
360362
},
361363
@@ -377,6 +379,10 @@
377379
* can jump to it.
378380
*/
379381
isNextQuestionAvailable() {
382+
if (this.submitted) {
383+
return false
384+
}
385+
380386
const q = this.activeQuestion
381387
382388
if (q && !q.required) {

src/components/Question.vue

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,17 @@
153153
this.focusField()
154154
},
155155
156-
returnFocus(){
156+
shouldFocus() {
157157
const q = this.$refs.questionComponent
158-
if(!q.focused && q.canReceiveFocus){
158+
159+
return q && q.canReceiveFocus && !q.focused
160+
},
161+
162+
returnFocus() {
163+
const q = this.$refs.questionComponent
164+
165+
if (this.shouldFocus()) {
159166
this.focusField()
160-
return
161167
}
162168
},
163169
@@ -166,32 +172,27 @@
166172
*/
167173
onEnter($event) {
168174
const q = this.$refs.questionComponent
169-
if (this.question.type === QuestionType.LongText){
170-
if(!q.focused){
175+
176+
if (q) {
177+
if (!q.focused) {
171178
this.$emit('answer', q)
172179
}
173-
q.onEnter()
174-
}
175-
176-
this.returnFocus()
177-
this.$emit('answer', q)
178-
q.onEnter()
180+
181+
q.onEnter()
182+
}
179183
},
180184
181185
onTab($event) {
182186
const q = this.$refs.questionComponent
183-
this.returnFocus()
184-
this.$emit('answer', q)
185-
q.onEnter()
186-
},
187187
188-
getFocus(){
189-
return this.$refs.questionComponent.focused
190-
},
191-
192-
getCanReceiveFocus(){
193-
return this.$refs.questionComponent.canReceiveFocus
188+
if (q) {
189+
this.returnFocus()
190+
this.$emit('answer', q)
191+
192+
q.onEnter()
193+
}
194194
},
195+
195196
/**
196197
* Check if the "OK" button should be shown.
197198
*/

src/components/QuestionTypes/BaseType.vue

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
this.enterPressed = false
8787
clearTimeout(this.timeoutId)
8888
89-
if ($event) {
90-
/* if ($event.key === 'Enter') {
91-
this.unsetFocus()
92-
} */
89+
if ($event) {
90+
if ($event.key === 'Enter' && !$event.shiftKey) {
91+
this.unsetFocus()
92+
}
9393
9494
if (this.allowedChars !== null) {
9595
// Check if the entered character is allowed.
@@ -157,10 +157,6 @@
157157
}
158158
},
159159
computed: {
160-
editingFinished() {
161-
return true
162-
},
163-
164160
placeholder() {
165161
return this.question.placeholder || this.language.placeholder
166162
},

src/components/QuestionTypes/LongTextType.vue

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
},
3838
data () {
3939
return {
40-
canReceiveFocus: true,
40+
canReceiveFocus: true
4141
}
4242
},
4343
mounted() {
@@ -68,11 +68,6 @@
6868
this._onEnter()
6969
}
7070
}
71-
},
72-
computed: {
73-
editingFinished() {
74-
return !this.isMobile
75-
}
7671
}
7772
}
7873
</script>

src/components/QuestionTypes/PhoneType.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
data() {
3737
return {
3838
inputType: 'tel',
39-
canReceiveFocus: true,
39+
canReceiveFocus: true
4040
}
4141
}
4242
}

src/components/QuestionTypes/TextType.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
data() {
3232
return {
3333
inputType: 'text',
34-
canReceiveFocus: true,
34+
canReceiveFocus: true
3535
}
3636
}
3737
}

0 commit comments

Comments
 (0)