Skip to content

Commit c7d1a67

Browse files
authored
Merge pull request #36 from fagnerlima/v3.0.0-beta1
v3.0.0 beta1
2 parents e122a4f + da07891 commit c7d1a67

64 files changed

Lines changed: 343 additions & 312 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
<a name="3.0.0-beta1"></a>
6+
## [3.0.0-beta1](https://github.com/robust-team/angular-forms/compare/v2.1.0...v3.0.0-beta1) (2017-10-10)
7+
8+
### Changes
9+
10+
* **Answers**' type into **Questions** changed of *string* to **Answer** object.
11+
12+
### Bug Fixes
13+
14+
* Fixed **isPristine** and **isDirty** methods into **AngularFormsComponent**.
15+
516
<a name="2.1.0"></a>
617
## [2.1.0](https://github.com/robust-team/angular-forms/compare/v1.2.0...v2.1.0) (2017-09-28)
718

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Issue Stats](http://issuestats.com/github/robust-team/angular-forms/badge/pr?style=flat)](http://issuestats.com/github/robust-team/angular-forms)
99
[![Issue Stats](http://issuestats.com/github/robust-team/angular-forms/badge/issue?style=flat)](http://issuestats.com/github/robust-team/angular-forms)
1010

11-
A Simple Form Generator for Angular.
11+
A Robust Forms Generator for Angular.
1212

1313
## Table of Contents
1414

@@ -243,15 +243,15 @@ Example output:
243243
value: {
244244
"G-01": [
245245
{
246-
"Q-101": "Option 1",
247-
"Q-102": "123"
246+
"Q-101": { "value": "Option 1", "id": null },
247+
"Q-102": { "value": "123", "id": null }
248248
}
249249
],
250250
"G-02": {
251-
"Q-201": "Option 1",
252-
"Q-202": "Option 2",
253-
"Q-203": null,
254-
"Q-204": null
251+
"Q-201": { "value": "Option 1", "id": null },
252+
"Q-202": { "value": "Option 2", "id": null },
253+
"Q-203": { "value": null, "id": null },
254+
"Q-204": { "value": null, "id": null }
255255
}
256256
}
257257
}
@@ -494,6 +494,19 @@ Read more in [https://fagnerlima.github.io/ng-mask/](https://fagnerlima.github.i
494494
}
495495
```
496496

497+
### Answer
498+
499+
**Answer** represents the question's answer.
500+
501+
```json
502+
{
503+
"value": "My answer",
504+
"id": 1
505+
}
506+
```
507+
508+
The **value** property represents the user's answer itself, while the **id** property is the answer's *identifier* into *database*, for example.
509+
497510
## Validations
498511

499512
**Validations** can be used in **Question** and **DataTable**, which can be of seven types:

demo/demo.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild } from '
22

33
import { DemoService } from './demo.service';
44
import { AngularFormsComponent } from '../src';
5-
import { Fieldset } from '../src/group/fieldset';
5+
import { Fieldset } from '../src/group';
66

77
@Component({
88
selector: 'rb-demo-app',

demo/demo.service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const groups: any[] = [
3939
'placeholder': 'Select',
4040
'validations': [
4141
{ 'type': 'required', 'message': 'Required field.' }
42-
]
42+
],
43+
'answer': { 'value': 'op-2', 'id': 1 }
4344
},
4445
{
4546
'name': 'Q-0103',
@@ -165,7 +166,7 @@ const groups: any[] = [
165166
],
166167
'options': ['Option 1', 'Option 2', 'Option 3'],
167168
'placeholder': 'Select',
168-
'answer': 'Option 2'
169+
'answer': { 'value': 'Option 2', 'id': 1 }
169170
},
170171
{
171172
'name': 'Q-0202',
@@ -175,7 +176,7 @@ const groups: any[] = [
175176
{ 'type': 'required', 'message': 'Required field.' }
176177
],
177178
'placeholder': 'Text Question 0202',
178-
'answer': 'Answer 0202'
179+
'answer': { 'value': 'Answer 0202', 'id': 1 }
179180
}
180181
],
181182
[
@@ -188,7 +189,7 @@ const groups: any[] = [
188189
],
189190
'options': ['Option 1', 'Option 2', 'Option 3'],
190191
'placeholder': 'Select',
191-
'answer': 'Option 3'
192+
'answer': { 'value': 'Option 3', 'id': 2 }
192193
},
193194
{
194195
'name': 'Q-0202',
@@ -198,7 +199,7 @@ const groups: any[] = [
198199
{ 'type': 'required', 'message': 'Required field.' }
199200
],
200201
'placeholder': 'Text Question 0202',
201-
'answer': 'Answer 0202 2'
202+
'answer': { 'value': 'Answer 0202 2', 'id': 2 }
202203
}
203204
]
204205
]

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@robust-team/angular-forms",
3-
"version": "2.1.0",
3+
"version": "3.0.0-beta1",
44
"license": "MIT",
55
"description": "A Simple Angular Forms Generator",
66
"main": "./bundles/angular-forms.umd.js",

src/angular-forms.component.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { FormControl, FormGroup } from '@angular/forms';
55
import { TranslateService } from '@ngx-translate/core';
66

77
import { AngularForms, Status } from '.';
8-
import { Group } from './group';
9-
import { DependencyService, Select, SelectService, Question } from './question';
8+
import { DataTable, Fieldset, Group, GroupType } from './group';
9+
import { Answer, DependencyService, Question, Select, SelectService } from './question';
1010
import { ReactiveFormsFactory } from './factory';
11-
import { StringUtils } from './util';
11+
import { ObjectUtils, StringUtils } from './util';
1212

1313
@Component({
1414
selector: 'rb-angular-forms',
@@ -187,6 +187,7 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
187187
@Output() public error: EventEmitter<Error> = new EventEmitter();
188188
@Output() public ready: EventEmitter<boolean> = new EventEmitter();
189189

190+
private _originalValueFormGroup: Object;
190191
private _status: Status;
191192

192193
public constructor(
@@ -249,28 +250,28 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
249250
}
250251

251252
public isPristine(): boolean {
252-
return this.formGroup.pristine;
253+
return ObjectUtils.isEquals(this._originalValueFormGroup, this.formGroup.value);
253254
}
254255

255256
public isDirty(): boolean {
256-
return this.formGroup.dirty;
257+
return !ObjectUtils.isEquals(this._originalValueFormGroup, this.formGroup.value);
257258
}
258259

259260
public isValid(): boolean {
260261
return this.formGroup.valid;
261262
}
262263

263264
public getAnswersGroups(): Object {
264-
const answersGroups: Object = this.formGroup.value;
265+
const answersGroups: Object = ObjectUtils.clone(this.formGroup.value);
265266

266267
Object.keys(answersGroups).forEach((groupIndex: string) => {
267268
if (answersGroups[groupIndex] instanceof Array) {
268-
(<Array<Object>>answersGroups[groupIndex]).map((answersGroup: Object) => this.convertAnswersOfGroupToString(answersGroup));
269-
269+
(<Array<Object>>answersGroups[groupIndex]).map((answersGroup: Object, index: number) =>
270+
this.convertAnswersDataTable(answersGroup, <DataTable>this.getGroupByCode(groupIndex), index + 1));
270271
return;
271272
}
272273

273-
answersGroups[groupIndex] = this.convertAnswersOfGroupToString(answersGroups[groupIndex]);
274+
answersGroups[groupIndex] = this.convertAnswersFieldset(answersGroups[groupIndex], <Fieldset>this.getGroupByCode(groupIndex));
274275
});
275276

276277
return answersGroups;
@@ -283,7 +284,6 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
283284
Object.keys(answersGroups).forEach((groupIndex: string) => {
284285
if (answersGroups[groupIndex] instanceof Array) {
285286
answers[groupIndex] = answersGroups[groupIndex];
286-
287287
return;
288288
}
289289

@@ -300,6 +300,10 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
300300
this.error.emit(error);
301301
}
302302

303+
public get originalValueFormGroup(): Object {
304+
return this._originalValueFormGroup;
305+
}
306+
303307
public get status(): Status {
304308
return this._status;
305309
}
@@ -315,16 +319,38 @@ export class AngularFormsComponent implements OnInit, OnChanges, AfterViewChecke
315319
try {
316320
this.groups = await AngularForms.fromJson(this.groups);
317321
this.formGroup = await ReactiveFormsFactory.createFormGroupFromGroups(this.groups);
322+
setTimeout(() => this.initOriginalValueFormGroup(), 2000);
318323
resolve();
319324
} catch (error) {
320325
reject(error);
321326
}
322327
});
323328
}
324329

325-
private convertAnswersOfGroupToString(answersGroup: Object): Object {
326-
Object.keys(answersGroup)
327-
.forEach((questionIndex: string) => answersGroup[questionIndex] = StringUtils.convertToString(answersGroup[questionIndex]));
330+
private initOriginalValueFormGroup(): void {
331+
this._originalValueFormGroup = ObjectUtils.clone(this.formGroup.value);
332+
}
333+
334+
private convertAnswersFieldset(answersGroup: Object, group: Fieldset): Object {
335+
Object.keys(answersGroup).forEach((questionName: string) => {
336+
const question: Question<any> = group.getQuestionByName(questionName);
337+
const id: number | string = (<Answer<any>>question.answer).id || null;
338+
const value: string = StringUtils.convertToString(answersGroup[questionName]);
339+
340+
answersGroup[questionName] = new Answer(value, id);
341+
});
342+
343+
return answersGroup;
344+
}
345+
346+
private convertAnswersDataTable(answersGroup: Object, group: DataTable, index: number): Object {
347+
Object.keys(answersGroup).forEach((questionName: string) => {
348+
const question: Question<any> = group.getQuestionByNameAndIndex(questionName, index);
349+
const id: number | string = question ? (<Answer<any>>question.answer).id : null;
350+
const value: string = StringUtils.convertToString(answersGroup[questionName]);
351+
352+
answersGroup[questionName] = new Answer(value, id);
353+
});
328354

329355
return answersGroup;
330356
}

src/factory/answer-factory.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { isNullOrUndefined } from 'util';
2+
3+
import { Answer } from '../question';
4+
5+
export class AnswerFactory {
6+
7+
public static create<AnswerType>(answer: Answer<AnswerType> | AnswerType): Answer<AnswerType> {
8+
return !isNullOrUndefined(answer) && 'object' === typeof answer
9+
? new Answer<AnswerType>(answer['value'], answer['id'])
10+
: new Answer<AnswerType>(answer);
11+
}
12+
}

src/factory/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './validation-factory';
2+
export * from './answer-factory';
23
export * from './question-factory';
34
export * from './validator-factory';
45
export * from './reactive-forms-factory';

src/factory/reactive-forms-factory.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ReactiveFormsFactory {
4646

4747
for await (const question of questions) {
4848
const validators: ValidatorFn[] = await ReactiveFormsFactory.createValidators(question.validations);
49-
const answer: any = !question.answer && (<Choice>question).defaultOption ? (<Choice>question).defaultOption : question.answer;
49+
const answer: any = ReactiveFormsFactory.getFormStateValue(question);
5050
const formState: any = { value: answer, disabled: checkDisabledQuestions && question.disabled };
5151
const control: FormControl = new FormControl(formState, validators);
5252

@@ -73,7 +73,7 @@ export class ReactiveFormsFactory {
7373
const group: FormGroup = new FormGroup({});
7474

7575
for (const column of question) {
76-
group.addControl(column.name, new FormControl(column.answer));
76+
group.addControl(column.name, new FormControl(column.answer ? column.answer.value : null));
7777
}
7878

7979
formArray.push(group);
@@ -110,4 +110,16 @@ export class ReactiveFormsFactory {
110110
}
111111
});
112112
}
113+
114+
private static getFormStateValue(question: Question<any>): any {
115+
if ((<Choice>question).defaultOption && (!question.answer || !question.answer.value)) {
116+
return (<Choice>question).defaultOption;
117+
}
118+
119+
if (question.answer && question.answer.value) {
120+
return question.answer.value;
121+
}
122+
123+
return null;
124+
}
113125
}

0 commit comments

Comments
 (0)