Skip to content

Commit cf073ea

Browse files
committed
Return focus into the input field with tab and enter
1 parent c2eef29 commit cf073ea

File tree

6 files changed

+83
-16
lines changed

6 files changed

+83
-16
lines changed

src/components/FlowForm.vue

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,23 @@
292292
/**
293293
* Global key listener, listens for Enter or Tab key events.
294294
*/
295-
onKeyListener(e) {
295+
onKeyListener(e) {
296296
if (e.shiftKey) {
297297
return
298298
}
299+
if (e.key === 'Enter'|| e.key === 'Tab') {
300+
const q = this.activeQuestionComponent()
299301
300-
if (e.key === 'Enter' || e.key === 'Tab') {
301-
e.stopPropagation()
302+
if(!q.getFocus() && q.getCanReceiveFocus()){
303+
q.focusField()
304+
return
305+
}
302306
303-
this.emitEnter()
307+
if(e.key === 'Enter') {
308+
this.emitEnter()
309+
}
310+
311+
e.stopPropagation()
304312
this.reverse = false
305313
}
306314
},
@@ -311,6 +319,20 @@
311319
e.preventDefault()
312320
this.goToPreviousQuestion()
313321
}
322+
323+
if (e.key === 'Tab') {
324+
e.preventDefault()
325+
const q = this.activeQuestionComponent()
326+
327+
if(!q.getFocus() && q.getCanReceiveFocus()){
328+
q.focusField()
329+
return
330+
}
331+
332+
this.emitTab()
333+
e.stopPropagation()
334+
this.reverse = false
335+
}
314336
},
315337
316338
emitEnter() {
@@ -325,6 +347,18 @@
325347
}
326348
},
327349
350+
emitTab() {
351+
const q = this.activeQuestionComponent()
352+
353+
if (q) {
354+
// Send tab event to the current question component
355+
q.onTab()
356+
} else if (this.completed && this.isOnLastStep) {
357+
// We're finished - submit form
358+
this.submit()
359+
}
360+
},
361+
328362
submit() {
329363
this.emitSubmit()
330364
this.submitted = true

src/components/Question.vue

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,45 @@
153153
this.focusField()
154154
},
155155
156+
returnFocus(){
157+
const q = this.$refs.questionComponent
158+
if(!q.focused && q.canReceiveFocus){
159+
this.focusField()
160+
return
161+
}
162+
},
163+
156164
/**
157165
* Emits "answer" event and calls "onEnter" method on Enter press
158166
*/
159167
onEnter($event) {
160168
const q = this.$refs.questionComponent
161-
162-
if (q) {
163-
if (!q.focused) {
169+
if (this.question.type === QuestionType.LongText){
170+
if(!q.focused){
164171
this.$emit('answer', q)
165172
}
166-
q.onEnter()
167-
}
173+
q.onEnter()
174+
}
175+
176+
this.returnFocus()
177+
this.$emit('answer', q)
178+
q.onEnter()
168179
},
169180
181+
onTab($event) {
182+
const q = this.$refs.questionComponent
183+
this.returnFocus()
184+
this.$emit('answer', q)
185+
q.onEnter()
186+
},
187+
188+
getFocus(){
189+
return this.$refs.questionComponent.focused
190+
},
191+
192+
getCanReceiveFocus(){
193+
return this.$refs.questionComponent.canReceiveFocus
194+
},
170195
/**
171196
* Check if the "OK" button should be shown.
172197
*/

src/components/QuestionTypes/BaseType.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
enterPressed: false,
3131
allowedChars: null,
3232
alwaysAllowedKeys: ['ArrowLeft', 'ArrowRight', 'Delete', 'Backspace'],
33-
focused: false
33+
focused: false,
34+
canReceiveFocus: false,
3435
}
3536
},
3637
mounted() {
@@ -85,10 +86,10 @@
8586
this.enterPressed = false
8687
clearTimeout(this.timeoutId)
8788
88-
if ($event) {
89-
if ($event.key === 'Enter') {
90-
this.unsetFocus()
91-
}
89+
if ($event) {
90+
/* if ($event.key === 'Enter') {
91+
this.unsetFocus()
92+
} */
9293
9394
if (this.allowedChars !== null) {
9495
// Check if the entered character is allowed.

src/components/QuestionTypes/LongTextType.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
components: {
3636
TextareaAutosize
3737
},
38+
data () {
39+
return {
40+
canReceiveFocus: true,
41+
}
42+
},
3843
mounted() {
3944
window.addEventListener('resize', this.onResizeListener)
4045
},

src/components/QuestionTypes/PhoneType.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
name: QuestionType.Phone,
3636
data() {
3737
return {
38-
inputType: 'tel'
38+
inputType: 'tel',
39+
canReceiveFocus: true,
3940
}
4041
}
4142
}

src/components/QuestionTypes/TextType.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
name: QuestionType.Text,
3131
data() {
3232
return {
33-
inputType: 'text'
33+
inputType: 'text',
34+
canReceiveFocus: true,
3435
}
3536
}
3637
}

0 commit comments

Comments
 (0)