Created
December 24, 2024 19:36
-
-
Save rakia/c3fa325571ad7672fc37312a057c3f73 to your computer and use it in GitHub Desktop.
Angular component: password-field
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
@Component({ | |
selector: 'app-password-field', | |
templateUrl: './password-field.component.html', | |
standalone: true, | |
changeDetection: ChangeDetectionStrategy.OnPush, | |
imports: [ReactiveFormsModule, StrengthMeterComponent] | |
}) | |
export class PasswordFieldComponent implements OnInit { | |
destroyRef = inject(DestroyRef); | |
@Input() passwordFormControl: FormControl = new FormControl<any>(''); | |
showPassword = false; | |
passwordStrength: number = 0; | |
ngOnInit(): void { | |
this.passwordFormControl.valueChanges | |
.pipe( | |
debounceTime(200), // wait for 200ms after the last keystroke | |
distinctUntilChanged(), // trigger only when the value actually changes | |
takeUntilDestroyed(this.destroyRef)) // unsubscribe when the component is destroyed | |
.subscribe((value) => { | |
// run logic | |
this.passwordStrength = this.calculatePasswordStrength(value); | |
}); | |
} | |
/** | |
* In the passwordRules, we check whether each the user has considered each factor that makes their password stronger. | |
* This is reflected on the UI as well to guide the user. | |
*/ | |
get passwordRules() { | |
// ... | |
} | |
calculatePasswordStrength(password: string): number { | |
// ... | |
} | |
togglePasswordVisibility(): void { | |
this.showPassword = !this.showPassword; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment