Created
December 4, 2017 08:30
-
-
Save AndrewAllison/dd7f831378bf66f6ac655d167713f0b6 to your computer and use it in GitHub Desktop.
Using reactive forms in a nice simple manner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class FormItemChange { | |
value: any; | |
isValid: boolean; | |
constructor(model: any) { | |
const { value, isValid } = model; | |
this.value = value; | |
this.isValid = isValid; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngIf="selectedField" [ngSwitch]="selectedField.type"> | |
<app-search-type-text [formValue]="criteriaValue" (valueChanged)="onValueChanged($event)" *ngSwitchCase="'text'"></app-search-type-text> | |
<button mat-raised-button [disabled]="!isValid" (click)="saveCriteria()">Add Criteria</button> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnDestroy, OnInit } from '@angular/core'; | |
import { FormControl, FormGroup } from '@angular/forms'; | |
import { NativeDateAdapter, DateAdapter } from '@angular/material'; | |
import { SearchCriteriaService } from 'app/search/main/search-panel/search-criteria.service'; | |
import { SearchCriteriaDataService } from 'app/search/main/search-panel/search-criteria-data.service'; | |
import { SearchCriteria } from 'app/search/models/search-criteria.model'; | |
import { SearchField } from 'app/search/models/search-fiields.model'; | |
import { FormItemChange } from 'app/search/models/form-item-change.model'; | |
@Component({ | |
selector: 'app-search-panel', | |
templateUrl: './search-panel.component.html', | |
styleUrls: ['./search-panel.component.scss'] | |
}) | |
export class SearchPanelComponent implements OnInit, OnDestroy { | |
criteriaValue: string; | |
isValid = false; | |
private _isActive = true; | |
ngOnDestroy() { | |
this._isActive = false; | |
} | |
onValueChanged(change: FormItemChange) { | |
this.criteriaValue = change.value; | |
this.isValid = change.isValid; | |
console.log(change); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div [formGroup]="searchTermFormGroup"> | |
<mat-form-field> | |
<input matInput formControlName="searchValueControl"> | |
<mat-error> | |
Search term | |
<strong>required</strong> | |
</mat-error> | |
</mat-form-field> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core'; | |
import { FormGroup } from '@angular/forms'; | |
import { FormBuilder, FormControl } from '@angular/forms'; | |
import { Validators } from '@angular/forms'; | |
import { FormItemChange } from 'app/search/models/form-item-change.model'; | |
@Component({ | |
selector: 'app-search-type-text', | |
templateUrl: './search-type-text.component.html', | |
styleUrls: ['./search-type-text.component.scss'] | |
}) | |
export class SearchTypeTextComponent implements OnInit { | |
@Input() formValue; | |
searchTermFormGroup: FormGroup; | |
@Output() valueChanged = new EventEmitter(); | |
constructor() { } | |
ngOnInit() { | |
this.searchTermFormGroup = new FormGroup({ | |
searchValueControl: new FormControl(this.formValue, [Validators.required]) | |
}); | |
this.searchTermFormGroup.valueChanges | |
.subscribe((change) => { | |
this.valueChanged.emit(new FormItemChange({ | |
value: change.searchValueControl, | |
isValid: this.searchTermFormGroup.valid | |
})); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment