Created
May 12, 2018 21:05
-
-
Save realtomaszkula/b22a4c62dadc6990094b33117f76f0b7 to your computer and use it in GitHub Desktop.
This file contains 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 } from '@angular/core'; | |
import { FormBuilder, FormGroup } from '@angular/forms'; | |
import { combineLatest } from 'rxjs'; | |
import { map, filter } from 'rxjs/operators'; | |
@Component({ | |
selector: 'app-suggested-username', | |
template: ` | |
<form [formGroup]="form"> | |
<input formControlName="firstName" placeholder="First name" /> | |
<input formControlName="lastName" placeholder="Last name" /> | |
<input formControlName="username" placeholder="Username" /> | |
</form> | |
`, | |
styleUrls: ['./suggested-username.component.css'] | |
}) | |
export class SuggestedUsernameComponent implements OnInit { | |
form: FormGroup; | |
constructor(private fb: FormBuilder) {} | |
ngOnInit() { | |
this.form = this.fb.group({ | |
firstName: '', | |
lastName: '', | |
username: '' | |
}); | |
this.reactiveUsername(); | |
} | |
reactiveUsername() { | |
const firstName = this.form.get('firstName'); | |
const lastName = this.form.get('lastName'); | |
const username = this.form.get('username'); | |
const combined$ = combineLatest( | |
firstName.valueChanges, | |
lastName.valueChanges | |
) | |
.pipe(map(([first, last]) => (first && last ? `${first}-${last}` : ''))) | |
.subscribe(value => username.setValue(value)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment