ability to add a new answer and focus

This commit is contained in:
tykayn 2019-08-10 18:31:15 +02:00
parent d413774d3b
commit 488b9f677a
5 changed files with 30 additions and 7 deletions

View File

@ -8,9 +8,10 @@
</p> </p>
<ol> <ol>
<li #answers *ngFor="let answer of config.answers; index as i;trackBy trackFunction" <li #answers *ngFor="let answer of config.answers; index as i;trackBy trackFunction"
class="anwser"> class="answer-item">
<input <input
type="text" type="text"
class="answer"
[(ngModel)]="answer.text" [(ngModel)]="answer.text"
placeholder="réponse" placeholder="réponse"
> >
@ -19,7 +20,7 @@
</ol> </ol>
<button <button
class="btn-outline btn-block" class="btn-outline btn-block"
(click)="config.answers.push({id:config.answers.length+1,text:''})"> (click)="addAnswer()">
Ajouter Ajouter
</button> </button>
<a [routerLink]="'/step/recapitulatif'" class="btn btn-block">Voyons ce que ça donne</a> <a [routerLink]="'/step/recapitulatif'" class="btn btn-block">Voyons ce que ça donne</a>

View File

@ -1,4 +1,4 @@
import {Component, OnInit} from '@angular/core'; import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {BaseComponent} from '../base-page/base.component'; import {BaseComponent} from '../base-page/base.component';
import {ConfigService} from '../../config.service'; import {ConfigService} from '../../config.service';
@ -9,9 +9,10 @@ import {ConfigService} from '../../config.service';
}) })
export class AnswersComponent extends BaseComponent implements OnInit { export class AnswersComponent extends BaseComponent implements OnInit {
private answerList=[]; private answerList = [];
constructor(config: ConfigService) { constructor(config: ConfigService,
private cd: ChangeDetectorRef) {
super(config); super(config);
this.answerList = this.config.answers; this.answerList = this.config.answers;
} }
@ -20,13 +21,26 @@ export class AnswersComponent extends BaseComponent implements OnInit {
ngOnInit() { ngOnInit() {
} }
trackFunction(index: number, item: any): number { return item.id; } trackFunction(index: number, item: any): number {
return item.id;
}
addAnswer() {
this.config.answers.push(
{
id: this.config.answers.length + 1,
text: ''
});
this.cd.detectChanges(); // to refresh the view before focusing on the new input
const AnswersDomToFocus = document.querySelectorAll('.answers .answer');
AnswersDomToFocus[AnswersDomToFocus.length - 1].focus();
}
// add a new answer on the press of ENTER in an input // add a new answer on the press of ENTER in an input
addWhenEnterKey(event) { addWhenEnterKey(event) {
console.log('event', event); console.log('event', event);
if (event.keyCode === 13) { if (event.keyCode === 13) {
this.config.addAnswer(); this.addAnswer();
} }
return; return;
} }

View File

@ -1,3 +1,5 @@
<h1 i18n> <h1 i18n>
Images Images
</h1> </h1>
<a [routerLink]="'/step/visibility'" class="btn btn-block">ok</a>

View File

@ -3,5 +3,8 @@
</h1> </h1>
<section> <section>
TODO TODO
<pre>
{{config |json}}
</pre>
</section> </section>
<a [routerLink]="'/step/end'" class="btn btn-block">C'est parfait!</a> <a [routerLink]="'/step/end'" class="btn btn-block">C'est parfait!</a>

View File

@ -36,6 +36,9 @@ textarea {
.btn-block { .btn-block {
display: block; display: block;
font-weight: 600;
font-size: 1.25em;
text-align: center;
} }
a { a {