Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ee28318
init forms
iurynogueira Jan 5, 2023
fe882a8
chore: render forms
mizek1 Jan 6, 2023
6ef3404
feat: setting up a component to test form on storybook
mizek1 Jan 10, 2023
6f2c257
refactor: render fields
mizek1 Jan 10, 2023
f326fa7
feat: tistreza
mizek1 Jan 11, 2023
a470d86
feat: alegriaaa
mizek1 Jan 12, 2023
cf5d2ec
feat: add label and refactor tests for input component
mizek1 Jan 13, 2023
afae3fd
feat: adding validators v1
mizek1 Jan 16, 2023
859f4f3
feat: textarea
mizek1 Jan 16, 2023
b06cc52
feat: check model
mizek1 Jan 16, 2023
83a7071
Merge remote-tracking branch 'origin/main' into forms
mizek1 Jan 16, 2023
1770c03
feat: add first tests
mizek1 Jan 17, 2023
2e9ca02
Merge branch 'main' into forms
mizek1 Jan 19, 2023
035018e
refactor: field key
mizek1 Jan 19, 2023
a241681
Merge remote-tracking branch 'origin/main' into forms
mizek1 Jan 19, 2023
1f07907
test: add input area with angular forms
mizek1 Jan 19, 2023
e4181b4
feat: switch component on ion form
mizek1 Jan 19, 2023
589073b
fix: switch with form
mizek1 Jan 19, 2023
20e9fce
Merge remote-tracking branch 'origin/main' into forms
mizek1 Jan 23, 2023
586bef3
fix: write value on switch + tests
mizek1 Jan 23, 2023
26f7079
test: textField
mizek1 Jan 23, 2023
3cb003c
test: textAreaField
mizek1 Jan 23, 2023
6a17b08
test: switchField
mizek1 Jan 23, 2023
bc0012e
Merge branch 'main' into forms
mizek1 Jan 24, 2023
28f665a
Merge branch 'main' into forms
iurynogueira Jan 25, 2023
151f7b8
Merge branch 'main' into forms
mizek1 Feb 2, 2023
c892632
Merge branch 'main' of github.com:Brisanet/ion into forms
mizek1 Feb 16, 2023
7b53f2d
fix: form module
mizek1 Feb 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions projects/ion/src/lib/alert/alert.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ export class IonAlertComponent implements OnInit {

@ViewChild('ionAlert', { static: false }) private ionAlert: ElementRef;

// public formConfig = {
// fields: [
// new TextField({
// key: 'nome',
// label: 'Nome',
// required: true,
// placeholder: 'Digite um nome',
// // icon: 'check',
// }),
// ],
// model: {
// select: [1, 4, 1230],
// },
// };

iconType: IconType;

closeEvent(): void {
Expand Down
1 change: 1 addition & 0 deletions projects/ion/src/lib/core/types/input-area.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EventEmitter } from '@angular/core';

export interface IonInputAreaProps {
key?: string;
cols?: string;
rows?: string;
disabled?: boolean;
Expand Down
1 change: 1 addition & 0 deletions projects/ion/src/lib/core/types/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IconDirection } from './icon';
export type InputType = 'text' | 'password';

export interface IonInputProps {
key?: string;
placeholder?: string;
button?: string;
iconInput?: string;
Expand Down
61 changes: 61 additions & 0 deletions projects/ion/src/lib/form/core/baseField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { AbstractControl, ValidatorFn, Validators } from '@angular/forms';

export interface IFormField {
disabled?: boolean;
show?: boolean;
size?: number;
required?: boolean;
validators?: ValidatorFn[];
}

export abstract class FormField {
show: boolean;
size: number;
type: string;
required: boolean;
private _key: string;
private formControl: AbstractControl;

constructor(
private disabled = false,
show = true,
size = 4,
required = false,
private readonly validators = []
) {
this.show = show;
this.size = size;
this.required = required;
}

setFormControl(control: AbstractControl): void {
this.formControl = control;
}

setDisable(value: boolean): void {
this.disabled = value;
value && this.formControl
? this.formControl.disable()
: this.formControl.enable();
this.formControl.updateValueAndValidity();
}

set key(key: string) {
this._key = key;
}

get key(): string {
return this._key;
}

getDisabled(): boolean {
return this.disabled;
}

getValidators(): ValidatorFn[] {
if (this.required) {
this.validators.push(Validators.required);
}
return this.validators;
}
}
44 changes: 44 additions & 0 deletions projects/ion/src/lib/form/core/switchField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { FormControl, Validators } from '@angular/forms';
import { ISwitchField, SwitchField } from './switchField';

const fields: ISwitchField = {
label: 'Teste',
};

describe('TextAreaField', () => {
let switchField: SwitchField;
const formControl = new FormControl(false);

beforeAll(() => {
switchField = new SwitchField({ ...fields });
switchField.setFormControl(formControl);
});

it('should instantiate a switch field', () => {
expect(switchField).toBeInstanceOf(SwitchField);
});
it('should set a key on field', () => {
switchField.key = 'test';
expect(switchField.key).toBe('test');
});
it('should have type switch', () => {
expect(switchField.type).toBe('switch');
});
it('should disable field', () => {
switchField.setDisable(true);
expect(switchField.getDisabled()).toBeTruthy();
expect(formControl.disabled).toBeTruthy();
});
it('should enable field', () => {
switchField.setDisable(false);
expect(switchField.getDisabled()).toBeFalsy();
expect(formControl.enabled).toBeTruthy();
});
it('should have required validator', () => {
expect(switchField.getValidators()).not.toContain(Validators.required);
});
it('should not have required validator', () => {
switchField = new SwitchField({ ...fields, required: true });
expect(switchField.getValidators()).toContain(Validators.required);
});
});
21 changes: 21 additions & 0 deletions projects/ion/src/lib/form/core/switchField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FormField, IFormField } from './baseField';

export interface ISwitchField extends IFormField {
label: string;
}

export class SwitchField extends FormField {
label = '';
type = 'switch';

constructor({ label, ...props }: ISwitchField) {
super(
props.disabled,
props.show,
props.size,
props.required,
props.validators
);
this.label = label;
}
}
46 changes: 46 additions & 0 deletions projects/ion/src/lib/form/core/textAreaField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FormControl, Validators } from '@angular/forms';
import { ITextAreaField, TextAreaField } from './textAreaField';
import { TextField } from './textField';

const fields: ITextAreaField = {
label: 'Teste',
placeholder: 'Teste',
};

describe('TextAreaField', () => {
let textAreaField: TextAreaField;
const formControl = new FormControl('');

beforeAll(() => {
textAreaField = new TextAreaField({ ...fields });
textAreaField.setFormControl(formControl);
});

it('should instantiate a text field', () => {
expect(textAreaField).toBeInstanceOf(TextAreaField);
});
it('should set a key on field', () => {
textAreaField.key = 'test';
expect(textAreaField.key).toBe('test');
});
it('should have type text', () => {
expect(textAreaField.type).toBe('textarea');
});
it('should disable field', () => {
textAreaField.setDisable(true);
expect(textAreaField.getDisabled()).toBeTruthy();
expect(formControl.disabled).toBeTruthy();
});
it('should enable field', () => {
textAreaField.setDisable(false);
expect(textAreaField.getDisabled()).toBeFalsy();
expect(formControl.enabled).toBeTruthy();
});
it('should have required validator', () => {
expect(textAreaField.getValidators()).not.toContain(Validators.required);
});
it('should not have required validator', () => {
textAreaField = new TextField({ ...fields, required: true });
expect(textAreaField.getValidators()).toContain(Validators.required);
});
});
26 changes: 26 additions & 0 deletions projects/ion/src/lib/form/core/textAreaField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FormField, IFormField } from './baseField';

export interface ITextAreaField extends IFormField {
label: string;
placeholder: string;
cols?: string;
rows?: string;
}

export class TextAreaField extends FormField {
label = '';
placeholder: string;
type = 'textarea';

constructor({ placeholder, label, ...props }: ITextAreaField) {
super(
props.disabled,
props.show,
props.size,
props.required,
props.validators
);
this.label = label;
this.placeholder = placeholder;
}
}
45 changes: 45 additions & 0 deletions projects/ion/src/lib/form/core/textField.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { FormControl, Validators } from '@angular/forms';
import { ITextField, TextField } from './textField';

