|
| 1 | +import { ChangeDetectionStrategy, Component, DestroyRef, inject, Injectable } from '@angular/core'; |
| 2 | +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
| 3 | +import { |
| 4 | + AbstractControl, |
| 5 | + FormControl, |
| 6 | + FormGroup, |
| 7 | + FormGroupDirective, |
| 8 | + NgForm, |
| 9 | + ReactiveFormsModule, |
| 10 | + ValidatorFn, |
| 11 | + Validators |
| 12 | +} from '@angular/forms'; |
| 13 | +import { KbqButtonModule } from '@koobiq/components/button'; |
| 14 | +import { |
| 15 | + ErrorStateMatcher, |
| 16 | + kbqDisableLegacyValidationDirectiveProvider, |
| 17 | + kbqErrorStateMatcherProvider |
| 18 | +} from '@koobiq/components/core'; |
| 19 | +import { KbqFormFieldModule, KbqPasswordToggle } from '@koobiq/components/form-field'; |
| 20 | +import { KbqInputModule } from '@koobiq/components/input'; |
| 21 | + |
| 22 | +@Injectable() |
| 23 | +class RequiredErrorStateMatcher implements ErrorStateMatcher { |
| 24 | + isErrorState(control: AbstractControl | null, form: FormGroupDirective | NgForm | null): boolean { |
| 25 | + return !!( |
| 26 | + (control?.hasError('required') && form?.submitted) || |
| 27 | + (control?.invalid && !control.hasError('required')) |
| 28 | + ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * @title Input change password |
| 34 | + */ |
| 35 | +@Component({ |
| 36 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 37 | + standalone: true, |
| 38 | + selector: 'input-change-password-example', |
| 39 | + imports: [ |
| 40 | + ReactiveFormsModule, |
| 41 | + KbqFormFieldModule, |
| 42 | + KbqInputModule, |
| 43 | + KbqPasswordToggle, |
| 44 | + KbqButtonModule |
| 45 | + ], |
| 46 | + template: ` |
| 47 | + <form class="layout-column layout-gap-m" [formGroup]="form"> |
| 48 | + <kbq-form-field style="width: 250px"> |
| 49 | + <input formControlName="newPassword" kbqInputPassword placeholder="Enter new password" /> |
| 50 | +
|
| 51 | + <kbq-password-toggle [kbqTooltipHidden]="'Show password'" [kbqTooltipNotHidden]="'Hide password'" /> |
| 52 | +
|
| 53 | + <kbq-hint>Password must not exceed 5 characters</kbq-hint> |
| 54 | +
|
| 55 | + @if (newPasswordControl.errors?.required) { |
| 56 | + <kbq-error>Required</kbq-error> |
| 57 | + } |
| 58 | +
|
| 59 | + @if (newPasswordControl.errors?.maxlength) { |
| 60 | + <kbq-error>Max Length</kbq-error> |
| 61 | + } |
| 62 | + </kbq-form-field> |
| 63 | +
|
| 64 | + <kbq-form-field style="width: 250px"> |
| 65 | + <input formControlName="confirmNewPassword" kbqInputPassword placeholder="Confirm new password" /> |
| 66 | +
|
| 67 | + <kbq-password-toggle [kbqTooltipHidden]="'Show password'" [kbqTooltipNotHidden]="'Hide password'" /> |
| 68 | +
|
| 69 | + @if (confirmNewPasswordControl.errors?.required) { |
| 70 | + <kbq-error>Required</kbq-error> |
| 71 | + } |
| 72 | +
|
| 73 | + @if (confirmNewPasswordControl.errors?.maxlength) { |
| 74 | + <kbq-error>Max Length</kbq-error> |
| 75 | + } |
| 76 | +
|
| 77 | + @if (confirmNewPasswordControl.errors?.passwordsNotMatch) { |
| 78 | + <kbq-error>Not Match</kbq-error> |
| 79 | + } |
| 80 | + </kbq-form-field> |
| 81 | +
|
| 82 | + <div> |
| 83 | + <button kbq-button type="submit">ChangePassword</button> |
| 84 | + </div> |
| 85 | + </form> |
| 86 | + `, |
| 87 | + providers: [ |
| 88 | + kbqDisableLegacyValidationDirectiveProvider(), |
| 89 | + kbqErrorStateMatcherProvider(RequiredErrorStateMatcher)] |
| 90 | +}) |
| 91 | +export class InputChangePasswordExample { |
| 92 | + protected readonly destroyRef = inject(DestroyRef); |
| 93 | + protected readonly form: FormGroup<{ |
| 94 | + newPassword: FormControl<string | null>; |
| 95 | + confirmNewPassword: FormControl<string | null>; |
| 96 | + }>; |
| 97 | + |
| 98 | + protected get newPasswordControl(): AbstractControl { |
| 99 | + return this.form.controls.newPassword; |
| 100 | + } |
| 101 | + |
| 102 | + protected get confirmNewPasswordControl(): AbstractControl { |
| 103 | + return this.form.controls.confirmNewPassword; |
| 104 | + } |
| 105 | + |
| 106 | + constructor() { |
| 107 | + this.form = this.createForm(); |
| 108 | + } |
| 109 | + |
| 110 | + createForm() { |
| 111 | + const compareWith = |
| 112 | + (compareControlName: string): ValidatorFn => |
| 113 | + (control: AbstractControl) => { |
| 114 | + const controlToCompareWith = control.parent?.get(compareControlName); |
| 115 | + const isValid = !control.value || controlToCompareWith?.value === control.value; |
| 116 | + |
| 117 | + return isValid ? null : { passwordsNotMatch: { value: control.value } }; |
| 118 | + }; |
| 119 | + |
| 120 | + const loginForm = new FormGroup({ |
| 121 | + newPassword: new FormControl<string>('', [Validators.required, Validators.maxLength(5)]), |
| 122 | + confirmNewPassword: new FormControl<string>('', [ |
| 123 | + Validators.required, |
| 124 | + Validators.maxLength(5), |
| 125 | + compareWith('newPassword')]) |
| 126 | + }); |
| 127 | + |
| 128 | + // run validation for confirmNewPassword after newPassword changed |
| 129 | + loginForm |
| 130 | + .get('newPassword') |
| 131 | + ?.statusChanges.pipe(takeUntilDestroyed(this.destroyRef)) |
| 132 | + .subscribe(() => { |
| 133 | + loginForm.get('confirmNewPassword')?.updateValueAndValidity(); |
| 134 | + }); |
| 135 | + |
| 136 | + return loginForm; |
| 137 | + } |
| 138 | +} |
0 commit comments