const fields: ITextField = {
label: 'Teste',
placeholder: 'Teste',
};

describe('TextField', () => {
let textField: TextField;
const formControl = new FormControl('');

beforeAll(() => {
textField = new TextField({ ...fields });
textField.setFormControl(formControl);
});

it('should instantiate a text field', () => {
expect(textField).toBeInstanceOf(TextField);
});
it('should set a key on field', () => {
textField.key = 'test';
expect(textField.key).toBe('test');
});
it('should have type text', () => {
expect(textField.type).toBe('text');
});
it('should disable field', () => {
textField.setDisable(true);
expect(textField.getDisabled()).toBeTruthy();
expect(formControl.disabled).toBeTruthy();
});
it('should enable field', () => {
textField.setDisable(false);
expect(textField.getDisabled()).toBeFalsy();
expect(formControl.enabled).toBeTruthy();
});
it('should have required validator', () => {
expect(textField.getValidators()).not.toContain(Validators.required);
});
it('should not have required validator', () => {
textField = new TextField({ ...fields, required: true });
expect(textField.getValidators()).toContain(Validators.required);
});
});
27 changes: 27 additions & 0 deletions projects/ion/src/lib/form/core/textField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FormField, IFormField } from './baseField';

export interface ITextField extends IFormField {
label: string;
placeholder: string;
icon?: string;
}

export class TextField extends FormField {
label = '';
placeholder: string;
icon: string;
type = 'text';

constructor({ placeholder, icon, label, ...props }: ITextField) {
super(
props.disabled,
props.show,
props.size,
props.required,
props.validators
);
this.label = label;
this.placeholder = placeholder;
this.icon = icon;
}
}
74 changes: 74 additions & 0 deletions projects/ion/src/lib/form/form.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<h1>Formulário</h1>
<form [formGroup]="formGroup" name="form">
<ng-container *ngFor="let field of formFields">
<ng-container [ngSwitch]="field.type">
<!-- InputText -->
<ng-container *ngSwitchCase="'text'">
<div *ngIf="formGroup.controls[field.key]">
<label [for]="field.key">
{{ field.label }}
</label>
<ion-input
[key]="field.key"
[placeholder]="field.placeholder"
[iconInput]="field.icon"
[formControlName]="field.key"
[invalid]="
formGroup.controls[field.key]?.invalid &&
(formGroup.controls[field.key]?.dirty ||
formGroup.controls[field.key]?.touched)
"
></ion-input>
</div>
</ng-container>
<!-- TextArea -->
<ng-container *ngSwitchCase="'textarea'">
<div *ngIf="formGroup.controls[field.key]">
<label [for]="field.key">
{{ field.label }}
</label>
<ion-input-area
[key]="field.key"
[placeholder]="field.placeholder"
[formControlName]="field.key"
></ion-input-area>
</div>
</ng-container>
<!-- SwitchField -->
<ng-container *ngSwitchCase="'switch'">
<div *ngIf="formGroup.controls[field.key]">
<label [for]="field.key">
{{ field.label }}
</label>
<ion-switch
[key]="field.key"
[formControlName]="field.key"
></ion-switch>
</div>
</ng-container>
</ng-container>

<div
*ngIf="
formGroup.controls[field.key]?.invalid &&
(formGroup.controls[field.key]?.dirty ||
formGroup.controls[field.key]?.touched)
"
>
<span *ngIf="formGroup.controls[field.key]?.errors?.required">
Campo obrigatório!
</span>
<span *ngIf="formGroup.controls[field.key]?.errors?.email">
E-mail inválido.
</span>
<span *ngIf="formGroup.controls[field.key]?.errors?.minlength">
Pequeno demais (tamanho mínimo:
{{ formGroup.controls[field.key]?.errors?.minlength.requiredLength }})
</span>
</div>

<span>Value: {{ formGroup.controls[field.key]?.value }} | </span>
<span>Valid: {{ formGroup.controls[field.key]?.valid }} | </span>
<span>Disabled: {{ formGroup.controls[field.key]?.disabled }}</span>
</ng-container>
</form>
Empty file.
Loading